Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit 8fe66f6

Browse files
committed
Added support for nested folders in translations, closes #12
1 parent aa70899 commit 8fe66f6

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kirschbaum-development/laravel-translations-loader",
3-
"version": "0.4.4",
3+
"version": "0.5.0",
44
"main": "all.js",
55
"author": {
66
"name": "Kirschbaum Development"
@@ -31,6 +31,7 @@
3131
}
3232
],
3333
"dependencies": {
34+
"klaw-sync": "^6.0.0",
3435
"loader-utils": "^1.1.0",
3536
"lodash": "^4.17.11",
3637
"php-array-parser": "^1.0.1"

php-loader.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,39 @@ const fs = require('fs');
33
const _ = require('lodash');
44
const loaderUtils = require('loader-utils');
55
const phpArrayParser = require('php-array-parser');
6+
const klaw = require('klaw-sync');
67

78
const phpLoader = {
89
execute (baseDirectory, options) {
910
var bundle = {};
10-
1111
var directories;
1212

13+
if (! path.isAbsolute(baseDirectory)) {
14+
baseDirectory = path.resolve(baseDirectory);
15+
}
16+
1317
directories = fs.readdirSync(baseDirectory).filter((file) => {
1418
return fs.statSync(path.join(baseDirectory, file)).isDirectory();
1519
});
1620

17-
1821
directories.forEach((directory) => {
1922
var langDirectory = path.join(baseDirectory, directory);
20-
var files = files = fs.readdirSync(langDirectory).filter(function (file) {
21-
return path.extname(file) === '.php';
22-
});
2323

24-
files.forEach((file) => {
25-
var content = fs.readFileSync(path.join(langDirectory, file), 'utf8');
24+
klaw(langDirectory, {
25+
nodir: true
26+
}).filter((file) => {
27+
return path.extname(file.path) === '.php';
28+
}).forEach((file) => {
29+
var filename = file.path.split(langDirectory + '/')[1];
30+
var content = fs.readFileSync(path.join(langDirectory, filename), 'utf8');
2631

2732
// Remove left part of return expression and any ending `?>`.
2833
const ret = content.indexOf('return') + 'return'.length
2934
content = content.substr(ret)
3035
content = content.replace(/\?>\s*$/, '_')
3136

3237
let langObject = {}
33-
langObject[file.replace('.php', '')] = phpArrayParser.parse(content)
38+
langObject[filename.replace('.php', '')] = phpArrayParser.parse(content)
3439

3540
if (typeof options.namespace !== 'undefined') {
3641
if (typeof bundle[directory] === 'undefined') {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
5+
'home' => 'Home',
6+
'about' => 'About',
7+
8+
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
5+
'required' => 'This field is required',
6+
7+
];

test/php.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
let assert = require('assert')
2+
let path = require('path')
23
let phpLoader = require('./../php-loader')
34

45
describe('it should load php language files', function () {
@@ -48,4 +49,18 @@ describe('it should load php language files', function () {
4849
}
4950
});
5051
});
52+
53+
it('should be able to load nested folders', function () {
54+
let content = phpLoader.execute('./test/fixtures/php-with-nested-folders', {});
55+
56+
assert.deepEqual(content.en, {
57+
validation: {
58+
required: 'This field is required',
59+
},
60+
"menu/main": {
61+
home: 'Home',
62+
about: 'About'
63+
}
64+
});
65+
});
5166
});

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ [email protected]:
7474
once "^1.3.0"
7575
path-is-absolute "^1.0.0"
7676

77+
graceful-fs@^4.1.11:
78+
version "4.1.15"
79+
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
80+
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==
81+
7782
7883
version "1.10.5"
7984
resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.5.tgz#f2735dc2283674fa67478b10181059355c369e5e"
@@ -107,6 +112,13 @@ json5@^0.5.0:
107112
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
108113
integrity sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=
109114

115+
klaw-sync@^6.0.0:
116+
version "6.0.0"
117+
resolved "https://registry.yarnpkg.com/klaw-sync/-/klaw-sync-6.0.0.tgz#1fd2cfd56ebb6250181114f0a581167099c2b28c"
118+
integrity sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==
119+
dependencies:
120+
graceful-fs "^4.1.11"
121+
110122
loader-utils@^1.1.0:
111123
version "1.1.0"
112124
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-1.1.0.tgz#c98aef488bcceda2ffb5e2de646d6a754429f5cd"

0 commit comments

Comments
 (0)