Skip to content

Commit b7e1fd4

Browse files
committed
handle malformed json better
1 parent c7e41ff commit b7e1fd4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

php-templates/translations.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,18 @@ protected function fromFile($file, $path, $namespace)
100100
protected function linesFromJsonFile($file)
101101
{
102102
$contents = file_get_contents($file);
103+
104+
try {
105+
$json = json_decode($contents, true) ?? [];
106+
} catch (Exception $e) {
107+
return [[], []];
108+
}
109+
110+
if (count($json) === 0) {
111+
return [[], []];
112+
}
113+
103114
$lines = explode(PHP_EOL, $contents);
104-
$json = json_decode($contents, true);
105115
$encoded = array_map(
106116
fn($k) => [json_encode($k), $k],
107117
array_keys($json),

src/templates/translations.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,18 @@ $translator = new class
100100
protected function linesFromJsonFile($file)
101101
{
102102
$contents = file_get_contents($file);
103+
104+
try {
105+
$json = json_decode($contents, true) ?? [];
106+
} catch (Exception $e) {
107+
return [[], []];
108+
}
109+
110+
if (count($json) === 0) {
111+
return [[], []];
112+
}
113+
103114
$lines = explode(PHP_EOL, $contents);
104-
$json = json_decode($contents, true);
105115
$encoded = array_map(
106116
fn($k) => [json_encode($k), $k],
107117
array_keys($json),

0 commit comments

Comments
 (0)