Not sure if you’ve noticed, but Laravel 5.6 make:auth is generating Blade files with translation-ready texts, so instead of just “Login” you will see {{ __(‘Login’) }}. How does it work and what’s the reason?
First, if you don’t need your project to be multi-language, just leave it as it is, it will just work “by magic”, converting whatever string inside __() into the same string itself.
Now, let’s dig a little deeper. This __() function actually isn’t new, it was released in Laravel 5.4 to support JSON-based translations.
We’re probably all used to have language files in resources/lang/en/ folder, creating files for each substantial section of your app. But the problem is that it’s hard to maintain and come up with new array keys like ‘error_someone_can_hack_into_the_system’, write them all with underscores and also prevent their duplications. So there’s a (maybe) better way – to have one JSON file with keys being any actual texts in main language.
Example from official documentation – resources/lang/es.json file for Spanish:
{ "I love programming.": "Me encanta programar." }
This allows you to call __(‘I love programming’) wherever you need (Controllers, Blade files etc.).
The best part is that you can use this function in the same way as before with @lang or trans() functions. In case that the key is not present in JSON (or JSON file missing at all), it will still fallback to the default language functionality. So you can do both:
echo __('messages.welcome'); echo __('I love programming.');
Or, in Blade:
{{ __('messages.welcome') }}
Read more about localization and translations in the official documentation.
You may also be interested in our article 10 Best Laravel packages for multi-language projects.
hi .
Just need to be renamed php to json ?
No, the structure should be different, too, it needs to be a real JSON structure, not PHP array.
so change php to json and change array syntax to json syntax ?
Yes.
[
‘name’ => ‘name’
]
to
{
name: ‘name’
}
hello again .
سلام دوستان ، وقت بخیر
i created json file on resources/lang folder : fa.json
{
“test” : ” hi hi hi”,
“upload” : {
“success” : ” upload successfully.”,
},
}
now code worked :
echo __(‘test’)
but multi index ( children ) not work :
echo __(‘upload.success’)
where is the problem from?
remove the “,” at end of >> “upload” : {
“success” : ” upload successfully.”,
},
because this coma will cause error in json_decode
Thanks for your article and its very informative in sense of general and fast development.
You have showed an example of 2 languages English & Spanish, my question is about how to write json code in lang file for 3 or 3+ languages. Thank You
Just create json file for each language, so resources/lang/es.json, resources/lang/en.json, resources/lang/de.json etc.
Okey Thanks!
I will be more thankful to you if you make a tutorial on how to select specific columns from 1 table that is the parent and also the tables with with() or has() methods, because these things are very hard to find on documentation. thnx
sir we just create json file inside “resources/lang/en.json”
json code….
{
“I love programming.”: “Me encanta programar.”,
“programming” : “Me encanta programar.”,
“developer” : “Me encanta developer.”
}
inside we.php file
Route::get(‘app/{local?}’,function($local = null){
App::setLocale($local);
return view(‘testing.app’);
});
inside testing.app file
{{ __(‘developer’) }}
result –
developer
why it is not working
Are you launching URL /app/en or something else?
Hi. Strangely, the php files in the lang folder don’t work for me while the en.json works really good with the double score helper. Any ideas why the php files like: lang\fa\posts.php don’t work for me? Thanks.