检查 Laravel 的 trans() 中的行是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29210690/
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
Check line existence in laravel's trans()
提问by Koen B.
Lets say in my lang/en/general.php
there are multiple translation lines for example:
假设在我的中lang/en/general.php
有多个翻译行,例如:
"token" => "This password reset token is invalid.",
"sent" => "Password reminder sent!",
"reset" => "Password has been reset!",
But in my lang/de/general.php
these lines are missing.
但是在我的lang/de/general.php
这些行中丢失了。
So later, when I use the Lang::get('general.token')
or simply trans('general.token')
所以后来,当我使用Lang::get('general.token')
或简单地trans('general.token')
The english version will return
英文版会回来
This password reset token is invalid.
此密码重置令牌无效。
And the german (de) version will return
德语(de)版本将返回
general.token
通用令牌
Is there any way I can handle a 'translation not found' function, like a filter but not creating a special class for it? For example, when a line has no translation, I want to throw an Exception.
有什么方法可以处理“找不到翻译”功能,例如过滤器但不为其创建特殊类?例如,当一行没有翻译时,我想抛出一个异常。
Thanks in advance!
提前致谢!
回答by Kalhan.Toress
回答by Kyobul
This question is getting a little bit old but as per version 5.8 you can simply check as this :
这个问题有点老了,但根据 5.8 版,您可以简单地检查如下:
array_key_exists('your-word-key', trans('your-file'))
or
或者
array_key_exists('your-word-key', trans('your-file.array_key'))
for nested translations
嵌套翻译
回答by jpcaparas
You might want to write a helper similar to the one below to help with fallbacks:
你可能想写一个类似于下面的帮助器来帮助回退:
/**
* Makes translation fall back to specified value if definition does not exist
*
* @param string $key
* @param null|string $fallback
* @param null|string $locale
* @param array|null $replace
*
* @return array|\Illuminate\Contracts\Translation\Translator|null|string
*/
function trans_fb(string $key, ?string $fallback = null, ?string $locale = null, ?array $replace = [])
{
if (\Illuminate\Support\Facades\Lang::has($key, $locale)) {
return trans($key, $replace, $locale);
}
return $fallback;
}
Note: The helper only works on PHP 7.1 (which has support nullable types). Adjust it to your PHP version if it's lower than 7.1.
注意:该助手仅适用于 PHP 7.1(支持可空类型)。如果低于 7.1,则将其调整为您的 PHP 版本。
回答by vladsch
You can create your own TranslationServiceProvider and Translator and override the get() method in translator to throw an exception when the parent::get() returns a translation string that is equal to the translation key that was passed in. Both @lang() and trans() functions call the get() method of the translator.
您可以创建自己的 TranslationServiceProvider 和 Translator 并覆盖翻译器中的 get() 方法以在 parent::get() 返回等于传入的翻译键的翻译字符串时抛出异常。 @lang()和 trans() 函数调用翻译器的 get() 方法。
Seems like a whole lot of trouble only to get another reason for a "Whoops! something went wrong!" on your site. You will only get the exception when the translation is encountered.
似乎遇到了很多麻烦只是为了得到另一个原因“哎呀!出了点问题!” 在您的网站上。您只会在遇到翻译时收到异常。
Another solution: you can use the barryvdh/laravel-translation-manager package, it has a translation service provider that logs missing translation keys and a web interface for managing translations. It will log missing translation keys per locale and let you edit them through a web interface.
另一种解决方案:你可以使用 barryvdh/laravel-translation-manager 包,它有一个翻译服务提供者记录丢失的翻译键和一个用于管理翻译的网络界面。它将记录每个区域设置丢失的翻译键,并让您通过 Web 界面编辑它们。
It is simple to setup and easy to modify. So you can replace the logging with throwing an exception.
它设置简单,易于修改。所以你可以用抛出异常来替换日志记录。