Translation strings as keys in Laravel

冷暖自知 提交于 2019-12-24 02:12:06

问题


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

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

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


回答1:


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

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

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

de.json:

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

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.

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

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




回答2:


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 BabelEdit displays 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.



回答3:


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

/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 notation to print with {{ __() }} or use @lang()

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

<!-- OR -->

@lang('messages.welcome')



回答4:


Messages (you can choose) is the name of your translation file. For each language you make a dir in the lang folder. Spanish is es.

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

I hope this helps...



来源:https://stackoverflow.com/questions/45451215/translation-strings-as-keys-in-laravel

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