fix: add i18n resources after server app load (#3068)

* fix: add i18n resources after server app load

* fix: skip cache
This commit is contained in:
chenos 2023-11-21 11:39:19 +08:00 committed by GitHub
parent 78c4334e19
commit 45dcdab083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import { ACL } from '@nocobase/acl'; import { ACL } from '@nocobase/acl';
import { registerActions } from '@nocobase/actions'; import { registerActions } from '@nocobase/actions';
import { actions as authActions, AuthManager, AuthManagerOptions } from '@nocobase/auth'; 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 Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/database';
import { AppLoggerOptions, createAppLogger, Logger } from '@nocobase/logger'; import { AppLoggerOptions, createAppLogger, Logger } from '@nocobase/logger';
import { ResourceOptions, Resourcer } from '@nocobase/resourcer'; import { ResourceOptions, Resourcer } from '@nocobase/resourcer';
@ -16,15 +16,15 @@ import lodash from 'lodash';
import { createACL } from './acl'; import { createACL } from './acl';
import { AppCommand } from './app-command'; import { AppCommand } from './app-command';
import { AppSupervisor } from './app-supervisor'; import { AppSupervisor } from './app-supervisor';
import { createCacheManager } from './cache';
import { registerCli } from './commands'; import { registerCli } from './commands';
import { CronJobManager } from './cron/cron-job-manager';
import { ApplicationNotInstall } from './errors/application-not-install'; import { ApplicationNotInstall } from './errors/application-not-install';
import { createAppProxy, createI18n, createResourcer, getCommandFullName, registerMiddlewares } from './helper'; import { createAppProxy, createI18n, createResourcer, getCommandFullName, registerMiddlewares } from './helper';
import { ApplicationVersion } from './helpers/application-version'; import { ApplicationVersion } from './helpers/application-version';
import { Locale } from './locale'; import { Locale } from './locale';
import { Plugin } from './plugin'; import { Plugin } from './plugin';
import { InstallOptions, PluginManager } from './plugin-manager'; import { InstallOptions, PluginManager } from './plugin-manager';
import { CronJobManager } from './cron/cron-job-manager';
import { createCacheManager } from './cache';
const packageJson = require('../package.json'); const packageJson = require('../package.json');
@ -220,6 +220,10 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
return this._locales; return this._locales;
} }
get localeManager() {
return this._locales;
}
protected _version: ApplicationVersion; protected _version: ApplicationVersion;
get version() { get version() {

View File

@ -8,6 +8,7 @@ export class Locale {
cache: Cache; cache: Cache;
defaultLang = 'en-US'; defaultLang = 'en-US';
localeFn = new Map(); localeFn = new Map();
resourceCached = new Map();
constructor(app: Application) { constructor(app: Application) {
this.app = app; 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) { async getCacheResources(lang: string) {
this.resourceCached.set(lang, true);
return await this.wrapCache(`resources:${lang}`, () => this.getResources(lang)); return await this.wrapCache(`resources:${lang}`, () => this.getResources(lang));
} }
@ -81,6 +92,9 @@ export class Locale {
// empty // empty
} }
} }
Object.keys(resources).forEach((name) => {
this.app.i18n.addResources(lang, name, resources[name]);
});
return resources; return resources;
} }
} }

View File

@ -14,6 +14,7 @@ export async function i18n(ctx, next) {
const lng = ctx.getCurrentLocale(); const lng = ctx.getCurrentLocale();
if (lng !== '*' && lng) { if (lng !== '*' && lng) {
i18n.changeLanguage(lng); i18n.changeLanguage(lng);
await ctx.app.localeManager.loadResourcesByLang(lng);
} }
await next(); await next();
} }

View File

@ -57,9 +57,9 @@ export class ClientPlugin extends Plugin {
} }
async load() { async load() {
this.app.locales.setLocaleFn('antd', async (lang) => getAntdLocale(lang)); this.app.localeManager.setLocaleFn('antd', async (lang) => getAntdLocale(lang));
this.app.locales.setLocaleFn('cronstrue', async (lang) => getCronstrueLocale(lang)); this.app.localeManager.setLocaleFn('cronstrue', async (lang) => getCronstrueLocale(lang));
this.app.locales.setLocaleFn('cron', async (lang) => getCronLocale(lang)); this.app.localeManager.setLocaleFn('cron', async (lang) => getCronLocale(lang));
this.db.addMigrations({ this.db.addMigrations({
namespace: 'client', namespace: 'client',
directory: resolve(__dirname, './migrations'), directory: resolve(__dirname, './migrations'),
@ -107,7 +107,7 @@ export class ClientPlugin extends Plugin {
}, },
async getLang(ctx, next) { async getLang(ctx, next) {
const lang = await getLang(ctx); const lang = await getLang(ctx);
const resources = await ctx.app.locales.get(lang); const resources = await ctx.app.localeManager.get(lang);
ctx.body = { ctx.body = {
lang, lang,
...resources, ...resources,

View File

@ -10,7 +10,7 @@ const getResourcesInstance = async (ctx: Context) => {
}; };
export const getResources = 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']; const client = resources['client'];
// Remove duplicated keys // Remove duplicated keys
Object.keys(resources).forEach((module) => { Object.keys(resources).forEach((module) => {