Welcome Guest, Not a member yet? Register   Sign In
URL language switch - My approach
#1

[eluser]Stefan Hueg[/eluser]
Hello there,
I just did some research on how to implement a URL based language switcher for URLs in the scheme http://example.com/en/controller/method, but didn't find a viable solution. So here is mine:

1. Extend your config.php with the following entries:
Code:
$config['url_suffix'] = "";
$config['languages'] = array(
    'en' => 'english',
    'de' => 'german'
);
$config['lang_file'] = 'mts';
The languages array is for the url -> file association. The second entry "lang_file" defines a commonly used language file.

2. Set your default language in $config['language'].

2. Create a MY_URI.php in your Application folder and extend it like this:
Code:
<?php
class MY_URI extends CI_URI {
    function _explode_segments()
    {
        foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $index => $val)
        {
            // Filter segments for security
            $val = trim($this->_filter_uri($val));

            if ($val != '')
            {
                $this->segments[] = $val;
            }
        }
        $this->_extract_lang();
    }

    function _extract_lang()
    {
        if (isset($this->segments[0]) AND array_key_exists($this->segments[0], $this->config->item('languages')))
        {
            $languages = $this->config->item('languages');
            $this->config->set_item('language', $languages[$this->segments[0]]);
            array_shift($this->segments);
        }
    }
}
After exploding the segments _extract_lang() is called. It looks if the first segment is present. If it's in the $config['languages'] array, the entry $config['language'] will be overwritten with the currently selected language, stripped from the URL. Additionally the segments will be shifted.

4. Create a MY_Controller.php and extend your Controllers using this MY_Controller (or extend your own MY_Controller.php if present). Add the following Code:
Code:
<?php
class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
        $this->lang->load($this->config->item('lang_file'), $this->config->item('language'));
    }
}
It just loads the previously selected language. (language/$config['language']/$config['lang_file].php)

5. Extend CI_Router and it's function _validate_request(). This hack is needed because it would else skip the default controller and throw some Notices. Add the following to the beginning of the function so that it looks like this:
Code:
<?php
class MY_Router extends CI_Router
{
    function _validate_request($segments)
    {
        if (count($segments) == 0)
        {
            $segments[0] = '';
        }
...

6. (additional) You could define a default language selector based on the browsers language (getenv['HTTP_ACCEPT_LANGUAGE'])

Well I am sorry if this solution is stupid, but it seems to be simple and efficient. Any suggestions?

Cheers,
shueg




Theme © iAndrew 2016 - Forum software by © MyBB