Bloodhound
Bloodhound

Reputation: 2966

How to change the language based on user preference in Yii2?

my site start with a default language(which is English) then based on user's preference i should change it. is this possible in Yii2 ? is there any widget for this

Upvotes: 1

Views: 420

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

I use contentNegotiator, without assign a language to the user the language is automatically assigned by the application.

for this In config/main.php in bootstrap section start the component

'bootstrap' => [
    'log',
    'contentNegotiator',
],

in component section

'components' => [
    'contentNegotiator' =>[
        'class' => 'yii\filters\ContentNegotiator',
        'languages' => [
                'en-US',
                'it-IT',
                'fr-FR',
        ],
    ],

],

otherwise you can change when and where you want. Is application action eg you can do in any controller you chose. this way

 \Yii::$app->language = 'zh-CN'; 

Upvotes: 3

Related Questions