Welcome Guest, Not a member yet? Register   Sign In
gettext library
#1

[eluser]Elianora la blanche[/eluser]
Hi !

I found this article in the wiki : gettext and choose the first method by Tassimo, I created the locale dirs and lang.po/lang.mo files

I put that in my controller :
Code:
$this->lang->load_gettext('fr_FR');
and that in my view :
Code:
<h1>&lt;?php echo $this->lang->line('wcp_welcome_text'); ?&gt; CodeIgniter</h1>


here is my lang.po content :
Code:
msgid "welcome_text"
msgstr "Bienvenue sur le site de test de"

msgid "test_by"
msgstr "Les tests ici présents sont réalisés par"

msgid "user_mgt"
msgstr "Gestion des utilisateurs"

the problem is it prints nothing

thanks for help and sorry for my english
#2

[eluser]xwero[/eluser]
I don't see a method in the library that outputs a line and the
Code:
$this->gettextbis->line('messSingle', array('plural' => 'messPlur', 'count' => 2, 'd' => 18));
is something that comes out of thin air.

You have to correct the class if you want to work with it.
#3

[eluser]Elianora la blanche[/eluser]
in the wiki page is written
Quote:It’s based on the original CI_Language library, and doesn’t replaces the original line metho
the basic line function should work...

gettextbis is in the second method with another library... or maybe I didn't understand all ^^
#4

[eluser]xwero[/eluser]
I didn't check the zip attached to the wiki page. There the Gettextbis method needs to renamed to MY_Language and then it should work. I guess he named the library like that and forgot to change the constructor when he ported it to CI.
#5

[eluser]Elianora la blanche[/eluser]
I corrected the class like this :
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Gettext management library with plural and dynamic allocation
*
* This class extends the original language library of Code Igniter.
* It has been implemented from Tassoman classes. You can use plural and ask for dynamic allocation of values.
*
* This is Free Software released under GNU LGPL license, because Code Igniter isn't released under a fully Free license.
*
* 2006 November, 9th
* The authors are Tassoman <[email protected]>
*                 Tchinkatchuk <mail by forum>
*
*/
class MY_Language extends CI_Language {

    
    /**
     * Array of domains loaded
     *
     * @access private
     * @var    array
     */
    var $_aDomains;
    
    /**
     * Path of gettext directory
     *
     * @access private
     * @var    string
     */
    var $_sGetTextPath;
    
    /**
     * Last domain loaded
     *
     * @access private
     * @var    string
     */
    var $_sCurrentDomain;
    
    /**
     * Does it a run default translation
     *
     * @access private
     * @var    bool
     * @see    method translate
     */
    var $_bRunDefault;
    
    
    /**
     * Let's start rock 'n roll from the constructor.
     *
     */
    function MY_Language() {
        
        parent::CI_Language();
        
        $this->_bRunDefault = false;
        $this->_aDomains    = array();
        
        $this->setLanguage();
        $this->_loadPath();
        $this->loadFile();
        
        log_message('debug','GETTEXT Lib: initialized');
    }


    /**
     * Set language that will be used for translation
     *
     * @final
     * @access public
     * @param  string $sLanguage
     */
    function setLanguage($sLanguage = '') {
        
        if ( $sLanguage == '' ) {
            
            $CI =& get_instance();
            $this->userlang = $CI->config->item('getTextLanguage');
        }
        setlocale(LC_MESSAGES, $this->userlang);
    }
    
    
    /**
     * Load a new translation file
     *
     * @final
     * @access public
     * @param  string $sDomain
     */
    function loadFile($sDomain = '') {
        
        if ( $sDomain == '' ) {
            
            $CI =& get_instance();
            $this->_sCurrentDomain = $CI->config->item('getTextDefaultDomain');
        }
        bindtextdomain($this->_sCurrentDomain, $this->_sGetTextPath);
        textdomain($this->_sCurrentDomain);
        $this->_aDomains[] = $this->_sCurrentDomain;
        log_message('debug', 'GETTEXT Lib: domain is set: '. $this->_sCurrentDomain);
    }
    

    /**
     * Load the translation path
     *
     * @final
     * @access private
     * @param  string $sDomain
     */
    function _loadPath() {

        $CI =& get_instance();

        setlocale(LC_MESSAGES, $this->userlang);

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

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

    /**
     * The translator method
     *
     * @param  string $original
     * @return string $sTranslate
     */
    function translate ( $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);
            }
        }
        
        
        /*
         * If no trad were found in the language given in parameter
         * get with default language : en_EN
         */
        if ( $original == $$sTranslate && $this->_bRunDefault === false ) {
            
            $this->_bRunDefault = true;
            setlocale(LC_MESSAGES, 'en_GB');
            return $this->translate($original, $aParams);
        }else if ($original == $sTranslate) {
            $this->_bRunDefault = false;
            return false;
        }
        return $sTranslate;
    }
    
    

    
    /**
     * Allow dunamic 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);
    }
}
?&gt;

now I get a fatal error :
Quote:Fatal error: Call to undefined function get_instance() in /home/elianora/public_html/codeigniter/system/application/libraries/MY_Language.php on line 83
Sad

here is the line :
Code:
$CI =& get_instance();

in system/libraries/Language.php we can find the same line and it seems to work...
#6

[eluser]Truong Chuong DUong[/eluser]
I just make a library which can auto translate all text between {t} and {/t} in the view, I posted here in case some one want to use it instead calling the gettext function in the view:

http://www.chuongduong.net/page/15/codei...-view.html

The view code might be:

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;{blog_title}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>{blog_heading}</h3>
{blog_entries}

<h5>{t}Title is{/t}  {title}</h5>

<p>{t 1="<b>" 2="</b>"}Click here %1to see%2 me{/t}{body}</p>

<p>{t 1="{id}" 2="author"}The id is: %1 wrote by %2{/t}</p>

<p>{t 1="<a >" 2="</a>"}Please lick on me%2{/t}</p>

{/blog_entries}

&lt;/body&gt;

&lt;/html&gt;





Theme © iAndrew 2016 - Forum software by © MyBB