yii 在阿拉伯语和英语站点中制作多种语言 url

yii make multiple language url in Arabic and English site

找了半天,想制作多语言网站文件夹 喜欢:ar / en

http://localhost/yii/en

 http://localhost/yii/ar

正确的方法 - 创建文件夹 或者 -change url 取决于 lang session

首先你可以在

中创建语言文件

protected\messages\ar Blockquote the file name should be translation.php, The language file contains array key and values pairs

<? return array (
"Why Join?"=>"Warum Mitglied werden?");

之后就可以访问urlhttp://localhost/projects/ar了。 请记住,所选语言应存储在会话中

对于这种情况,Yii 有非常好的 wiki: SEO conform multilingual urls and language selector widget

  1. 扩展 CUrlManager

创建文件'components/UrlManager.php',内容为:

<?php
class UrlManager extends CUrlManager
{
    public function createUrl($route,$params=array(),$ampersand='&')
    {
        if (!isset($params['language'])) {
            if (Yii::app()->user->hasState('language'))
                Yii::app()->language = Yii::app()->user->getState('language');
            else if(isset(Yii::app()->request->cookies['language']))
                Yii::app()->language = Yii::app()->request->cookies['language']->value;
            $params['language']=Yii::app()->language;
        }
        return parent::createUrl($route, $params, $ampersand);
    }
}
?>
  1. 编辑您的控制器

将以下代码添加到 'components/Controller.php':

<?php
public function __construct($id,$module=null){
    parent::__construct($id,$module);
    // If there is a post-request, redirect the application to the provided url of the selected language 
    if(isset($_POST['language'])) {
        $lang = $_POST['language'];
        $MultilangReturnUrl = $_POST[$lang];
        $this->redirect($MultilangReturnUrl);
    }
    // Set the application language if provided by GET, session or cookie
    if(isset($_GET['language'])) {
        Yii::app()->language = $_GET['language'];
        Yii::app()->user->setState('language', $_GET['language']); 
        $cookie = new CHttpCookie('language', $_GET['language']);
        $cookie->expire = time() + (60*60*24*365); // (1 year)
        Yii::app()->request->cookies['language'] = $cookie; 
    }
    else if (Yii::app()->user->hasState('language'))
        Yii::app()->language = Yii::app()->user->getState('language');
    else if(isset(Yii::app()->request->cookies['language']))
        Yii::app()->language = Yii::app()->request->cookies['language']->value;
}
public function createMultilanguageReturnUrl($lang='en'){
    if (count($_GET)>0){
        $arr = $_GET;
        $arr['language']= $lang;
    }
    else 
        $arr = array('language'=>$lang);
    return $this->createUrl('', $arr);
}
?>

注意:如果我们没有为每个请求显式设置Yii::app()->language,它将等于其在confg中设置的默认值文件。如果未在配置文件中设置,它将等于值 Yii::app()->sourceLanguage,默认为 'en_us'。 您可以使用

在配置文件中为应用程序的语言和源语言设置默认值
'sourceLanguage'=>'en',
'language'=>'de',
  1. 构建语言选择器小部件

创建文件'components/widgets/LanguageSelector.php',内容为:

<?php
class LanguageSelector extends CWidget
{
    public function run()
    {
        $currentLang = Yii::app()->language;
        $languages = Yii::app()->params->languages;
        $this->render('languageSelector', array('currentLang' => $currentLang, 'languages'=>$languages));
    }
}
?>

创建文件'components/widgets/views/languageSelector.php',内容为:

<div id="language-select">
<?php 
    if(sizeof($languages) < 4) {
        // Render options as links
        $lastElement = end($languages);
        foreach($languages as $key=>$lang) {
            if($key != $currentLang) {
                echo CHtml::link(
                     $lang, 
                     $this->getOwner()->createMultilanguageReturnUrl($key));
            } else echo '<b>'.$lang.'</b>';
            if($lang != $lastElement) echo ' | ';
        }
    }
    else {
        // Render options as dropDownList
        echo CHtml::form();
        foreach($languages as $key=>$lang) {
            echo CHtml::hiddenField(
                $key, 
                $this->getOwner()->createMultilanguageReturnUrl($key));
        }
        echo CHtml::dropDownList('language', $currentLang, $languages,
            array(
                'submit'=>'',
            )
        ); 
        echo CHtml::endForm();
    }
?>
</div>
  1. 将小部件放在您的网站上

在'views/layouts/main.php'

中的header-div中添加如下代码
<div  id="language-selector" style="float:right; margin:5px;">
    <?php 
        $this->widget('application.components.widgets.LanguageSelector');
    ?>
</div>
  1. 编辑您的配置文件

将以下 changes/additions 应用到文件 'config/main.php':

<?php
'components'=>array(
    ...
    'request'=>array(
        'enableCookieValidation'=>true,
        'enableCsrfValidation'=>true,
    ),
    'urlManager'=>array(
        'class'=>'application.components.UrlManager',
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'rules'=>array(
            '<language:(de|tr|en)>/' => 'site/index',
            '<language:(de|tr|en)>/<action:(contact|login|logout)>/*' => 'site/<action>',
            '<language:(de|tr|en)>/<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<language:(de|tr|en)>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<language:(de|tr|en)>/<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
        ),
    ),
),
'params'=>array(
    'languages'=>array('tr'=>'Türkçe', 'en'=>'English', 'de'=>'Deutsch'),
),
?>

我们将新的 class 'UrlManager' 声明为 urlManager 组件要使用的 class,并在我们的规则数组的键前加前缀 /。