Yii 如何为不同的用户切换区域设置/语言
我设计了一个web应用程序,它适用于两个不同的用户,即Yii 如何为不同的用户切换区域设置/语言,yii,translation,yii-extensions,yii-modules,Yii,Translation,Yii Extensions,Yii Modules,我设计了一个web应用程序,它适用于两个不同的用户,即user1和user2,这两个用户都需要不同语言的视图 我已经研究了yii:t(),但是通过这种方法,我们必须在main.config中定义语言,它为两个用户设置了相同的语言 如何为两个用户翻译不同语言的视图?我希望这能帮助您: 您需要在组件中编辑urlmanager.php,如果没有文件,则需要创建一个 检查此url: 谢谢。把这个放到你的站点控制器.php: public function actionChangeLocale($loca
user1
和user2
,这两个用户都需要不同语言的视图
我已经研究了yii:t()
,但是通过这种方法,我们必须在main.config
中定义语言,它为两个用户设置了相同的语言
如何为两个用户翻译不同语言的视图?我希望这能帮助您:
您需要在组件中编辑urlmanager.php,如果没有文件,则需要创建一个
检查此url:
谢谢。把这个放到你的站点控制器.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);
}
编辑main.phpconfig url管理器规则:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'lang/<id:\w+>' => 'site/changeLocale',
如果在数据库中保存了登录用户的首选区域设置,请将其添加到SiteController.php登录操作:
$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);
以上用法适用于使用htaccess重写的用户。确保基本.htaccess文件为:
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
相关文章:
相关模块:
嘿,Samuel,当我使用url时,我收到错误请求的url/Snitch appointpress/appointpress/lang/en在此服务器上找不到。嘿,编码员,我正在处理该链接,当我在UI中单击任何语言链接时,它会给出一个错误,如url:site/logout not found,我的url位于?r=site/logout,我该怎么做
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