-
Notifications
You must be signed in to change notification settings - Fork 0
i18n Multi language Library Helper
[size=4][b]Not yet finished !!![/b][/size]
This tutorial is about three language things:
- Using multi-language websites
http://domain.com [default language] http://domain.com/nl http://domain.com/de
- Using multi-language segments
http://domain.com/news http://domain.com/nl/nieuws http://domain.com/de/
- Using multi-language domains
...
[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]
// 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]
// default language
#$config['language'] = 'dutch';
$domains = array( 'domain.nl' => 'dutch', 'domain.com' => 'english' );
$host = implode('.', array_slice(explode('.', $_SERVER['HTTP_HOST']), -2));
if (key_exists($host, $domains)) { $config['language'] = $domains[$host]; } else { $config['language'] = 'dutch'; }
// available languages (key: language code, value: language name)
$config['languages'] = array('nl' => 'dutch', 'en' => 'english');
// available countries (key: country code, value: language code)
$config['country_codes'] = array('nl' => 'nl', 'uk' => 'en', 'us' => 'en', 'au' => 'en');
[/code]
[b]application/libraries/MY_Config.php[/b]
Needed for "multi-language segments"
[code]
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Config extends CI_Config {
/**
* Site URL
*
* Note: First I renamed the function to i18n_url() but
* site_url() is used all over other functions as well.
* There is only one difference with the original site_url()
* Each segment is first checked if it exists in the language
* files with an "url_" prefix if not then the given segment
* is used.
*
* @access public
* @param string the URI string
* @param bool en-/disable lang
* @return string
*/
function site_url($uri = '', $lang = TRUE)
{
if ( ! is_array($uri))
{
$uri = explode('/', $uri);
}
if ($uri == '' || $lang == FALSE)
{
$uri = implode('/', $uri);
return parent::site_url($uri);
}
$CI =& get_instance();
$segments = array();
foreach ($uri as $segment)
{
$line = $CI->lang->line('url_' . $segment);
$segments[] = ($line == NULL) ? $segment : $line;
}
$uri = implode('/', $segments);
return parent::site_url($uri);
}
}
// END MY_Config Class
/* End of file MY_Config.php / / Location: ./system/application/libraries/MY_Config.php */
[/code]
[b]application/languages/dutch/urls_lang.php[/b]
[code]
$lang['url_forum'] = "prikbord"; $lang['url_general'] = "algemeen";
[/code]
[b]application/languages/english/urls_lang.php[/b]
[code]
$lang['url_forum'] = "forum"; $lang['url_general'] = "general";
[/code]
When you begin coding the line "url_forum" without url_ is the same as text "forum". But later in your project when the line is used everywhere you can still change the text by simply change it in the language-file.
[b]application/helpers/MY_url_helper.php[/b]
[code]
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
- I18n URL
- Create a local URL based on your basepath. Segments can be passed via the
- first parameter either as a string or an array. The only difference with
- site_url() is that i18n_url() will try to get the segments from your
- language url_lang.php files. If one or more segments do not exists in these
- files, the segments which are not found the ones you entered are used.
- Note: If you don't want to rename all site_url() functions to i18n_url(),
- just rename the function i18n_url() below to site_url().
- @access public
- @param string
- @return string */ function i18n_url($uri = '') { $CI =& get_instance(); return $CI->config->i18n_url($uri); }
/* End of file MY_url_helper.php / / Location: ./system/application/helpers/MY_url_helper.php */
[/code]
[b]application/helper/i18n_helper.php[/b]
[code]
function i18n ($item, $param = null) { $CI =& get_instance();
$line = $CI->lang->line($item);
if ($line == null)
{
log_message('error', 'i18n: unknown item [' . $item . '] in ' . $_SERVER['REQUEST_URI']);
$line = "{" . $item . "}";
}
if ($param == null)
{
return $line;
}
else
{
if (is_array($param)) return vsprintf($line, $param); else return sprintf($line, $param);
}
}
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]
i18n() is a replacement for lang() and does almost the same. Parameter is added to do the folowing:
[code]
// in language file
$lang['time_met_you'] = "This is the % time I met you";
// in view file
echo i18n('time_met_you', '3rd'); // produces: This is the 3rd time I met you
[/code]
i18n() is used for text (not urls) but you can if you would like to use it for urls also. If the line is not found it produces {line} to identify the text is missing. (In the future I would like to rebuild this to show always the default language if line is not found instead of the {line} text.
- i18n_url('forum')
- i18n_url('forum/general')
- i18n_url(array('forum','general'))
In dutch produces same as:
- site_url('prikbord')
- site_url('prikbord/algemeen')
- site_url('prikbord/algemeen')
In english produces same as:
- site_url('forum')
- site_url('forum/general')
- site_url('forum/general')
If a "segment" does not exist, the name used to call the segment is used.
- Original author: Derek Jones
- How to extend helpers: See User Guide
- Modified by: Thomas Stapleton (id, classes, selected country option and all option)
- Modified by: Bradley De-Lar (construct, setLayout example)