简体   繁体   English

i18n 翻译在 Controller 中不起作用

[英]i18n translations not working in Controller

Why would i18n translations work in a view , but not in AppController ?为什么 i18n 翻译可以在view中工作,而不是在AppController中?

Doesn't work - In AppController :不起作用 - 在AppController中:

class AppController extends Controller
{
    public function initialize()
    {
        parent::initialize();
        
        debug(__('My English Text'));
        ...
    }
}

Works - In my view :作品 - 在我view

<p><?= __('My English Text') ?></p>

I simply had to set the language before calling __() .我只需要在调用__()之前设置语言。 In my case, it was being set further down the chain.就我而言,它被设置在链条的更下游。

You can use it like this你可以像这样使用它


$test = __('My English Text');
dd($test);

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

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