Pergunta

I have designed a web application, It works for two different users say user1 and user2, and both of the users need the view in different languages.

I have studied about yii:t() but by that method we have to define language in main.config, which set the same language for both users.

How can I translate my view in different languages for both users?

Foi útil?

Solução

I hope this can help you: you need to edit urlmanager.php in your components, if there is no file, you need to create one.

Check this url: Multilingual

Thanks.

Outras dicas

Put this in your SiteController.php:

public function actionChangeLocale($locale) {

    // (OPTIONAL) if is registered user (not guest), save preferred locale in database 
    if (!Yii::app()->user->isGuest) {

        // Update user settings
        $uid = Yii::app()->user->id;
        User::model()->updateByPk($uid, array('locale' => $locale));
    }

    // change locale
    Yii::app()->user->setState('_locale', $locale);

    // redirect to previous page, in the new locale
    if(isset($_SERVER["HTTP_REFERER"]))
        $referrer = $_SERVER["HTTP_REFERER"];
    else
        $referrer = Yii::app()->getBaseUrl(true) . '/';

    $this->redirect($referrer);
}

Edit your main.php config url manager rules:

'urlManager' => array(
    'urlFormat' => 'path',
        'showScriptName' => false,
        'caseSensitive' => false,
        'rules' => array(
             'lang/<id:\w+>' => 'site/changeLocale',

To change locale, create a link to point user to desired locale:

http://www.mysite.com/myapp/lang/en
http://www.mysite.com/myapp/lang/zh
http://www.mysite.com/myapp/lang/ja
http://www.mysite.com/myapp/lang/in
...

If you saved the logged-in user's preferred locale in database, add this to SiteController.php Login action:

    $uid = Yii::app()->user->id;
    $user = User::model()->findbypk($uid);

    $userLocale = isset($user->locale) ? $model->locale : Yii::app()->language;
    Yii::app()->user->setState('_locale', $userLocale);

Above usage is for those using htaccess rewrite. Make sure base .htaccess file is:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] # Remove trailing slash

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Related Articles:

Related Modules:

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top