Skip to content

Commit 3836a56

Browse files
committed
Improvements on docs and tests.
1 parent 956b47d commit 3836a56

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

loader/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
12
const phpArrayParser = require('./parser/php-array')
2-
const jsonParser = require('json-loader');
3+
const jsonParser = require('json-loader')
34

5+
/**
6+
* The Laravel Localization loader.
7+
* @param {string} source The source contents.
8+
* @return {string} The parsed contents.
9+
*/
410
const laravelLocalizationLoader = function(source) {
511
const isPHP = ~source.indexOf('<?php')
12+
let parsed;
13+
614
if (isPHP) {
7-
return phpArrayParser(source)
15+
parsed = phpArrayParser(source)
816
} else {
9-
return jsonParser(source)
17+
parsed = jsonParser(source)
1018
}
19+
20+
return parsed
1121
}
1222

1323
module.exports = laravelLocalizationLoader

loader/parser/php-array.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,32 @@ const parser = new engine({
44
ast: { withPositions: true },
55
})
66

7+
/**
8+
* The PHP array parser.
9+
* @param {string} source The PHP source contents.
10+
* @return {string} The parsed contents.
11+
*/
712
const phpArrayParser = function (source) {
813
const ast = parser.parseCode(source)
914
const ret = ast.children.find((child) => child.kind === 'return')
1015
const parsed = parse(ret.expr)
11-
.reduce((acc, val) => Object.assign({}, acc, val), {})
1216
return `module.exports = ${JSON.stringify(parsed)};`
1317
}
1418

19+
/**
20+
* Parse a PHP expression to JavaScript
21+
* @param {Object} expr The AST PHP expression.
22+
* @return {*} A JavaScript object or value.
23+
*/
1524
function parse(expr) {
1625
switch(expr.kind) {
1726
case 'array':
18-
return expr.items.map(parse)
27+
const isKeyed = expr.items.every((item) => item.key !== null)
28+
let items = expr.items.map(parse)
29+
if (isKeyed) {
30+
items = items.reduce((a, v) => Object.assign({}, a, v), {})
31+
}
32+
return items
1933
case 'entry':
2034
return { [parse(expr.key)]: parse(expr.value) }
2135
case 'string':

tests/fixtures/resources/lang/en/messages.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'string' => 'Rubens',
55
'number' => 123,
6+
'parent' => [
7+
'child' => 'Mariuzzo',
8+
],
69
];

tests/loader.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ const path = require('path')
22
const webpack = require('webpack')
33
const merge = require('webpack-merge')
44

5-
test('should load PHP array translation file', () => {
5+
test('should load PHP Laravel translation file', () => {
66
return runWebpack({
77
entry: path.join(__dirname, './fixtures/resources/lang/en/messages.php'),
88
})
99
.then((result) => {
1010
expect(result).toBeDefined()
1111
expect(result).toHaveProperty('string', 'Rubens')
1212
expect(result).toHaveProperty('number', 123)
13+
expect(result).toHaveProperty('parent.child', 'Mariuzzo')
1314
})
1415
})
1516

16-
function runWebpack(config) {
17+
function runWebpack(config, legacy) {
1718
return new Promise((resolve, reject) => {
1819
const webpackConfig = merge({
1920
output: {
@@ -26,6 +27,7 @@ function runWebpack(config) {
2627
{
2728
test: /resources\/lang.+\.php$/,
2829
loader: 'laravel-localization-loader',
30+
options: { legacy },
2931
}
3032
]
3133
},

tests/output/translation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ return /******/ (function(modules) { // webpackBootstrap
8080
/* 0 */
8181
/***/ (function(module, exports) {
8282

83-
module.exports = {"string":"Rubens","number":123};
83+
module.exports = {"string":"Rubens","number":123,"parent":{"child":"Mariuzzo"}};
8484

8585
/***/ })
8686
/******/ ]);

0 commit comments

Comments
 (0)