Skip to content

Commit 3b836c9

Browse files
committed
fix: Nested translations aren't translated
1 parent 8536785 commit 3b836c9

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

src/loader.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,9 @@ export const parseAll = (folderPath: string): { name: string; path: string }[] =
3535

3636
const data = []
3737
for (const folder of folders) {
38-
const lang = {}
38+
const langFolderPath = folderPath + path.sep + folder
3939

40-
fs.readdirSync(folderPath + path.sep + folder)
41-
.sort()
42-
.forEach((langFolderItem) => {
43-
const langFolderPath = folderPath + path.sep + folder
44-
const langFolderItemPath = langFolderPath + path.sep + langFolderItem
45-
46-
if (fs.statSync(langFolderItemPath).isDirectory()) {
47-
// Lang sub folder
48-
const subFolderFileKey = langFolderItem.replace(/\.\w+$/, '')
49-
lang[subFolderFileKey] = {}
50-
51-
fs.readdirSync(langFolderItemPath)
52-
.filter((file) => !fs.statSync(langFolderItemPath + path.sep + file).isDirectory())
53-
.sort()
54-
.forEach((file) => {
55-
lang[subFolderFileKey][file.replace(/\.\w+$/, '')] = parse(
56-
fs.readFileSync(langFolderItemPath + path.sep + file).toString()
57-
)
58-
})
59-
} else {
60-
// Lang file
61-
lang[langFolderItem.replace(/\.\w+$/, '')] = parse(fs.readFileSync(langFolderItemPath).toString())
62-
}
63-
})
40+
const lang = readThroughDir(langFolderPath)
6441

6542
data.push({
6643
folder,
@@ -147,3 +124,22 @@ export const reset = (folderPath) => {
147124
fs.unlinkSync(folderPath + file)
148125
})
149126
}
127+
128+
export const readThroughDir = (dir) => {
129+
const data = {}
130+
131+
fs.readdirSync(dir)
132+
.forEach((file) => {
133+
const absoluteFile = dir + path.sep + file
134+
135+
if (fs.statSync(absoluteFile).isDirectory()) {
136+
const subFolderFileKey = file.replace(/\.\w+$/, '')
137+
138+
data[subFolderFileKey] = readThroughDir(absoluteFile)
139+
} else {
140+
data[file.replace(/\.\w+$/, '')] = parse(fs.readFileSync(absoluteFile).toString())
141+
}
142+
})
143+
144+
return data
145+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'is_electric' => 'Electric',
5+
'foo' => [
6+
'level1' => [
7+
'level2' => 'barpt',
8+
]
9+
]
10+
];

test/loader.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ it('includes .php lang file in subdirectory in .json', () => {
3030
expect(langEn['domain.car.foo.level1.level2']).toBe('barpt');
3131
});
3232

33+
it('includes .php lang file in nested subdirectory in .json', () => {
34+
const files = parseAll(__dirname + '/fixtures/lang/');
35+
const langEn = JSON.parse(fs.readFileSync(files[0].path).toString())
36+
37+
expect(langEn['nested.cars.car.is_electric']).toBe('Electric');
38+
expect(langEn['nested.cars.car.foo.level1.level2']).toBe('barpt');
39+
})
40+
3341
it('transforms .php lang to .json', () => {
3442
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/auth.php').toString());
3543

0 commit comments

Comments
 (0)