Laravel默认身份验证模块翻译 [英] Laravel default auth module translation

查看:83
本文介绍了Laravel默认身份验证模块翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成了默认的Laravel身份验证模块.

I have generated the default Laravel auth module.

在模块刀片中的任何地方,我都看到Double Underscore __ 函数,假定几乎已经存在翻译了.

Everywhere in the blades of the module, I see Double Underscore __ function assuming that translation is almost there.

例如

<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?

我的意思是,如果我去Laravel文档站点,有这样的例子

I mean, if I go to the Laravel documentation site there are examples like this

echo __('messages.welcome');

附带说明

例如,让我们从 resources/lang/messages.php 语言文件中检索 welcome 翻译字符串:

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?

推荐答案

您只需要自己添加翻译文件即可. __('Login')表示字符串已准备好翻译,您可以在翻译文件中指向它.

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.

Laravel中的所有语言翻译文件应存储在 PROJECT_DIRECTORY/resources/lang 中.当您通过工匠进行Auth时,它会自动创建它.但是,如果找不到它,请手动创建.

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.

例如:

PROJECT_DIRECTORY/resources/lang 目录中创建一个名为 auth.php 的文件.然后在上面放一个简单的php数组:

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>

但是,文档还有另一种使用翻译字符串作为键的方法.在这种方法中,您可以使用本地名称在 PROJECT_DIRECTORY/resources/lang 中创建一个JSON文件,例如西班牙语名称为 es.json 或德语de.json ,这取决于您的本地名称.

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.

现在创建一个JSON对象,并将翻译内容与您在刀片中使用的字符串名称放在一起:

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!",
}

然后使用PHP double underscores方法在blade中调用您的翻译:

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默认身份验证模块翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆