Laravel default auth modult translation

筅森魡賤 提交于 2019-12-08 12:08:50

问题


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?


回答1:


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.




回答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.');


来源:https://stackoverflow.com/questions/51851907/laravel-default-auth-modult-translation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!