Skip to content

Commit 35ad9aa

Browse files
authored
fix: Ignore null and empty array php translations (#159)
* fix: Ignore `null` and empty `array` `php` translations * style: fixes
1 parent ce503bf commit 35ad9aa

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/loader.ts

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ const parseItem = (expr) => {
7777
return expr.value
7878
}
7979

80+
if (expr.kind === 'nullkeyword') {
81+
return null
82+
}
83+
8084
if (expr.kind === 'array') {
8185
let items = expr.items.map((item) => parseItem(item))
8286

@@ -102,6 +106,10 @@ const convertToDotsSyntax = (list) => {
102106
const flatten = (items, context = '') => {
103107
const data = {}
104108

109+
if (items === null) {
110+
return data
111+
}
112+
105113
Object.entries(items).forEach(([key, value]) => {
106114
if (typeof value === 'string') {
107115
data[context + key] = value

test/fixtures/lang/en/ignore.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'empty_array' => [],
5+
'null' => null,
6+
];

test/loader.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ it('transforms simple index array to .json', () => {
9898
expect(lang['arr.1']).toBe('bar');
9999
});
100100

101+
it('ignores empty `array` or `null` translations', () => {
102+
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/ignore.php').toString());
103+
104+
expect(lang['empty_array']).toBe(undefined);
105+
expect(lang['null']).toBe(undefined);
106+
});
107+
101108
it('checks if there is .php translations', () => {
102109
expect(hasPhpTranslations(__dirname + '/fixtures/lang/')).toBe(true);
103110
expect(hasPhpTranslations(__dirname + '/fixtures/wronglangfolder/')).toBe(false);

0 commit comments

Comments
 (0)