【问题标题】:How can enable users switching between languages with URI Language Identifier如何使用户能够使用 URI 语言标识符在语言之间切换
【发布时间】:2012-11-05 17:16:38
【问题描述】:

我正在使用这个URI Language Identifier

http://localhost/internationalisation/index.php/en/
http://localhost/internationalisation/index.php/en/welcome/
http://localhost/internationalisation/index.php/en/contact/
http://localhost/internationalisation/index.php/es/
http://localhost/internationalisation/index.php/es/welcome/

我已经设置了$config['lang_ignore'] = FALSE;,所以 URL 会显示我当前使用的语言,就像上面一样。

问题:如何让用户在语言之间切换?

View 中的这些代码不起作用:

<a href="<?php echo site_url('en'); ?>">English</a>
<a href="<?php echo site_url('es'); ?>">Spanish</a>

因为他们会产生这样的链接:

http://localhost/internationalisation/index.php/en/en
http://localhost/internationalisation/index.php/en/es

谢谢

【问题讨论】:

    标签: php codeigniter url


    【解决方案1】:

    你有几个选择...

    • site_url 将始终在末尾返回 /en 或 /es 或任何路径位,因此您可以使用字符串函数来破坏(删除)结束位并添加您自己的。

    • 您可以设置具有站点名称并引用该站点名称的配置属性(“http://localhost/internationalisation/index.php”),然后附加您的语言标识符。

    • 您可以将相对路径和 basename 函数与 FILE 魔术常量一起使用

    <?php echo basename(__FILE__) . '/en'; ?>
    

    【讨论】:

      【解决方案2】:

      遵循 LastCoder 的第一个建议:

      配置:

      $config['lang_ignore'] = FALSE;
      

      查看:

      <a href="<?php echo site_url('language/en'); ?>">English</a>
      <a href="<?php echo site_url('language/es'); ?>">Spanish</a>
      

      控制器:

      class Language extends CI_Controller
      {
          public function __construct()
          {
              parent::__construct();
      
              $this->load->helper('url');
      
              $lang_uri_abbr = $this->config->item('lang_uri_abbr');
      
              if ($this->uri->segment(2) === false ||
                  ! isset($lang_uri_abbr[$this->uri->segment(2)]))
              {
                  redirect();
              }
              else
              {
                  $site_url = substr(site_url(), 0, -2);
      
                  redirect($site_url . $this->uri->segment(2));
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-09
        • 2018-07-29
        • 1970-01-01
        • 2021-06-24
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多