gettext problem in codeigniter

2.1k views Asked by At

I added one library for gettext translation. Added corresponding po and mo files.

And the translation is working fine.

Now when I update my po file, change some translation.. after that when I reload the page, I am getting the old translation, not the new.

Here is the Library code:

/**
 * 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;
}

This is the usage in a controller:

$this->pos_language->load_gettext('fr_FR');
echo $this->pos_language->_trans('Hello world, good morning');
2

There are 2 answers

1
jfoucher On BEST ANSWER

I think you need to compile your .po files to .mo files. Gettext uses the .mo file, the .po is just a human readable form.

If you haven't done the compilation step, your application is still reading your old .mo files, with the untranslated strings...

This page has some more info about gettext translation: http://wiki.creativecommons.org/Translating_PO_Files

0
damko On

I wrote a helper to integrate php-gettext with Smarty & Code Igniter 2 http://bit.ly/rrITVx

I hope it might help