Change language

Hello friends

I’m trying change the language of my app, I was read this guide:

http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html

And I put this in my web.php to change the language of my app to Portuguese:




'components' => [

        

        'i18n' => [

        'translations' => [

            'app*' => [

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

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

                'sourceLanguage' => 'pt-BR',

                'fileMap' => [

                    'app' => 'app.php',

                    'app/error' => 'error.php',

                ],

            ],

        ],

    ],




But the default errors messages of my forms are in english. How can I do to my app be in Portuguese?

When I use Yii 1 I only change in main.php adding this line:

‘sourceLanguage’ => ‘pt_br’,

‘language’ => ‘pt_br’,

What Can i do on Yii2

Just define custom language like that:




// config/main.php

return [

     'language' => 'ru' // custom lang

];



If not work - also add sourceLanguage.

Hello friend

Thanks for help.

Should I put it in web.php, in components? There is not config/main.php on yii2.

If it does not, where do I add the source?

Hello, I will use this topic to ask one question.

How can you dynamically set which language application should use ? I would like to have two links in my main menu where users can click: Fr | En . If user click "Fr" I want application to load French translation and if user click "En" I want English one. What do we need to do to achieve this ? What is the best way ?

@black-room-boy Have you found a solution for this yet?

I hope it helps

Thanks for the tip. It give’s me a pretty good idea of how to solve this. Thanks.

If the sourceLanguage and language are the same value, the translation mechanism will not happen - it is the same as saying my messages in code and my messages in e.g. database are in the same language - why translate?

The sourceLanguage needs to be the language of the messages in Yii::t() in code and the language needs to be what language you want them displayed in.

@black-room-boy - don’t hijack someone else’s thread - ask another question on the forum!