fix(l10n): issue of publishing locale resources (#5416)

This commit is contained in:
YANG QIA 2024-10-14 23:25:19 +08:00 committed by GitHub
parent c4ee286f12
commit 3e8326f941
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 5 deletions

View File

@ -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 });
}

View File

@ -18,6 +18,7 @@ export interface ResourceStorer {
getResources(lang: string): Promise<{
[ns: string]: Record<string, string>;
}>;
reset?: () => Promise<void>;
}
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<any>) {

View File

@ -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');
});
});
});

View File

@ -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(),
});
}

View File

@ -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();
}
}