Skip to content

Commit 6358195

Browse files
committed
IDVA6-1468: Improve language and namespace loading in i18nCh utility
- Enhance type safety by specifying `lng` and `ns` as strings in loadPath - Refactor loadAllNamespaces to use a more functional approach - Consolidate namespace loading logic into loadNamespacesFromFolder - Simplify getLanguageFolders using array flattening - Improve code readability and maintainability - Ensure consistent handling of both project and node_modules folders
1 parent 44655a9 commit 6358195

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/utils/i18nCh.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class i18nCh {
3737
fallbackLng: "en",
3838
preload: this.getLanguageFolders(),
3939
backend: {
40-
loadPath: (lng: any, ns: any) => {
40+
loadPath: (lng: string, ns: string) => {
4141
const projectPath = path.join(this.localesFolder, `${lng}/${ns}.json`);
4242
const nodePath = path.join(this.nodeModulesFolder, `${lng}/${ns}.json`);
4343
return fs.existsSync(projectPath) ? projectPath : nodePath;
@@ -61,17 +61,16 @@ export default class i18nCh {
6161
//_______________________________________________________________________________________________
6262
// load all the file names (without extension: ".json") present in 'localesFolder'
6363
private loadAllNamespaces(): string[] {
64-
if (!this.localesFolder) {
65-
return [];
66-
}
67-
68-
const projectRootNamespaces = this.loadNamespacesFromPath(path.join(this.localesFolder, "en"));
69-
const nodeModulesNamespaces = this.loadNamespacesFromPath(path.join(this.nodeModulesFolder, "en"));
64+
const allNamespaces = [
65+
this.loadNamespacesFromFolder(this.localesFolder),
66+
this.loadNamespacesFromFolder(this.nodeModulesFolder)
67+
];
7068

71-
return [...new Set([...projectRootNamespaces, ...nodeModulesNamespaces])];
69+
return [...new Set(allNamespaces.flat())];
7270
}
7371

74-
private loadNamespacesFromPath(folderPath: string): string[] {
72+
private loadNamespacesFromFolder(baseFolder: string): string[] {
73+
const folderPath = path.join(baseFolder, "en");
7574
try {
7675
return fs.readdirSync(folderPath)
7776
.filter(file => path.extname(file) === ".json")
@@ -83,9 +82,11 @@ export default class i18nCh {
8382
}
8483

8584
private getLanguageFolders(): string[] {
86-
const projectLangs = this.getFoldersFromPath(this.localesFolder);
87-
const nodeLangs = this.getFoldersFromPath(this.nodeModulesFolder);
88-
return [...new Set([...projectLangs, ...nodeLangs])];
85+
const allFolders = [
86+
this.getFoldersFromPath(this.localesFolder),
87+
this.getFoldersFromPath(this.nodeModulesFolder)
88+
];
89+
return [...new Set(allFolders.flat())];
8990
}
9091

9192
private getFoldersFromPath(folderPath: string): string[] {

0 commit comments

Comments
 (0)