70d5b9e44b
* feat: init localization-management * feat: resource api * Merge branch 'main' into T-62 * chore: change name * feat: basic feature * feat: support filter & sync * feat: support auto get texts afterSave * Merge branch 'main' into T-62 * chore: upgrade * fix: dependency * fix: field type * fix: type error * chore: remove some translations * feat: support extract text from menu * chore: cache text keys * chore: remove test key * fix: issue of extracting menu titles * feat: translate collections & fields name * fix: remove unique of text * refactor: improve cache * chore: remove listeners after disable * chore: translation * fix: lang switch bug * refactor: actions & filter * fix: translation * refactor: merge lang bundles at backend * fix: style & field name * fix: translate issues * fix: cache bug * fix: translation merge bug * fix: translate issues * fix: map translation * fix: translation issues * fix: card title bug * feat: cover mobile client tabbar * fix: menu title * refactor: add locale plugin * chore: merge locale plugin * fix: map translation * chore: remove no data * style: change button style * fix: sync bug * docs: add README * chore: change name --------- Co-authored-by: chenos <chenlinxh@gmail.com>
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { Cache, createCache } from '@nocobase/cache';
|
|
import { Database } from '@nocobase/database';
|
|
|
|
export default class Resources {
|
|
cache: Cache;
|
|
db: Database;
|
|
CACHE_KEY_PREFIX = 'localization:';
|
|
|
|
constructor(db: Database) {
|
|
this.cache = createCache();
|
|
this.db = db;
|
|
}
|
|
|
|
async getTexts() {
|
|
return await this.cache.wrap(`${this.CACHE_KEY_PREFIX}texts`, async () => {
|
|
return await this.db.getRepository('localizationTexts').find({
|
|
fields: ['id', 'module', 'text'],
|
|
raw: true,
|
|
});
|
|
});
|
|
}
|
|
|
|
async getTranslations(locale: string) {
|
|
return await this.cache.wrap(`${this.CACHE_KEY_PREFIX}translations:${locale}`, async () => {
|
|
return await this.db.getRepository('localizationTranslations').find({
|
|
fields: ['textId', 'translation'],
|
|
filter: { locale },
|
|
raw: true,
|
|
});
|
|
});
|
|
}
|
|
|
|
async getResources(locale: string) {
|
|
const [texts, translations] = await Promise.all([this.getTexts(), this.getTranslations(locale)]);
|
|
const resources = {};
|
|
const textsMap = texts.reduce((map, item) => {
|
|
map[item.id] = item;
|
|
return map;
|
|
}, {});
|
|
translations.forEach((item) => {
|
|
const text = textsMap[item.textId];
|
|
if (!text) {
|
|
return;
|
|
}
|
|
const module = text.module;
|
|
if (!resources[module]) {
|
|
resources[module] = {};
|
|
}
|
|
resources[module][text.text] = item.translation;
|
|
});
|
|
return resources;
|
|
}
|
|
|
|
async filterExists(texts: (string | { text: string })[]) {
|
|
let existTexts = await this.getTexts();
|
|
existTexts = existTexts.map((item) => item.text);
|
|
return texts.filter((text) => {
|
|
if (typeof text === 'string') {
|
|
return !existTexts.includes(text);
|
|
}
|
|
return !existTexts.includes(text.text);
|
|
});
|
|
}
|
|
|
|
async updateCacheTexts(texts: any[]) {
|
|
const newTexts = texts.map((text) => ({
|
|
id: text.id,
|
|
module: text.module,
|
|
text: text.text,
|
|
}));
|
|
const existTexts = await this.getTexts();
|
|
await this.cache.set(`${this.CACHE_KEY_PREFIX}texts`, [...existTexts, ...newTexts]);
|
|
}
|
|
|
|
async resetCache(locale: string) {
|
|
await this.cache.del(`${this.CACHE_KEY_PREFIX}translations:${locale}`);
|
|
}
|
|
}
|