fix(l10n): issue of publishing locale resources (#5416)
Some checks are pending
Build docker image / build-and-push (push) Waiting to run
Build pro image / build-and-push (push) Waiting to run
E2E / Build (push) Waiting to run
E2E / Core and plugins (push) Blocked by required conditions
E2E / plugin-workflow (push) Blocked by required conditions
E2E / plugin-workflow-approval (push) Blocked by required conditions
E2E / plugin-data-source-main (push) Blocked by required conditions
E2E / Comment on PR (push) Blocked by required conditions
NocoBase backend test / sqlite-test (20, false) (push) Waiting to run
NocoBase backend test / sqlite-test (20, true) (push) Waiting to run
NocoBase backend test / postgres-test (public, 20, nocobase, false) (push) Waiting to run
NocoBase backend test / postgres-test (public, 20, nocobase, true) (push) Waiting to run
NocoBase backend test / postgres-test (public, 20, public, false) (push) Waiting to run
NocoBase backend test / postgres-test (public, 20, public, true) (push) Waiting to run
NocoBase backend test / postgres-test (user_schema, 20, nocobase, false) (push) Waiting to run
NocoBase backend test / postgres-test (user_schema, 20, nocobase, true) (push) Waiting to run
NocoBase backend test / postgres-test (user_schema, 20, public, false) (push) Waiting to run
NocoBase backend test / postgres-test (user_schema, 20, public, true) (push) Waiting to run
NocoBase backend test / mysql-test (20, false) (push) Waiting to run
NocoBase backend test / mysql-test (20, true) (push) Waiting to run
NocoBase backend test / mariadb-test (20, false) (push) Waiting to run
NocoBase backend test / mariadb-test (20, true) (push) Waiting to run
NocoBase frontEnd test / frontend-test (18) (push) Waiting to run
Test on Windows / build (push) Waiting to run

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