mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 09:17:23 +00:00
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:
parent
78c4334e19
commit
45dcdab083
@ -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<StateT = DefaultState, ContextT = DefaultContext> exten
|
||||
return this._locales;
|
||||
}
|
||||
|
||||
get localeManager() {
|
||||
return this._locales;
|
||||
}
|
||||
|
||||
protected _version: ApplicationVersion;
|
||||
|
||||
get version() {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user