Laravel default auth module translation
Asked Answered
B

3

8

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 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 any default? Where does it sit? Where was it set?

Boart answered 15/8, 2018 at 1:54 Comment(4)
A quick Google search yields two entries in the Laravel docs - 1 and 2. Does this answer your question?Sedulous
Can you tell, after using this links, where is a translation file for a construction like __('Login')? I cannot.Boart
The second link covers all of that.Sedulous
Sorry, It is not clear for me. It has similar example echo __('I love programming.'); but It does not say where the translation should come from.Boart
B
2

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.');
Boart answered 17/8, 2018 at 0:40 Comment(0)
F
14

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>
Flood answered 15/8, 2018 at 8:24 Comment(6)
This is a valid point of view. Have you fond it in documentation somewhere?Boart
Yes, according to the official documentation if you want to store all translations in one json file you can use translation strings without pointing to the file. You write __('Login') in your template file, then in the languages directory create one json file like en.json. Now you can write all translate strings like this { "Login": "This is the login page", "Logout": "You've logged out" }. You can see more details here.Flood
thanks for the comment but it is very different from the answer that you are commenting. What version of laravel is it?Boart
Sorry if there was some misunderstanding, This feature comes from version 5.4 and still works (to 5.6).Flood
@YevgeniyAfanasyev I describe the answer for you in full detail.Flood
Thank you for the answer! I was looking this for an hour online!Bananas
B
2

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.');
Boart answered 17/8, 2018 at 0:40 Comment(0)
G
2

It looks like there are no translation file for the default __('Login'), __('Register'), ... provided by laravel.

By default if no translation is found for __('foobar'), laravel just uses the string in the parentheses. So here, assuming there is no translation file, __('Login') is expanded to 'Login'.

Goulet answered 28/8, 2020 at 13:55 Comment(2)
I don't understand why this answer was downvoted. Jila explained that there is no file where Login translation is located. I'll just add for the full answer that by default laravel checks the folder lang/en/* because config('app.locale') is set to 'en'. If you change locale to 'ru', laravel will look into lang/ru/*Jabe
You just made a good point. I was confused searching for this translation but couldn't find the associated file. I now understand it.Towhaired

© 2022 - 2024 — McMap. All rights reserved.