From 45dcdab0833d7561f343b5abf734e14fd86909ce Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 21 Nov 2023 11:39:19 +0800 Subject: [PATCH] fix: add i18n resources after server app load (#3068) * fix: add i18n resources after server app load * fix: skip cache --- packages/core/server/src/application.ts | 10 +++++++--- packages/core/server/src/locale/locale.ts | 14 ++++++++++++++ packages/core/server/src/middlewares/i18n.ts | 1 + .../@nocobase/plugin-client/src/server/server.ts | 8 ++++---- .../src/server/actions/localization.ts | 2 +- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index 3f773d4046..7bd995eb99 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -1,7 +1,7 @@ import { ACL } from '@nocobase/acl'; import { registerActions } from '@nocobase/actions'; import { actions as authActions, AuthManager, AuthManagerOptions } from '@nocobase/auth'; -import { CacheManagerOptions, Cache, CacheManager } from '@nocobase/cache'; +import { Cache, CacheManager, CacheManagerOptions } from '@nocobase/cache'; import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/database'; import { AppLoggerOptions, createAppLogger, Logger } from '@nocobase/logger'; import { ResourceOptions, Resourcer } from '@nocobase/resourcer'; @@ -16,15 +16,15 @@ import lodash from 'lodash'; import { createACL } from './acl'; import { AppCommand } from './app-command'; import { AppSupervisor } from './app-supervisor'; +import { createCacheManager } from './cache'; import { registerCli } from './commands'; +import { CronJobManager } from './cron/cron-job-manager'; import { ApplicationNotInstall } from './errors/application-not-install'; import { createAppProxy, createI18n, createResourcer, getCommandFullName, registerMiddlewares } from './helper'; import { ApplicationVersion } from './helpers/application-version'; import { Locale } from './locale'; import { Plugin } from './plugin'; import { InstallOptions, PluginManager } from './plugin-manager'; -import { CronJobManager } from './cron/cron-job-manager'; -import { createCacheManager } from './cache'; const packageJson = require('../package.json'); @@ -220,6 +220,10 @@ export class Application exten return this._locales; } + get localeManager() { + return this._locales; + } + protected _version: ApplicationVersion; get version() { diff --git a/packages/core/server/src/locale/locale.ts b/packages/core/server/src/locale/locale.ts index 0eabf4df5b..f5835bef7f 100644 --- a/packages/core/server/src/locale/locale.ts +++ b/packages/core/server/src/locale/locale.ts @@ -8,6 +8,7 @@ export class Locale { cache: Cache; defaultLang = 'en-US'; localeFn = new Map(); + resourceCached = new Map(); constructor(app: Application) { this.app = app; @@ -54,7 +55,17 @@ export class Locale { }); } + async loadResourcesByLang(lang: string) { + if (!this.cache) { + return; + } + if (!this.resourceCached.has(lang)) { + await this.getCacheResources(lang); + } + } + async getCacheResources(lang: string) { + this.resourceCached.set(lang, true); return await this.wrapCache(`resources:${lang}`, () => this.getResources(lang)); } @@ -81,6 +92,9 @@ export class Locale { // empty } } + Object.keys(resources).forEach((name) => { + this.app.i18n.addResources(lang, name, resources[name]); + }); return resources; } } diff --git a/packages/core/server/src/middlewares/i18n.ts b/packages/core/server/src/middlewares/i18n.ts index ff78562b34..8e2a4245ef 100644 --- a/packages/core/server/src/middlewares/i18n.ts +++ b/packages/core/server/src/middlewares/i18n.ts @@ -14,6 +14,7 @@ export async function i18n(ctx, next) { const lng = ctx.getCurrentLocale(); if (lng !== '*' && lng) { i18n.changeLanguage(lng); + await ctx.app.localeManager.loadResourcesByLang(lng); } await next(); } diff --git a/packages/plugins/@nocobase/plugin-client/src/server/server.ts b/packages/plugins/@nocobase/plugin-client/src/server/server.ts index 4b4608c2fd..6bbcabbff6 100644 --- a/packages/plugins/@nocobase/plugin-client/src/server/server.ts +++ b/packages/plugins/@nocobase/plugin-client/src/server/server.ts @@ -57,9 +57,9 @@ export class ClientPlugin extends Plugin { } async load() { - this.app.locales.setLocaleFn('antd', async (lang) => getAntdLocale(lang)); - this.app.locales.setLocaleFn('cronstrue', async (lang) => getCronstrueLocale(lang)); - this.app.locales.setLocaleFn('cron', async (lang) => getCronLocale(lang)); + this.app.localeManager.setLocaleFn('antd', async (lang) => getAntdLocale(lang)); + this.app.localeManager.setLocaleFn('cronstrue', async (lang) => getCronstrueLocale(lang)); + this.app.localeManager.setLocaleFn('cron', async (lang) => getCronLocale(lang)); this.db.addMigrations({ namespace: 'client', directory: resolve(__dirname, './migrations'), @@ -107,7 +107,7 @@ export class ClientPlugin extends Plugin { }, async getLang(ctx, next) { const lang = await getLang(ctx); - const resources = await ctx.app.locales.get(lang); + const resources = await ctx.app.localeManager.get(lang); ctx.body = { lang, ...resources, diff --git a/packages/plugins/@nocobase/plugin-localization-management/src/server/actions/localization.ts b/packages/plugins/@nocobase/plugin-localization-management/src/server/actions/localization.ts index 6b79018589..756ffcad3b 100644 --- a/packages/plugins/@nocobase/plugin-localization-management/src/server/actions/localization.ts +++ b/packages/plugins/@nocobase/plugin-localization-management/src/server/actions/localization.ts @@ -10,7 +10,7 @@ const getResourcesInstance = async (ctx: Context) => { }; export const getResources = async (ctx: Context) => { - const resources = await ctx.app.locales.getCacheResources(ctx.get('X-Locale') || 'en-US'); + const resources = await ctx.app.localeManager.getCacheResources(ctx.get('X-Locale') || 'en-US'); const client = resources['client']; // Remove duplicated keys Object.keys(resources).forEach((module) => {