fix(locale): fix issues of syncing locale resources in cluster mode

This commit is contained in:
xilesun 2024-10-12 11:01:01 +08:00
parent 0e39579ae6
commit ff9a33b28a
2 changed files with 42 additions and 11 deletions

View File

@ -38,6 +38,13 @@ export class Locale {
this.app.log.debug('locale resource loaded', { submodule: 'locale', method: 'onAfterLoad' });
this.app.setMaintainingMessage('locale resource loaded');
});
this.app.syncMessageManager.subscribe('localeManager', async (message) => {
switch (message.type) {
case 'reload':
await this.cache.reset();
return;
}
});
}
async load() {
@ -52,6 +59,7 @@ export class Locale {
async reload() {
await this.cache.reset();
this.app.syncMessageManager.publish('localeManager', { type: 'reload' });
}
setLocaleFn(name: string, fn: (lang: string) => Promise<any>) {

View File

@ -9,9 +9,7 @@
import { Model } from '@nocobase/database';
import PluginUISchemaStorageServer from '@nocobase/plugin-ui-schema-storage';
import { InstallOptions, OFFICIAL_PLUGIN_PREFIX, Plugin } from '@nocobase/server';
import deepmerge from 'deepmerge';
import { resolve } from 'path';
import { InstallOptions, Plugin } from '@nocobase/server';
import localization from './actions/localization';
import localizationTexts from './actions/localizationTexts';
import Resources from './resources';
@ -46,8 +44,16 @@ export class PluginLocalizationServer extends Plugin {
text: title,
},
})
.then((res) => this.resources.updateCacheTexts([res]))
.catch((err) => {});
.then((res) => {
this.resources.updateCacheTexts([res]);
this.sendSyncMessage({
type: 'updateCacheTexts',
texts: [res],
});
})
.catch((err) => {
this.log.error(err);
});
});
}
@ -56,14 +62,12 @@ export class PluginLocalizationServer extends Plugin {
beforeLoad() {}
async load() {
await this.importCollections(resolve(__dirname, 'collections'));
this.app.resource({
this.app.resourceManager.define({
name: 'localizationTexts',
actions: localizationTexts,
});
this.app.resource({
this.app.resourceManager.define({
name: 'localization',
actions: localization,
});
@ -103,8 +107,19 @@ export class PluginLocalizationServer extends Plugin {
transaction: options?.transaction,
},
)
.then((newTexts) => this.resources.updateCacheTexts(newTexts, options?.transaction))
.catch((err) => {});
.then((newTexts) => {
this.resources.updateCacheTexts(newTexts, options?.transaction);
this.sendSyncMessage(
{
type: 'updateCacheTexts',
texts: newTexts,
},
{ transaction: options?.transaction },
);
})
.catch((err) => {
this.log.error(err);
});
});
const cache = await this.app.cacheManager.createCache({
@ -121,6 +136,14 @@ export class PluginLocalizationServer extends Plugin {
});
}
async handleSyncMessage(message: any): Promise<void> {
switch (message.type) {
case 'updateCacheTexts':
await this.resources.updateCacheTexts(message.texts);
return;
}
}
async install(options?: InstallOptions) {}
async afterEnable() {}