potsky / laravel-localization-helpers

🎌 Artisan commands to generate and update lang files automatically
GNU General Public License v3.0
187 stars 38 forks source link

Generated lang files contains an invalid JSON structure. #68

Open azuddin opened 6 years ago

azuddin commented 6 years ago

generated php code instead of json.

screen shot 2018-01-14 at 11 25 57 pm
mserralta commented 6 years ago

Same here....

knno commented 6 years ago

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:

  1. 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.
  2. Run localization:missing.
  3. Backup the "en.json.php" file (PHP syntax) to "en.json.backup.php".
  4. Rename the generated "en.json" (PHP syntax) to "en.json.php".
  5. You can safely edit the new file "en.json.php".
  6. 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)
  7. 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!

harimau99 commented 4 years ago

i also received the same error. would be solution given by @knno is the solution ?