Skip to content

Commit f3c9f17

Browse files
authored
Support for subdirectories in a language (#44)
1 parent cb1cd39 commit f3c9f17

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

src/loader.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,28 @@ export const parseAll = (folderPath: string): { name: string; path: string }[] =
3838
const lang = {}
3939

4040
fs.readdirSync(folderPath + path.sep + folder)
41-
.filter((file) => !fs.statSync(folderPath + path.sep + folder + path.sep + file).isDirectory())
4241
.sort()
43-
.forEach((file) => {
44-
lang[file.replace(/\.\w+$/, '')] = parse(
45-
fs.readFileSync(folderPath + path.sep + folder + path.sep + file).toString()
46-
)
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+
}
4763
})
4864

4965
data.push({

test/fixtures/lang/en/domain/car.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'car' => 'Car|Cars',
5+
'is_electric' => 'Electric',
6+
'charge_speed' => 'Charge speed',
7+
'foo' => [
8+
'level1' => [
9+
'level2' => 'barpt'
10+
]
11+
],
12+
];

test/fixtures/lang/en/domain/user.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'user' => 'User|Users',
5+
'first_name' => 'First name',
6+
'sub_dir_support_is_amazing' => 'Subdirectory support is amazing',
7+
];

test/loader.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ it('creates a file for each lang', () => {
2121
expect(langPt['auth.foo.level1.level2']).toBe('barpt');
2222
});
2323

24+
it('includes .php lang file in subdirectory in .json', () => {
25+
const files = parseAll(__dirname + '/fixtures/lang/');
26+
const langEn = JSON.parse(fs.readFileSync(files[0].path).toString());
27+
28+
expect(langEn['domain.user.sub_dir_support_is_amazing']).toBe('Subdirectory support is amazing');
29+
expect(langEn['domain.car.is_electric']).toBe('Electric');
30+
expect(langEn['domain.car.foo.level1.level2']).toBe('barpt');
31+
});
32+
2433
it('transforms .php lang to .json', () => {
2534
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/auth.php').toString());
2635

0 commit comments

Comments
 (0)