laravel-localization-helpers
laravel-localization-helpers copied to clipboard
Generated lang files contains an invalid JSON structure.
generated php code instead of json.
Same here....
You can see issue #56
Hey, you can workaround this by requiring the php file, and overwriting a JSON encoded string of the array parsed.
Thanks alot to the author of this package. However, this is my Temporary solution. Consider this:
- Rename a previous "en.json.php" file to "en.json" (which is actually PHP syntax). Then the package will add missing strings, deprecated strings, etc... normally.
- Run
localization:missing
. - Backup the "en.json.php" file (PHP syntax) to "en.json.backup.php".
- Rename the generated "en.json" (PHP syntax) to "en.json.php".
- You can safely edit the new file "en.json.php".
- When finishing the manual editing, you can parse array by including the file "en.json.php" and write output as json to "en.json". (consider it compiling :D)
- When you want to edit translations, start from number 1.
// @Author Kenan Masri
Artisan::command('localize', function () {
$locales = config('app.locales');
foreach ($locales as $locale) {
if ($locale == "en") continue;
$this->comment(" - Localize: " . $locale);
$file = base_path('resources/lang/'.$locale.".json");
if (file_exists($file.".php")) {
$array = require ( $file.".php" );
if (file_exists($file)) {
unlink($file);
}
$fp = fopen($file, 'w');
fwrite($fp, json_encode($array, JSON_UNESCAPED_UNICODE) );
fclose($fp);
$this->comment(" parsed \"resources/lang/".$locale.".json.php\" -> \"" . $locale . ".json\"");
} else {
$this->comment(" Not found: \"resources/lang/".$locale.".json.php\"");
}
}
})->describe('Generates the JSON language after using :missing');
// @Author Kenan Masri
Artisan::command('localize:missing', function () {
$locales = config('app.locales');
// Put the files for potsky/laravel-localization-helpers
foreach ($locales as $locale) {
if ($locale == "en") continue;
$this->comment(" - Localize: " . $locale);
$file = base_path('resources/lang/'.$locale.".json");
if (file_exists( $file.".php" )) {
copy($file.".php", $file);
$this->comment(" copy \"resources/lang/".$locale.".json.php\" -> \"" . $locale . ".json\"");
}
}
$this->comment("");
$this->comment(" - Running localization:missing -n");
// Find missings
$this->call('localization:missing', ['-n']);
// Hide the files for potsky/laravel/localization-helpers
$done = [];
foreach ($locales as $locale) {
if ($locale == "en") continue;
$this->comment(" - Localize: " . $locale);
$file = base_path('resources/lang/'.$locale.".json");
if (file_exists( $file )) {
if (file_exists($file.".php")) {
rename($file.".php", $file.".backup.php");
}
rename($file, $file.".php");
$this->comment(" rename \"resources/lang/".$locale.".json\" -> \"" . $locale . ".json.php\"");
$done[] = $locale;
} else {
$this->comment(" Not found: \"resources/lang/".$locale.".json\"");
}
}
$this->comment("");
foreach ($done as $l) {
$this->comment("You can edit the \"".$l.".json.php\" file safely.");
}
$this->comment("");
$this->comment("Tip: Generate JSON with the artisan command: \"localize\".");
})->describe('Generates the PHP sources to edit missing translations');
@potsky Sir, I don't know if it's useful. I hope so maybe you can think of an idea like this. Thanks!
i also received the same error. would be solution given by @knno is the solution ?