Skip to content

Commit

Permalink
fix: Ignore null and empty array php translations
Browse files Browse the repository at this point in the history
  • Loading branch information
xiCO2k committed Jan 5, 2024
1 parent ce503bf commit c2be6c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ const parseItem = (expr) => {
return expr.value
}

if (expr.kind === 'nullkeyword') {
return null;
}

if (expr.kind === 'array') {
let items = expr.items.map((item) => parseItem(item))

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

if (items === null) {
return data;
}

Object.entries(items).forEach(([key, value]) => {
if (typeof value === 'string') {
data[context + key] = value
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/lang/en/ignore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'empty_array' => [],
'null' => null,
];
7 changes: 7 additions & 0 deletions test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ it('transforms simple index array to .json', () => {
expect(lang['arr.1']).toBe('bar');
});

it('ignores empty `array` or `null` translations', () => {
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/ignore.php').toString());

expect(lang['empty_array']).toBe(undefined);
expect(lang['null']).toBe(undefined);
});

it('checks if there is .php translations', () => {
expect(hasPhpTranslations(__dirname + '/fixtures/lang/')).toBe(true);
expect(hasPhpTranslations(__dirname + '/fixtures/wronglangfolder/')).toBe(false);
Expand Down

0 comments on commit c2be6c8

Please sign in to comment.