-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.4] Add new JSON loader for translations #16424
Conversation
protected function loadJson($path, $locale) | ||
{ | ||
if ($this->files->exists($full = "{$path}/{$locale}.json")) { | ||
return (array) json_decode($this->files->get($full)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use json_decode($this->files->get($full), true)
instead? It'll give us an associative array [1] :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, changed :)
@themsaid: Maybe the |
Why not using something more fluent over one-letter functions? __() seems kind of noisy or at least use t(). |
I think this PR provides unnecessary mess and redundancy with old |
@kduma how? |
Actually “__” was the name of the function in Laravel 1.0 - 3.0. #trivia :) |
@themsaid now there are two ways of storing translations, the current way in PHP files ( |
@taylorotwell in my first PHP projects, I've also used |
Glad to see the Laravel translation system improving. Thanks. |
|
|
This, combined with the makeJsonReplacements method, looks odd. Doesn't trans() already support this parameter substitution? Why wasn't that code reused? |
@sisve Although your questions/doubts might be reasonable, it would be more constructive if you'd propose your improvements instead of questioning solutions of others. |
makeJsonReplacements was removed. |
@sisve we welcome your improvements to the loader setup via PR. |
So, what about piped plural strings? |
This PR introduces a new form of handling localization that's based on .json files instead of current PHP array files, the new format is similar to the popular gettext format except that it loads from json instead of po files.
This line will load the
resources/lang/en.json
and look for aProduct ":product" was saved successfully.
key inside it, if key wasn't found it just returns the given string.The replacement of parameters happens even if the key wasn't found so that you don't have to have translation files for your main language, using the keys only would work.
If no translation was found in the JSON files, Laravel will start looking for translation keys in the old PHP array files located in
resources/lang/en
, in case a match was found it's returned to the user. That way the method is fully backward compatible.The old translation methods will work as they used to
trans()
&trans_choice()
, these PR doesn't look into deprecating these methods, it just introduces an alternative approach.