翻译字符串作为 Laravel 中的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45451215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:25:44  来源:igfitidea点击:

Translation strings as keys in Laravel

laravelblade

提问by Andreas

I read the documentation for translation strings on Retrieving Translation Stringsbut somehow I don't understand how to apply it.

我阅读了有关检索翻译字符串的翻译字符串文档,但不知何故我不明白如何应用它。

Let's say I would like to render in a view posts.indexthe message "I like programming"in English, German ("Ich mag Programmieren") or Spanish ("Me encanta programar"), depending on the localization set by App::setLocale().

假设我想用英语、德语(“ Ich mag Programmieren”)或西班牙语(“ Me encanta programar”)在视图中呈现posts.index消息“我喜欢编程”,具体取决于 App::setLocale() 设置的本地化.

How would the translation files look like and how would I setup the view?

翻译文件会是什么样子,我将如何设置视图?

回答by Andreas

I finally understood the concept. Within resources/langyou create a translation JSON file for every language, e. g.:

我终于明白了这个概念。在resources/lang您内部为每种语言创建一个翻译 JSON 文件,例如:

/resources
    /lang
        /de.json
        /es.json

There is no need to create an en.jsonfile as enwill be the default language if you don't set a language with App::setLocale().

如果您没有设置语言,则无需创建en.json文件作为en默认语言App::setLocale()

de.json:

de.json:

{
     "I love programming.": "Ich mag programmieren."
}

es.json:

es.json:

{
     "I love programming.": "Me encanta programar."
}

Next, you set in your controller the language via App::setLocale();and now comes the fun part. In the view, you only include the key of the JSON, e. g.

接下来,您在控制器中设置语言App::setLocale();,现在是有趣的部分。在视图中,您只包含 JSON 的键,例如

{{ __('I love programming.') }}

{{ __('I love programming.') }}

and depending on your localization, Laravel will automatically load the correct translation.

并且根据您的本地化,Laravel 会自动加载正确的翻译。

回答by Andreas L?w

I'd recommend to stay away from using translation strings and use keys instead:

我建议不要使用翻译字符串,而是使用键:

  1. Keys contain context information: main-screen.dialog.add-item-button- you know that this is a button on the main screen. This is much better than working with a string Add Item.
  2. An editor like BabelEditdisplays the keys as tree. This makes it much easier to edit a particular subset of your translations. Using strings you easily end up with a list like Abort, Add item, Alabama, Alaska, All, Arizona,..
  3. You can use different translations for different positions of the same text. E.g. for a form label you can create a shorter translation if you don't have enough space in a language.
  4. You don't have to update all translation files if your main languages changes. E.g. if you add decide to change Hello world.to Hello world!you don't have to update all your files.
  1. 键包含上下文信息:main-screen.dialog.add-item-button- 您知道这是主屏幕上的一个按钮。这比使用 string 好得多Add Item
  2. BabelEdit这样的编辑器将键显示为树。这使得编辑翻译的特定子集变得更加容易。使用字符串,你很容易得到一个像Abort, Add item, Alabama, Alaska, All, Arizona,..
  3. 您可以对同一文本的不同位置使用不同的翻译。例如,对于表单标签,如果您没有足够的语言空间,您可以创建更短的翻译。
  4. 如果您的主要语言发生变化,您不必更新所有翻译文件。例如,如果您添加决定更改Hello world.Hello world!则不必更新所有文件。

回答by Zayn Ali

Store your language files within resources/lang, and structure would be like this.

将您的语言文件存储在 中resources/lang,结构将是这样的。

/resources
    /lang
        /en
            messages.php
        /es
            messages.php

All language files simply return an array of keyed strings. For example:

所有语言文件都只返回一个键控字符串数组。例如:

<?php

return [
    'welcome' => 'Welcome to our application'
];

Then you've to define your route where you capture your locale and set it. like this

然后,您必须定义捕获区域设置的路线并进行设置。像这样

Route::get('welcome/{locale}', function ($locale) {
    App::setLocale($locale);

    // your code here
});

And then simply use dot notationto print with {{ __() }}or use @lang()

然后简单地用于dot notation打印{{ __() }}或使用@lang()

{{ __('messages.welcome') }}

<!-- OR -->

@lang('messages.welcome')

回答by Bas

Messages (you can choose) is the name of your translation file.

Messages(您可以选择)是您的翻译文件的名称。

For each language, you make a dir in the lang folder. Spanish is es.

对于每种语言,您在 lang 文件夹中创建一个目录。西班牙语是es。

{{ trans('messages.cool') }}

enter image description here

在此处输入图片说明

I hope this helps...

我希望这有帮助...