Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel default auth module translation

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?

like image 722
Yevgeniy Afanasyev Avatar asked Aug 15 '18 01:08

Yevgeniy Afanasyev


2 Answers

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>
like image 194
irandoust Avatar answered Oct 16 '22 13:10

irandoust


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.');
like image 2
Yevgeniy Afanasyev Avatar answered Oct 16 '22 12:10

Yevgeniy Afanasyev