将字符串转换为Laravel中的键 [英] Translation strings as keys in Laravel

查看:112
本文介绍了将字符串转换为Laravel中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检索翻译字符串上阅读了翻译字符串的文档.但是我不知道如何应用它.

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

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

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?

推荐答案

我终于理解了这个概念.在resources/lang中,您可以为每种语言(例如e)创建翻译JSON文件. g.:

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

无需创建en.json文件,因为如果未使用App::setLocale()设置语言,则en将是默认语言.

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."
}

接下来,您通过App::setLocale();在控制器中设置语言,现在变得很有趣了.在视图中,您仅包含JSON的密钥,例如. G.

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.') }}

,根据您的本地化,Laravel将自动加载正确的翻译.

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

这篇关于将字符串转换为Laravel中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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