1

このURI言語識別子を使用しています

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に表示されるように設定しました。

質問:ユーザーが言語を切り替えることができるようにするにはどうすればよいですか?

ビューのこれらのコードは機能しません:

<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

ありがとう

4

2 に答える 2

1

いくつかのオプションがあります...

  • site_urlは常に/enまたは/es、あるいはパスの最後のビットを返すので、文字列関数を使用してエンドビットをマングル(削除)し、独自のビットを追加できます。

  • サイト名と参照( "http://localhost/internationalisation/index.php")を持つ構成プロパティを設定してから、言語識別子を追加できます。

  • 相対パスとbasename関数をFILEマジック定数で使用できます

<?php echo basename(__FILE__) . '/en'; ?>
于 2012-11-05T17:42:03.097 に答える
0

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));
        }
    }
}
于 2012-11-06T09:27:34.400 に答える