Skip to content

Commit 80459f4

Browse files
authored
Merge pull request #88 from Majkie/main
fix: Nested translations aren't translated
2 parents 8536785 + a4d7df1 commit 80459f4

File tree

6 files changed

+58
-25
lines changed

6 files changed

+58
-25
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ export class I18n {
337337
* Get the translation for the given key and watch for any changes.
338338
*/
339339
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
340+
key = key.replace(/\//g, '.')
341+
340342
if (!this.activeMessages[key]) {
341343
const hasChildItems = this.activeMessages[`${key}.0`] !== undefined
342344

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+
];
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' => 'É elétrico?',
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

test/translate.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,10 @@ it('translates a possible nested item, and if not exists check on the root level
191191
await global.mountPlugin(undefined, 'pt');
192192
expect(trans('foo.bar')).toBe('baz');
193193
});
194+
195+
it('translates a nested file item while using "/" and "." at the same time as a delimiter', async () => {
196+
await global.mountPlugin()
197+
198+
expect(trans('nested/cars/car.is_electric')).toBe('É elétrico?');
199+
expect(trans('nested/cars/car.foo.level1.level2')).toBe('barpt');
200+
})

0 commit comments

Comments
 (0)