简体   繁体   English

Laravel字符串配置文件中的本地化

[英]Laravel string Localization in config files

I'm having problem using trans() function in config file, I feel it not supposed to be used that way. 我在配置文件中使用trans()函数时遇到问题,我觉得不应该这样使用。 However I've no clue on what would be the most efficient way to translate string text in config files (files in /config folder). 但是我不知道在配置文件(/ config文件夹中的文件)中翻译字符串文本的最有效方法是什么。

Original code 原始代码

<?php

return [
    'daily' => 'Daily'
];

When I try to implement trans() application crashes and laravel return white page without any error messages 当我尝试实现trans()应用程序崩溃和laravel返回白页而没有任何错误消息

<?php

return [
    'daily' => trans('app.daily_text')
];

The config files are one of the first stuff Laravel initialize, it means you can't use Translator nor UrlGenerator inside a config file. 配置文件是Laravel初始化的第一个东西,它意味着你不能在配置文件中使用TranslatorUrlGenerator

I don't know what you are trying to do, but you shouldn't need to use Translator inside a config file though... 我不知道你要做什么,但你不应该在配置文件中使用Translator但是......

You cannot not use trans or route method inside the Laravel config file. 您不能在Laravel配置文件中使用trans或route方法。 At the time the config file is loaded, these methods are not available to run. 在加载配置文件时,这些方法无法运行。 Also, the purpose of the configuration file is used for storing pure value and we should not trigger any actions inside the configuration file. 此外,配置文件的用途是用于存储纯值,我们不应该触发配置文件中的任何操作。

I know sometimes you want to put things into config file with dynamic data generated from route or text from language key. 我知道有时你想把东西放到配置文件中,其中包含路由生成的动态数据或来自语言键的文本。 In my usecase is: configure the menu structure inside the config file. 在我的用例中是:配置配置文件中的菜单结构。 On that case, you should choose the approach of: storing only the translation key and an array which include information that you can generate the URL at run time. 在这种情况下,您应该选择以下方法:仅存储转换键和包含可在运行时生成URL的信息的数组。

I put my gist here for you to look up on the approach. 我把我的要点放在这里让你查看方法。

You can just store the key in config file like and then use the trans function in the view to get the translations: 您可以将密钥存储在配置文件中,然后使用视图中的trans函数来获取翻译:

Config file: 配置文件:

<?php

return [
'foo' => 'bar'

];

Then in the view: 然后在视图中:

{{ trans(config('config.foo') }}

I don't know if this is good practice but I ended doing this in my similar situation. 我不知道这是不是很好的做法,但我在类似的情况下结束了。

Config.php: config.php文件:

'Foo' => array('
'route' => 'route.name',
'name' => 'translated_line', //translated in lang file ex. /en/general.php
'),

Then in the view I used: 然后在我使用的视图中:

<a href="{{ route(Config::get('foo.route')) }}">{{ Lang::get('general.'.Config::get('foo.name'))) }}</a>

Maybe this is too late but I posted it here anyway so that maybe someone will find it useful, like me :)) 也许这太晚了,但无论如何我都在这里发布,所以也许有人会发现它很有用,就像我:))

As of Laravel v5.4, you can use the __ helper function to access the translations after Laravel has booted. Laravel v5.4开始,您可以使用__ helper函数在Laravel启动后访问翻译。

Example: 例:

config/example.php 配置/使用example.php

<?php

return [
    'daily' => 'Daily',
    'monthly' => 'app.monthly_text',
    'yearly' => 'app.yearly_text'
];

resources/lang/en/app.php 资源/郎/ EN / app.php

<?php

return [
    'monthly_text' => 'Monthly'
];

You can access the translations like so: 您可以像这样访问翻译:

<?php

// ...

$daily = config('example.daily');
$a = __($daily); // "Daily"

$monthly = config('example.monthly');
$b = __($monthly); // "Monthly"

$yearly = config('example.yearly');
$c = __($yearly); // "app.yearly_text"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM