I have generated the default Laravel auth module.
Everywhere in the blades 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 the 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 theresources/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 any default? Where does it sit? Where was it set?
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.
(1)
There's a 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') }}
(2)
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>
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.');
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