yii在阿拉伯语和英语网站上制作多语言网址

时间:2015-02-03 11:11:06

标签: php yii

经过长时间的搜索,我想制作多个语言的网站文件夹 喜欢:ar / en

http://localhost/yii/en

 http://localhost/yii/ar

正确的方式 - 创建文件夹 要么 -change url取决于lang session

2 个答案:

答案 0 :(得分:0)

首先,您可以在

中创建语言文件
  

保护\消息\ AR   Blockquote文件名应该是translation.php,语言文件包含数组键和值对

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

之后,您可以访问网址http://localhost/projects/ar。 请记住,所选语言应该是会话中的stord

答案 1 :(得分:0)

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

  1. 扩展CUrlManager
  2. 使用内容

    创建文件'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. 编辑您的控制器
    2. 将以下代码添加到'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() - &gt;语言,它将等于confg文件中设置的默认值。如果未在配置文件中设置,它将等于值Yii :: app() - &gt; sourceLanguage,默认为'en_us'。 您可以使用

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

        创建文件'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. 将小部件放在您的网站上
        2. 将以下代码添加到'views / layouts / main.php'

          中的header-div中
          <div  id="language-selector" style="float:right; margin:5px;">
              <?php 
                  $this->widget('application.components.widgets.LanguageSelector');
              ?>
          </div>
          
          1. 编辑配置文件
          2. 将以下更改/添加内容应用于'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'),
            ),
            ?>
            

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