I would like to add a language code segment to my URIs in CodeIgniter, but I'd like to somehow — possibly with routing — force the browser to stop interpreting a language code as a path.
I haven't yet successfully implemented any of the i18n libraries in my CodeIgniter install, and I'm fairly certain my problem is simple enough to solve without a library anyway.
The method I had in mind was simply to load the appropriate language files respective of the language code that appears in the URI.
For example
http://example.com/about // Default language
http://example.com/sv/about // Load Swedish language
Here's the controller logic:
<?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);
}
}
?>
Am I going about this the wrong way? If so, why? Please avoid canned responses.