Skip to content

Commit

Permalink
1.0.2 - add scoped translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Elschnagoo committed Sep 30, 2024
1 parent 19820ab commit 6b1aab7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grandlinex/bundle-multilang",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"type": "module",
"exports": {
Expand Down
44 changes: 31 additions & 13 deletions src/client/LangClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,35 @@ export default class LangClient extends CoreClient<
return !!(await this.getModule().getDb().lang.getObjById(code));
}

async getLang(code: string): Promise<LangData | undefined> {
async getLang(
code: string,
scopes?: string[],
): Promise<LangData | undefined> {
const db = this.getModule().getDb();
const lang = await db.lang.getObjById(code);
if (!lang) {
return undefined;
}
let d: Translation[] = [];
if (!scopes) {
d = await db.translations.getObjList({
search: {
t_lang: lang.e_id,
},
});
} else {
for (const scope of scopes) {
d = d.concat(
await db.translations.getObjList({
search: {
t_lang: lang.e_id,
scope,
},
}),
);
}
}

const d = await db.translations.getObjList({
search: {
t_lang: lang.e_id,
},
});
const nlang: LangData = {
label: lang.label,
code: lang.e_id,
Expand All @@ -47,16 +64,16 @@ export default class LangClient extends CoreClient<
return nlang;
}

async getDefault(): Promise<LangData | undefined> {
return this.getLang(await this.getDbLang());
async getDefault(scopes?: string[]): Promise<LangData | undefined> {
return this.getLang(await this.getDbLang(), scopes);
}

async getCur(): Promise<LangData | null> {
async getCur(scopes?: string[]): Promise<LangData | null> {
const db = this.getModule().getDb();
if (await db.configExist(LangClient.DEFAULT_LANG_DB_KEY)) {
const code = await db.getConfig(LangClient.DEFAULT_LANG_DB_KEY);
if (code && (await this.hasLang(code.c_value))) {
const lang = await this.getLang(code.c_value);
const lang = await this.getLang(code.c_value, scopes);
if (lang) {
return lang;
}
Expand All @@ -65,8 +82,8 @@ export default class LangClient extends CoreClient<
return (await this.getDefault()) || null;
}

async getCurTranslator(): Promise<GLang> {
return new GLang(await this.getCur(), this);
async getCurTranslator(scopes?: string[]): Promise<GLang> {
return new GLang(await this.getCur(scopes), this);
}

async getLangList(): Promise<{ code: string; label: string }[]> {
Expand All @@ -78,7 +95,7 @@ export default class LangClient extends CoreClient<
);
}

async loadLangFromFolder(path: string) {
async loadLangFromFolder(path: string, scope = 'default'): Promise<void> {
const info = fs.statSync(path);
const db = this.getModule().getDb();
this.debug(path);
Expand Down Expand Up @@ -108,6 +125,7 @@ export default class LangClient extends CoreClient<
await db.translations.createObject(
new Translation({
key,
scope,
value: json[key],
t_lang: lang.e_id,
}),
Expand Down
6 changes: 6 additions & 0 deletions src/db/entity/Translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default class Translation extends CoreEntity {
})
value: string;

@Column({
dataType: 'string',
})
scope: string;

@EntityColumn(new Language())
t_lang: string;

Expand All @@ -27,5 +32,6 @@ export default class Translation extends CoreEntity {
this.key = props?.key || '';
this.value = props?.value || '';
this.t_lang = props?.t_lang || '';
this.scope = props?.scope || 'default';
}
}
14 changes: 14 additions & 0 deletions src/tests/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ describe('TestDatabase', () => {
expect(t.get("test.key.second")).toBe("helloWorldEn2")
expect(t.get("test.key.third")).toBe("test.key.third")
})
test("translator-default",async ()=>{
const client = mod.getClient() as LangClient;
const t= await client.getCurTranslator(["default"]);
expect(t.get("test.key.first")).toBe("helloWorldEn")
expect(t.get("test.key.second")).toBe("helloWorldEn2")
expect(t.get("test.key.third")).toBe("test.key.third")
})
test("translator-missing",async ()=>{
const client = mod.getClient() as LangClient;
const t= await client.getCurTranslator(["missing"]);
expect(t.get("test.key.first")).toBe("test.key.first")
expect(t.get("test.key.second")).toBe("test.key.second")
expect(t.get("test.key.third")).toBe("test.key.third")
})
test("translator clear",async ()=>{
const client = mod.getClient() as LangClient;
const t= await client.getCurTranslator();
Expand Down

0 comments on commit 6b1aab7

Please sign in to comment.