I read the documentation for translation strings on Retrieving Translation Strings but somehow I don't understand how to apply it.
Let's say I would like to render in a view posts.index
the message "I like programming" in English, German ("Ich mag Programmieren") or Spanish ("Me encanta programar"), depending on the localization set by App::setLocale().
How would the translation files look like and how would I setup the view?
I finally understood the concept. Within resources/lang
you create a translation JSON file for every language, e. g.:
/resources
/lang
/de.json
/es.json
There is no need to create an en.json
file as en
will be the default language if you don't set a language with App::setLocale()
.
de.json:
{
"I love programming.": "Ich mag programmieren."
}
es.json:
{
"I love programming.": "Me encanta programar."
}
Next, you set in your controller the language via App::setLocale();
and now comes the fun part. In the view, you only include the key of the JSON, e. g.
{{ __('I love programming.') }}
and depending on your localization, Laravel will automatically load the correct translation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With