Thursday, March 10, 2022

[FIXED] i18n translations not working in Controller

Issue

Why would i18n translations work in a view, but not in AppController?

Doesn't work - In AppController:

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

Works - In my view:

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

Solution

I simply had to set the language before calling __(). In my case, it was being set further down the chain in initialize() within AppController.



Answered By - TechFanDan

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.