feat: localizations
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -2,6 +2,8 @@ import type { App } from 'vue';
|
||||
import { router } from '@KTXC/router';
|
||||
import { useModuleStore } from '@KTXC/stores/moduleStore';
|
||||
import { useIntegrationStore } from '@KTXC/stores/integrationStore';
|
||||
import { useL10nStore } from '@KTXC/stores/l10nStore';
|
||||
import type { CatalogMessages } from '@KTXC/stores/l10nStore';
|
||||
|
||||
function installModuleCSS(moduleHandle: string, cssPaths: string | string[]): void {
|
||||
const cssFiles = Array.isArray(cssPaths) ? cssPaths : [cssPaths];
|
||||
@@ -46,12 +48,20 @@ function installModuleRoutes(moduleHandle: string, routes: any[]): void {
|
||||
});
|
||||
}
|
||||
|
||||
function installModuleIntegrations(
|
||||
moduleHandle: string,
|
||||
integrations: Record<string, any[]>
|
||||
): void {
|
||||
const integrationStore = useIntegrationStore();
|
||||
integrationStore.registerModuleIntegrations(moduleHandle, integrations);
|
||||
function installModuleIntegrations(moduleHandle: string, integrations: Record<string, any[]>): void {
|
||||
useIntegrationStore().registerModuleIntegrations(moduleHandle, integrations);
|
||||
}
|
||||
|
||||
async function installModuleLocalizations(moduleHandle: string): Promise<void> {
|
||||
await useL10nStore().registerCatalogProvider(moduleHandle, (locale) => loadModuleLocalizations(moduleHandle, locale));
|
||||
}
|
||||
|
||||
async function loadModuleLocalizations(moduleHandle: string, locale: string): Promise<CatalogMessages | null> {
|
||||
const response = await fetch(
|
||||
`/modules/${moduleHandle}/static/l10n/${locale}.json`,
|
||||
{ headers: { Accept: 'application/json' } },
|
||||
);
|
||||
return response.ok ? await response.json() : null;
|
||||
}
|
||||
|
||||
export async function initializeModules(app: App): Promise<void> {
|
||||
@@ -72,11 +82,16 @@ export async function initializeModules(app: App): Promise<void> {
|
||||
console.log(`Module Loader - Loading ${moduleInfo.handle} from ${moduleUrl}`);
|
||||
|
||||
const loadPromise = import(/* @vite-ignore */ moduleUrl)
|
||||
.then((module) => {
|
||||
.then(async (module) => {
|
||||
// Load CSS if module explicitly exports css path(s)
|
||||
if (module.css) {
|
||||
installModuleCSS(moduleInfo.handle, module.css);
|
||||
}
|
||||
// Load translation catalogs if module explicitly exports l10n flag
|
||||
if (module.l10n) {
|
||||
console.log(`Module Loader - Installing Catalogs ${moduleInfo.handle}`);
|
||||
await installModuleLocalizations(moduleHandle);
|
||||
}
|
||||
// install module
|
||||
console.log(`Module Loader - Installing ${moduleInfo.handle}`);
|
||||
if (module.default && typeof module.default.install === 'function') {
|
||||
@@ -104,4 +119,4 @@ export async function initializeModules(app: App): Promise<void> {
|
||||
|
||||
// Wait for all dynamic loading to complete
|
||||
await Promise.all(loadPromises);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user