Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated lang files contains an invalid JSON structure. #68

Open
azuddin opened this issue Jan 14, 2018 · 3 comments
Open

Generated lang files contains an invalid JSON structure. #68

azuddin opened this issue Jan 14, 2018 · 3 comments

Comments

@azuddin
Copy link

azuddin commented Jan 14, 2018

generated php code instead of json.
screen shot 2018-01-14 at 11 25 57 pm

@mserralta
Copy link

Same here....

@knno
Copy link

knno commented Feb 27, 2018

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants