我想語言代碼段笨添加到我的URI,但我想以某種方式 - 可能與路由 - 強制瀏覽器停止解釋一個語言代碼的路徑。笨語言代碼路由
我還沒有成功實施任何國際化圖書館在我的笨安裝,我敢肯定我的問題很簡單,而不庫反正來解決。
我想到的方法是簡單地加載相應的出現在URI的語言代碼的相應的語言文件。
例如
http://example.com/about // Default language
http://example.com/sv/about // Load Swedish language
這裏的控制器邏輯:
<?php
// ** Update **
// The following updated code works, as long as the language code appears
// at the end of the URI, e.g, http://example.com/about/sv
//
// Ideally, I would like the language code segment to always appear first.
//
// There is also the problem of keeping the language selected while
// navigating around the site…
class Pages extends CI_Controller {
public function view ($page = 'home') {
if (!file_exists('application/views/pages/'.$page.'.php'))
show_404();
$uri = explode("/", $_SERVER['REQUEST_URI']);
$lang_code = end($uri);
$data['title'] = $lang_code;
switch ($lang_code) {
case "sv": $the_language = "swedish"; break;
case "no": $the_language = "norwegian"; break;
default: $the_language = "english";
}
$this->lang->load('general',$the_language);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
我要對這個錯誤的方式?如果是這樣,爲什麼?請避免預先回復。
另一種解決方案是將選擇在cookie ,然後創建你自己的擴展到CI_Controller,它試圖讀取語言選擇(或設置默認值),並將其放置在'$ this-> the_language'或類似的地方。當然,使用URL中的語言代碼可以讓用戶清楚所期望的語言,所以它也有好處。 – danneth
是的,因爲這個原因,我真的希望語言代碼存在於URI中。 –
有肯定是一個更好的方式來做到這一點(htaccess的可能),但也許你可以改變/擴展路由類首先要檢查的URI段是語言(僞這裏)'如果(in_array(uri_segment(1),陣列(「SV 」,‘恩’)){set_language_var; strip_first_segment_from_uri;}繼續路由;' – danneth