Skip to content

i18n Multi language Library Helper

World Wide Web Server edited this page Jul 4, 2012 · 59 revisions

Not finished yet!!!

[b]Multi-domain:[/b] http://domain.nl (dutch = nl) http://domain.com (english = en)

[b]Multi-language:[/b] http://domain.nl/en (default dutch = nl, override english = en) http://domain.com/nl (default english = en, override dutch = nl)

[b]Multi-segments:[/b] http://domain.nl/en/news (default dutch = nl, override english = en, english segments) http://domain.com/nl/nieuws (default english = en, override dutch = nl, dutch segments)

In this tutorial I use libraries, helpers, routes, configs, hooks, etc.

HMVC proof to use modules...

[b]Used ideas:[/b]

[url=http://maestric.com/doc/php/codeigniter_i18n]Internationalization (i18n) library by Jérôme Jaglale[/url] [url=http://codeigniter.com/wiki/URI_Language_Identifier/]URI Language Identifier by Wiredesignz[/url]

This library / helper uses the usual way CI stores language-files. Inside the [b]application[/b] folder a folder named [b]languages[/b]. Inside laguages the language folders (dutch english etc.) and inside the language folders the language-files. application/languages/dutch/urls_lang.php

[b]application/config/routes.php[/b]

[code]

$route['^(\w{2})/(.*)$'] = '$2'; $route['^(\w{2})$'] = $route['default_controller'];

// Urls start

$route['^mobiel|^handy|^movil|^cell(/:any)?'] = "mobile$1";

// Urls end

[/code]

The part "Urls" is only for the controllers. In my case I have a database with three tables:

Table "url_languages" > columns: lang_id, lang_ci, lang_cc Table "url_segments" > columns: urlseg_id, urlsec_first (bool), urlsec_line, urlsec_text Table "url_languages" > columns: urllang_id, urlseg_id, lang_id, urllang_text

And I will generate the routes and language-files automaticaly. Later more on this...

[b]application/config/config.php[/b]

[code]

[/code]

[b]application/helper/i18n_helper.php[/b]

[code]

function i18n_url ($item, $param = null) { $CI =& get_instance();

if (is_array($item))
{
    $items = $item;
    
    $segments = array();
    
    foreach ($items as $item)
    {
        $line = $CI->lang->line('url_' . $item);
        
        $segments[] = ($line == null) ? $item : $line;
    }
    
    $line = implode('/', $segments);
}
else if (stripos($item, '/'))
{
    $items = explode('/', $item);
    
    foreach ($items as $item)
    {
        $line = $CI->lang->line('url_' . $item);
        
        $segments[] = ($line == null) ? $item : $line;
    }
    
    $line = implode('/', $segments);
}
else
{
    $line = $CI->lang->line('url_' . $item);
    
    if ($line == null)
    {
        $line = $item;
    }
}

$line = site_url($line);

if ($param == null)
{
    return $line;
}
else
{
    if (is_array($param)) return vsprintf($line, $param); else return sprintf($line, $param);
}

}

[/code]

Clone this wiki locally