-
Notifications
You must be signed in to change notification settings - Fork 7.6k
i18n Multi language Library Helper
These methods should work from CI 1.7.2 and higher (maybe also lower)
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. For ex. application/languages/dutch/url_lang.php
[size=4][b]This tutorial is about four language things:[/b][/size]
[b]1) Using multi-language websites[/b]
http://domain.com [default language] http://domain.com/nl/...
(Default is redirected to root: if default is dutch and the url is http://domain.com/nl/... the page is redirected to http://domain.com/...)
[b]2) Using multi-language segments[/b]
http://domain.com/news http://domain.com/nl/nieuws
(Default segment is the one of the default language. If a segment for and language is not set/given in the specific language file the default is given. If that one is not present the one given in the code is used.)
[b]3) Using multi-language domains[/b]
http://domain.nl/nieuws http://domain.nl/en/news http://domain.com/news http://domain.com/nl/nieuws
(This sounds strange but imagine if you have many domains (like me) using the same website and it is not so simple to just say .com is english and .nl is dutch and at the same time you want it to be possible to have a multi-language domain (each domain) without changing the domain (because the sites have nothing to do with each other for example))
[b]4) Using multi-language text with parameters[/b]
[code]
// dutch $lang['news'] = "nieuws"; $lang['news_item'] = "nieuws item %s"; $lang['news_items'] = "nieuws item %s en %s";
// english $lang['news'] = "news"; $lang['news_item'] = "news item %s"; $lang['news_items'] = "nieuws item %s and %s";
// if dutch echo lang('news'); // prints: nieuws echo lang('news_item', '', '123'); // prints: nieuws item 123 echo lang('news_items', '', array('123','456')); // prints: nieuws item 123 en 456
// if english echo lang('news'); // prints: news echo lang('news_item', '', '123'); // prints: news item 123 echo lang('news_items', '', array('123','456')); // prints: news item 123 and 456
// OR use i18n() to replace lang(), they do the same, just added for name compatibility with i18n_url()
[/code]
[b]Used ideas from:[/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]
[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 (or modules). Automated generated at the bottom of this tutorial...
[b]application/config/config.php[/b]
[code]
// instead "dutch" you can also use "nl_NL" ("english" > "en_UK") etc. // don't forget to change the folder names in languages also // if you want to use this also as http://domain.com/nl_NL/... // don't forget to change the router (\w{2}) to (\w{2}_\w{2})
// 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_languages'] = array('nl' => 'nl', 'uk' => 'en', 'us' => 'en', 'au' => 'en');
// debug if line is not found as [line]
$config['language_debug'] = TRUE;
[/code]
[b]application/libraries/MY_Config.php[/b]
[code]
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Config extends CI_Config {
/**
* Constructor
*
* @access public
*/
function MY_Config()
{
parent::CI_Config();
log_message('debug', "MY Config Class Initialized");
}
// --------------------------------------------------------------------
/**
* I18n URL
*
* Note: 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. A
* second param is added to change the url language.
*
* @access public
* @param string the URI string
* @param string
* @return string
*/
function i18n_url($uri = '', $language = '')
{
global $LANG;
$uri = $LANG->i18n($uri, $language);
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/libraries/MY_Language.php[/b]
[code]
Soon...
[/code]
[b]application/helper/MY_language_helper.php[/b]
Added "param" and "language" to lang() and the new function i18n()
[code]
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
-
Lang
-
Fetches a language variable and optionally outputs a form label
-
Note: Difference with original is the added $param and language
-
@access public
-
@param string the language line
-
@param string the id of the form element
-
@param string
-
@return string */
function lang($line, $id = '', $param = '', $language = '') { $CI =& get_instance(); $line = $CI->lang->line($line, $param, '', $language);if ($id != '') { $line = ''.$line.""; }
return $line; }
// --------------------------------------------------------------------
/**
- I18n
- Fetches a language variable and optionally outputs a form label
- Note: Exact same as new lang(), just added for the name
- @access public
- @param string the language line
- @param string the id of the form element
- @param string
- @return string
*/
function i18n($line, $id = '', $param = '', $language = '') { return lang($line, $id, $param, $language); }
/* End of file MY_language_helper.php / / Location: ./system/application/helpers/MY_language_helper.php */
[/code]
Examples:
[code]
// in the language files dutch and english
$lang['time_met_you'] = "Dit is de %s keer dat ik jouw heb ontmoet"; $lang['time_met_you'] = "This is the %s time I met you";
// in view file // lang() or i18n() are the same functions // param can also be an array to have more params
echo lang('time_met_you', '', '3e'); echo lang('time_met_you', '', '3rd');
// prints if dutch: Dit is de 3e keer dat ik jouw heb ontmoet // prints if english: This is the 3rd time I met you
echo lang('time_met_you', '', '3e', 'en'); echo lang('time_met_you', '', '3rd', 'nl');
// prints if english: Dit is de 3e keer dat ik jouw heb ontmoet // prints if dutch: This is the 3rd time I met you
[/code]
[b]application/helpers/MY_url_helper.php[/b]
Added "language" to current_url() and new function i18n_url()
[code]
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
- I18n URL
- Note: Almost the same as site_url() but each segment is first checked
- if it exists in the language files with an "url_" prefix, if not then
- the given segment is used. A second parameter is added to change the
- language in the url.
- @access public
- @param string
- @param string
- @return string */ function i18n_url($uri = '', $language = '') { $CI =& get_instance(); return $CI->config->i18n_url($uri, $language); }
// ------------------------------------------------------------------------
/**
-
Current URL
-
Note: Same as original but param added to change url to specified language
-
@access public
-
@param string
-
@return string */ function current_url($language = '') { $CI =& get_instance();
if ($language == '') { return $CI->config->site_url($CI->uri->uri_string()); } else { return $CI->config->i18n_url($CI->uri->uri_string(), $language); } }
/* End of file MY_url_helper.php / / Location: ./system/application/helpers/MY_url_helper.php */
[/code]
Examples:
[code]
// in the language files
$lang['url_test'] = "test123";
// in view file
echo i18n_url('test'); echo site_url(i18n('url_test')); echo site_url(i18n_url('test'));
// prints: test123
// REMEMBER that functions like anchor, anchor_popup, form_open etc. // use site_url() and therefore if you want to translate the url segments // you have to use these functions like this:
echo anchor(i18n_url('test'), 'Link'); echo anchor(i18n('url_test'), 'Link');
// prints: Link
echo anchor(i18n_url('test/test/test'), 'Link'); echo anchor(i18n_url(array('test','test','test')), 'Link'); echo anchor(i18n('url_test').'/'.i18n('url_test').'/'.i18n('url_test'), 'Link');
// prints: Link
echo anchor(i18n_url('test/test/test'), 'Link'); echo anchor(i18n_url(array('test','test','test')), 'Link'); echo anchor(i18n('url_test').'/'.i18n('url_test').'/'.i18n('url_test'), 'Link');
// prints: Link
[/code]
...........
[b]application/languages/dutch/url_lang.php[/b]
[code]
$lang['url_forum'] = "prikbord"; $lang['url_general'] = "algemeen";
[/code]
[b]application/languages/english/url_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.
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 (when i18n_url() is used.
....
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...