duan0427 2018-04-09 07:50
浏览 82
已采纳

Yii2为ajax搜索字段创建多语言网站

I'm using Yii2 and I want to create a web application with the ability to perform fast searches.
For example, when I type characters in a textbox, results displayed.
It's easy with ajax when we have only one language but how about in multilingual mode?

  • 写回答

1条回答 默认 最新

  • dongzong5017 2018-04-09 08:13
    关注

    First set up multi language for your site there is doc for this.

    Best way of auto support multi language for your site is using cookies variable for language. You can set up language cookies from any action as

       public function actionLanguage()
    {
        if (isset($_POST['lang'])) {
            $language = $_POST['lang'];
    
            if (($langaugeModel = \app\models\Langauge::findOne(['name' => $language])) !== null) {
                $varLang = [
                        'id' => $langaugeModel->id,
                        'name' => $langaugeModel->name,
                        'iso1' => $langaugeModel->iso1,
                        'iso2' => $langaugeModel->iso2
                    ];
                $cookies = new Cookie([
                    'name' => 'lang',
                    'value' => json_encode($varLang),
                ]);
                yii::$app->getResponse()->getCookies()->add($cookies);
                return $this->goBack((!empty(Yii::$app->request->referrer) ? Yii::$app->request->referrer : null));
            } else {
                throw new NotFoundHttpException('The requested langauge does not exist.');
            }
    
        } else {
                return $this->goBack((!empty(Yii::$app->request->referrer) ? Yii::$app->request->referrer : null));
        }
    }
    

    Here what I did was i placed all the language support of site in database and generate necessary cookies variable and placed it on client browser.

    Next set up be before request event of your yii2 site in config/web.php file as

     'as beforeRequest' => [
       'class' => 'app\components\MyBehavior',
    ],
    

    then create components\Mybehaviou.php file and place this code

      namespace app\components;
      use yii;
      use yii\base\Behavior;
      class MyBehavior extends Behavior
       {
         public function events(){
           return [
               \yii\web\Application::EVENT_BEFORE_REQUEST => 'myBehavior',
             ];
           }
        public function myBehavior(){
        if (\yii::$app->getRequest()->getCookies()->has('lang')) {
            $langIso = 'sdn';
            \yii::$app->language = $langIso;
            $langaugeVar = \yii::$app->getRequest()->getCookies()->getValue('lang');
    
            $langauge = json_decode($langaugeVar);
            $langIso = $langauge->iso2;
    
            \yii::$app->language = $langIso;
    
        }
      }
    }
    

    This create your site language which depends on client because it depends on cookies of client.

    Then create your search controller according to site language(\yii::$app->language)

    for ajax search you can use select2 Widget. you can find demo and configuration on this link

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同