I have generated the default Laravel auth module.
Everywhere in the blades of of the module I see Double Underscore __
function assuming that translation is almost there.
for example
<li>
<a class="nav-link" href="{{ route('login') }}">
{{ __('Login') }}
</a>
</li>
My question: Where is a translation file? Where should I put it if I create one?
I mean if I go to the Laravel documentation site there are examples like this
echo __('messages.welcome');
with explanations
For example, let's retrieve the welcome
translation string from the resources/lang/messages.php
language file:
BUT in my example above there is no file name specified. It is only text:
__('Login')
Question is: What is used for the language file if no file specified? Is there a default? Where does it sit? Where was it set?
You should simply add the translation file by yourself. __('Login')
means the string is translation ready and in the translation file you can point to it.
Edit:
All the language translation files in Laravel should be stored in PROJECT_DIRECTORY/resources/lang
. When you make an Auth with artisan, it automatically creates it. But if you can't find it, then create manually.
For Example:
Create a file named auth.php
in PROJECT_DIRECTORY/resources/lang
directory. then put a simple php array like this on it:
<?php
return [
/*
Translations go here...
*/
];`
Then add your translate strings to it:
<?php
return [
'Login' => 'Welcome to Login Page!',
'Logout' => 'You are logged out!',
];`
Now in the blade template simply do this:
<li>
<a class="nav-link" href="{{ route('login') }}">
{{ __('auth.Login') }}
</a>
</li>
But there's a second way to using translation strings as keys by the docs. In this method you can create a JSON file in PROJECT_DIRECTORY/resources/lang
with the name of your local, for example for Spanish name it es.json
or German de.json
, it depends on your local name.
Now create a JSON object and put the translations with the string name you used in your blade:
{
"Login": "Welcome to Login Page!",
"Logout" => "You are logged out!",
}
Then use the PHP double underscores method to call your translations in blades:
{{ __('Login') }}
I hope the explanations are more clear. Feel free to ask any question.
Laravel Docs
Have an instruction about the json file. Yes it is not php, but json file. Example would be:
resources/lang/es.json
content
{
"I love programming.": "Me encanta programar."
}
Usage
echo __('I love programming.');