Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 13 revisions

Category:Internationalization

[h3]Gettext extension of CI_Language library[/h3]

This is an extension library using Gettext for internationalization, based on the original CI_Language library.

[code]<?php

/**

  • This class extends the original language library of Code Igniter
  • It's pushing out a lot of "spam" in logs because is pretty young, so if you have enought of loggin just comment log_message rows.
  • Help is needed to improve gettext implementation
  • You'll need $config['language'] = 'it_IT'; or something similar: en_EN fr_FR
  • This is Free Software released under GNU LGPL license, because Code Igniter isn't released under a fully Free license.
  • 2006 November, 9th
  • The author is Tassoman [email protected]
  • Visit my blog: http://blog.tassoman.com

*/

class MY_Language extends CI_Language {

/**
 * Let's start rock 'n roll from the constructor.
 *
 */
function MY_Language() {
    parent::CI_Language();
    log_message('debug','GETTEXT Lib: initialized');
}

/**
 * Let's load the class as needed... or in autostart if you want.
 *
 */
function load () {

    $CI =& get_instance();

    $CI->load->library('session');
        $v = $CI->session->userdata('language');
    if( isset($v) && $v != '' ) {
        $this->userlang = $CI->session->userdata('language');
        log_message('debug', 'GETTEXT Lib: userlang from session: '. $this->userlang);
    }
    else {
        $this->userlang = $CI->config->item('language');
        $CI->session->set_userdata(array('language'=> $this->userlang));
        log_message('debug', 'GETTEXT Lib: userlang from config: '. $this->userlang);
        log_message('debug', 'GETTEXT Lib: language stored in session');
    }

    putenv("LANG=$this->userlang");
    setlocale(LC_ALL, $this->userlang);

    $v = $CI->config->item('gettext_path');
    if( isset($v) && $v != '' ) {
        $this->gettext_path = $CI->config->item('gettext_path');
        log_message('debug', 'GETTEXT Lib: gettext_path from config');
    }
    else {
        $this->gettext_path = APPPATH.'language/gettext';
        log_message('debug', 'GETTEXT Lib: gettext_path default: applications/language/gettext');
    }

    $this->domain = 'lang';
    bindtextdomain($this->domain, $this->gettext_path);
    textdomain($this->domain);
    log_message('debug', 'GETTEXT Lib: domain is set: '. $this->domain);

}

/**
 * This is a placeholder for the translate method, the real translator.
 *
 * @param string $line
 * @return string translated
 */
function line( $line ) {
    return $this->translate( $line );
}

/**
 * The translator method
 *
 * @param string $original
 * @return string translated
 */
function translate ( $original ) {
    $translated = gettext( $original );
    return $translated;
}

}

?>[/code]

Clone this wiki locally