【问题标题】:Codeigniter Internationalization i18n trailing slash not added未添加 Codeigniter 国际化 i18n 斜杠
【发布时间】:2013-06-12 14:57:03
【问题描述】:

我有一个项目正在进行,现在[管理部分几乎完成](我知道有点晚了)我正在尝试在项目中实施i18n。 我认为一切正常,除非当我输入http://localhost/my_project 而my_project 是其中安装了CI 的工作目录时,我被重定向到以下http://localhost/my_project/enhomeen 之后没有斜杠)有什么想法吗?

预期结果 http://localhost/my_project/en/home,不仅仅是 home 控制器,而是所有控制器的行为都相同。

.htaccessbase_urlindex_page 设置正确(所有工作没有 i18n)。

routes.php 缺货

$route['default_controller'] = "home";
$route['404_override'] = '';

// URI like '/en/about' -> use controller 'about'
$route['^(en|de|fr|nl)/(.+)$'] = "$2";

// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(en|de|fr|nl)$'] = $route['default_controller'];

编辑 我正在使用来自 ellislab 的“新”i18n。抱歉链接错误(适用于 1.7 CI 版本)。

【问题讨论】:

  • 你如何重定向它?你能发布那个代码吗?
  • 我没有在其他项目上重定向任何库(i18n 完成所有工作),它开箱即用。但在这一点上有问题。

标签: codeigniter internationalization routes


【解决方案1】:

所以经过一番挖掘,我在核心文件中发现了问题,在这个发布的脚本的底部有变量$new_url,它在没有“修复”的情况下回显Redirect triggered to http://192.168.1.184/my_project/enhome。我刚刚在其中添加了/,它运行良好我想知道这里发生了什么以及这是否是OK 修复。

function MY_Lang() {
    parent::__construct();

    global $CFG;
    global $URI;
    global $RTR;


    $this->uri = $URI->uri_string();
    $this->default_uri = $RTR->default_controller;

    $uri_segment = $this->get_uri_lang($this->uri);
    $this->lang_code = $uri_segment['lang'] ;

    $url_ok = false;
    if ((!empty($this->lang_code)) && (array_key_exists($this->lang_code, $this->languages)))
    {
        $language = $this->languages[$this->lang_code];
        $CFG->set_item('language', $language);
        $url_ok = true;
    }

    if ((!$url_ok) && (!$this->is_special($uri_segment['parts'][0]))) // special URI -> no redirect
    { 
      // set default language
      $CFG->set_item('language', $this->languages[$this->default_lang()]);

      $uri = (!empty($this->uri)) ? $this->uri: $this->default_uri;
      //OPB - modification to use i18n also without changing the .htaccess (without pretty url) 
      $index_url = empty($CFG->config['index_page']) ? '' : $CFG->config['index_page']."/";
      $new_url = $CFG->config['base_url'].$index_url.$this->default_lang()."/".$uri;
      echo "Redirect triggered to ".$new_url;

      //header("Location: " . $new_url, TRUE, 302);
      // exit;
    }
}

【讨论】:

    【解决方案2】:

    这个库有点旧,作者很久没有更新了,它适用于 CI 1.7,但这里是与 CI 2.1 兼容的更新版本。* 相同的库,但更新版本的工作方式与您可以从这里下载的方式相同

    CodeIgniter 2.1 internationalization i18n

    【讨论】:

    • 我正在使用来自 github 的新 :)。
    • 我最近的项目使用了相同的,完全没有问题
    • 我也在不同的项目中使用它! (没问题)。但是现在这个新问题出现了,在“自动重定向”(从localhost/my_project/whateverlocalhost/my_project/en/whatever)之后没有插入斜杠,它实际上重定向到localhost/my_project/enwhatever
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2017-06-09
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多