Not loading correct language

Hi,

Im developing a new project to community that is a all-in-one project with admin already configure with docker and more things. I have stopped in a part that i debug but dont understand why it happens on Yii2.

I have a folder with all my translations in "pt-BR" and "en-US". And have a configuration to translate all messages from frontend to the frontend/messages folder. When i set language to "pt-BR" is get all messages from "pt-BR" folder. If i set to "en-US" it dont get the messages from "en-US" folder. If i set to "en", it get the message from "en-US" folder.

1 - message configuration:




'i18n' => [

    'translations' => [

        'frontend*' => [

            'class' => 'yii\i18n\PhpMessageSource',

            'basePath' => '@frontend/messages',

        ],

    ],

],



2 - My folders structure is attached.

3 - My bootstrap code:




'bootstrap' => [

        'log',

        [

            'class' => 'backend\components\LanguageSelector',

            'supportedLanguages' => ['pt-BR', 'en-US'],

        ],

]



4 - My language loader class:




<?php


namespace backend\components;


use yii\base\BootstrapInterface;


class LanguageSelector implements BootstrapInterface

{


    public $supportedLanguages = [];


    /**

     * @inheritdoc

     */

    public function bootstrap($app)

    {

        $app->language = 'en-US';   // en = works, en-US = dont works, pt-BR = work

    }


}



Can anyone help me with it?

I see that have a line of code inside MessageSource.php that is:




if ($this->forceTranslation || $language !== $this->sourceLanguage) {

    // translate ...

}



But i dont know why the framework dont let get the message from en-US folder or my folder need have a different name? My translations are not phrases, but a key, like "Model.CreatedAt".

Can anyone help me with it?

Exactly. In your case, sourceLanguage and targetLanguage are the same, so you need to set forceTranslation flag to true.

http://www.yiiframework.com/doc-2.0/yii-i18n-messagesource.html#$forceTranslation-detail

It could be useful to allow language recognition by browser configuration:




<?php


namespace frontend\components;


use yii\base\BootstrapInterface;


class LanguageSelector implements BootstrapInterface

{

    public function bootstrap($app)

    {

        $supportedLanguages = array_keys(\Yii::$app->params['languages']);

        

        if(\Yii::$app->session->has('language') && (in_array(\Yii::$app->session->get('language'), $supportedLanguages)))

        {

            $app->language = \Yii::$app->session->get('language');

        }

        else

        {

            $preferredLanguage = $app->request->getPreferredLanguage($supportedLanguages);

            $app->language = $preferredLanguage;

        }

    }

}

?>



where supportedLanguages are defined in frontend/common/params.php:




<?php

return [

    // Languages

    'languages' => [

        'en' => [ 'code' => 'en', 'label' => 'English' ],

        'it' => [ 'code' => 'it', 'label' => 'Italiano' ],

        'tr' => [ 'code' => 'tr', 'label' => 'Türkçe' ],

        'ro' => [ 'code' => 'ro', 'label' => 'Română' ],

        'zh' => [ 'code' => 'zh', 'label' => '中文' ],

    ],

];



Finally, let me to suggest my extension to configure languages translations dinamically:

Thanks. It is working now:




'i18n' => [

            'translations' => [

                'frontend*' => [

                    'class' => 'yii\i18n\PhpMessageSource',

                    'forceTranslation' => true,

                    'basePath' => '@frontend/messages',

                ],

            ],

],