Skip to content

Commit 7cd9a4a

Browse files
committed
feat: refactor i18n generation to use direct imports and simplify resource resolution
1 parent 019df4d commit 7cd9a4a

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

scripts/i18n-gen.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const OUTPUT = path.resolve('src/helpers/i18n/i18n.gen.ts');
66

77
const resources: Record<string, Record<string, string>> = {};
88
const namespaces = new Set<string>();
9+
const imports: string[] = [];
910

1011
for (const ns of fs.readdirSync(LOCALES_DIR)) {
1112
const nsPath = path.join(LOCALES_DIR, ns);
@@ -17,13 +18,17 @@ for (const ns of fs.readdirSync(LOCALES_DIR)) {
1718
if (!file.endsWith('.ts')) continue;
1819

1920
const lang = file.replace('.ts', '');
21+
const varName = `${lang.replace(/[^a-zA-Z0-9]/g, '')}_${ns}`;
2022
resources[lang] ??= {};
21-
resources[lang][ns] = `() => import('../../locales/${ns}/${lang}')`;
23+
resources[lang][ns] = varName;
24+
imports.push(`import ${varName} from '../../locales/${ns}/${lang}';`);
2225
}
2326
}
2427

2528
const content = `
2629
/* AUTO-GENERATED — DO NOT EDIT */
30+
${imports.join('\n')}
31+
2732
export const namespaces = ${JSON.stringify([...namespaces])}
2833
2934
export const resources = {
@@ -37,7 +42,8 @@ ${Object.entries(nss)
3742
},`,
3843
)
3944
.join('\n')}
45+
4046
}
4147
`;
4248

43-
fs.writeFileSync(OUTPUT, content.trim());
49+
fs.writeFileSync(OUTPUT, content.trim());

src/helpers/i18n/i18n.gen.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
/* AUTO-GENERATED — DO NOT EDIT */
2+
import viVN_common from '../../locales/common/vi-VN';
3+
import enUS_common from '../../locales/common/en-US';
4+
import viVN_exception from '../../locales/exception/vi-VN';
5+
import enUS_exception from '../../locales/exception/en-US';
6+
27
export const namespaces = ["common","exception"]
38

49
export const resources = {
510

6-
'en-US': {
7-
common: () => import('../../locales/common/en-US'),
8-
exception: () => import('../../locales/exception/en-US'),
11+
'vi-VN': {
12+
common: viVN_common,
13+
exception: viVN_exception,
914
},
1015

11-
'vi-VN': {
12-
common: () => import('../../locales/common/vi-VN'),
13-
exception: () => import('../../locales/exception/vi-VN'),
16+
'en-US': {
17+
common: enUS_common,
18+
exception: enUS_exception,
1419
},
20+
1521
}

src/helpers/i18n/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ for (const lang in resources) {
1212
resolved[l] = {};
1313
for (const ns in resources[l]) {
1414
const n = ns as Ns;
15-
resolved[l][n] = (await resources[l][n]()).default;
15+
resolved[l][n] = resources[l][n];
1616
}
1717
}
1818

0 commit comments

Comments
 (0)