diff --git a/packages/core/cache/src/cache-manager.ts b/packages/core/cache/src/cache-manager.ts index e39545b10e..79ff749245 100644 --- a/packages/core/cache/src/cache-manager.ts +++ b/packages/core/cache/src/cache-manager.ts @@ -100,7 +100,7 @@ export class CacheManager { async createCache(options: { name: string; prefix?: string; store?: string; [key: string]: any }) { const { name, prefix, store = this.defaultStore, ...config } = options; - if (!lodash.isEmpty(config)) { + if (!lodash.isEmpty(config) || store === 'memory') { const newStore = await this.createStore({ name, storeType: store, ...config }); return this.newCache({ name, prefix, store: newStore }); } diff --git a/packages/core/server/src/locale/locale.ts b/packages/core/server/src/locale/locale.ts index 4ec017eb00..3f7a39f2c1 100644 --- a/packages/core/server/src/locale/locale.ts +++ b/packages/core/server/src/locale/locale.ts @@ -18,6 +18,7 @@ export interface ResourceStorer { getResources(lang: string): Promise<{ [ns: string]: Record; }>; + reset?: () => Promise; } export class Locale { @@ -45,14 +46,15 @@ export class Locale { name: 'locale', prefix: 'locale', store: 'memory', - max: 2000 }); await this.get(this.defaultLang); } async reload() { - await this.cache.reset(); + const storers = Array.from(this.resourceStorers.getValues()); + const promises = storers.map((storer) => storer.reset()); + await Promise.all([this.cache.reset(), ...promises]); } setLocaleFn(name: string, fn: (lang: string) => Promise) { diff --git a/packages/plugins/@nocobase/plugin-localization/src/server/__tests__/actions.test.ts b/packages/plugins/@nocobase/plugin-localization/src/server/__tests__/actions.test.ts index 5742932503..1f5b7fe04c 100644 --- a/packages/plugins/@nocobase/plugin-localization/src/server/__tests__/actions.test.ts +++ b/packages/plugins/@nocobase/plugin-localization/src/server/__tests__/actions.test.ts @@ -27,9 +27,11 @@ describe('actions', () => { }; beforeAll(async () => { + process.env.APP_ENV = 'production'; app = await createMockServer({ plugins: ['localization'], }); + await app.emitAsync('afterLoad'); db = app.db; repo = db.getRepository('localizationTexts'); agent = app.agent(); @@ -106,5 +108,28 @@ describe('actions', () => { expect(res.body.data[0].translation).toBeUndefined(); }); }); + + it('publish', async () => { + await repo.create({ + values: [ + { + module: 'test', + text: 'text', + translations: [ + { + locale: 'en-US', + translation: 'translation', + }, + ], + }, + ], + }); + const { resources } = await app.localeManager.get('en-US'); + expect(resources.test).toBeUndefined(); + await agent.resource('localization').publish(); + const { resources: resources2 } = await app.localeManager.get('en-US'); + expect(resources2.test).toBeDefined(); + expect(resources2.test.text).toBe('translation'); + }); }); }); diff --git a/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts index 5bfa09fcdb..b2d4fc722b 100644 --- a/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-localization/src/server/plugin.ts @@ -118,6 +118,7 @@ export class PluginLocalizationServer extends Plugin { this.app.localeManager.registerResourceStorer('plugin-localization', { getResources: (lang: string) => this.resources.getResources(lang), + reset: () => this.resources.reset(), }); } diff --git a/packages/plugins/@nocobase/plugin-localization/src/server/resources.ts b/packages/plugins/@nocobase/plugin-localization/src/server/resources.ts index bef751b3c5..949829454b 100644 --- a/packages/plugins/@nocobase/plugin-localization/src/server/resources.ts +++ b/packages/plugins/@nocobase/plugin-localization/src/server/resources.ts @@ -83,7 +83,7 @@ export default class Resources { await this.cache.set(`texts`, [...existTexts, ...newTexts]); } - async resetCache(locale: string) { - await this.cache.del(`translations:${locale}`); + async reset() { + await this.cache.reset(); } }