Gettext support
The following are two extension libraries using Gettext for internationalization instead of variables set in the language file.
We’re implementing by our own two kinds of Gettext support for Code Igniter. One approach is based as extension library and it made by Tassoman. The other approach is based as Library overwrititing.
Gettext extension of CI_Language library
It’s based on the original CI_Language library, and doesn’t replaces the original line method. You need to call it in your controller: $this->lang->load_gettext(‘it_IT’). You don’t need of adding any string of configuration in the config.php file.
Installation
You must create a directory called locales under your application/languages/, with each locale directory (for example it_IT); Then add another subdir called LC_MESSAGES for each locale langdir.
In those LC_MESSAGES directories you must put your lang.po lang.mo files.
application/language
locale
it_IT
LC_MESSAGES
lang.po
lang.mo
To deal with PO and MO files, please use the easy multi platform POEDIT free software.
Source code
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Code Igniter Gettext Extension library
*
* This Library overides the original CI's language class. Needs the $config['language'] variable set as it_IT or en_EN or fr_FR ...
*
* @package Gettext Extension
* @author Pierluigi Tassi
* @copyright Copyright (c) 2006, Bologna Informatica di Pierluigi Tassi.
* @license http://www.gnu.org/licenses/lgpl.txt
* @link http://www.bolognainformatica.com
* @version Version 0.3
* @since 2006 November, 9th
*/
// ------------------------------------------------------------------------
class MY_Language extends CI_Language {
var $gettext_language;
var $gettext_domain;
var $gettext_path;
/**
* The constructor initialize the library
*
* @return MY_Language
*/
function MY_Language() {
parent::CI_Language();
$this->gettext_domain = 'lang';
log_message('debug','Gettext Class initialized');
}
/**
* This method overides the original load method. Its duty is loading the domain files by config or by default internal settings.
*
*/
function load_gettext ( $userlang = false ) {
/* I want the super object */
if($userlang ) $this->gettext_language = $userlang;
else $this->gettext_language = 'it_IT';
log_message('debug', 'Gettext Class gettext_language was set by parameter:' . $this->gettext_language );
putenv("LANG=$this->gettext_language");
setlocale(LC_ALL, $this->gettext_language);
/* Let's set the path of .po files */
$this->gettext_path = APPPATH.'language/locale';
log_message('debug', 'Gettext Class path chosen is: '.$this->gettext_path);
bindtextdomain($this->gettext_domain, $this->gettext_path);
textdomain($this->gettext_domain);
log_message('debug', 'Gettext Class the domain chosen is: '. $this->gettext_domain);
return true;
}
/**
* Plural forms added by Tchinkatchuk
* http://www.codeigniter.com/forums/viewthread/2168/
*/
/**
* The translator method
*
* @param string $original the original string to translate
* @param array $aParams the plural parameters
* @return the string translated
*/
function _trans( $original, $aParams = false ) {
if ( isset($aParams['plural']) && isset($aParams['count']) ) {
$sTranslate = ngettext($original, $aParams['plural'], $aParams['count']);
$sTranslate = $this->replaceDynamically($sTranslate, $aParams);
}
else{
$sTranslate = gettext( $original );
if (is_array($aParams) && count($aParams) ) $sTranslate = $this->replaceDynamically($sTranslate, $aParams);
}
return $sTranslate;
}
/**
* Allow dynamic allocation in traduction
*
* @final
* @access private
* @param string $sString
* @return string
*/
function replaceDynamically($sString) {
$aTrad = array();
for ( $i=1, $iMax = func_num_args(); $i<$iMax; $i++) {
$arg = func_get_arg($i);
if (is_array($arg)) {
foreach ($arg as $key => $sValue) {
$aTrad['%'.$key] = $sValue;
}
}
else {
$aTrad['%'.$key] = $arg;
}
}
return strtr($sString, $aTrad);
}
}
?>
Errors
If you get the following error:
Fatal error: Call to undefined function bindtextdomain() in /.../system/application/libraries/xxx.php on line 53
Please check that Gettext support is correctly installed into your PHP server.
Discuss
If you want, iscuss about it in the forums
Gettext translation by overwriting method
This library allow plural translation and dynamic allocation of values in the translate string
You will have to add this in your config file
$config['getTextPath'] = 'your domain path (ex : /var/www/system/application/language/locale/)';
$config['getTextLanguage'] = 'your default language (ex : en_GB)';
$config['getTextDefaultDomain'] = 'yourdomain';
————————————————————————————————————————————————————————————————
The po file could contain this kind of text
msgid “messSingle”
msgid_plural “messPlur”
msgstr[0] “o;nt unread message in %d folders”
msgstr[1] “o;unt unread messages in %d folders”
————————————————————————————————————————————————————————————————
if you call it like this
$this->gettextbis->line('messSingle', array('plural' => 'messPlur', 'count' => 2, 'd' => 18));
it will return
2 unread messages in 18 folders
————————————————————————————————————————————————————————————————
