CodeIgniter Forums

Full Version: Character encoding using lang()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]rossmurphy[/eluser]
Hi all,

I am using the language helper for a multilingual site. I am finding that in my language files i have to use this:

Code:
$lang['label.category.payment'] = '<span class="bold">Tarjeta de Cr&eacute;dito y D&eacute;bito</span>';

Instead of this:

Code:
$lang['label.category.payment'] = '<span class="bold">Tarjeta de Crédito y Débito</span>';

However, if in the language line i don't used html i find that i dont have to character encode the text. But as you can see from the first example above, i have to encode the special chars.


Has anyone run into this problem?

El Forum

[eluser]Isern Palaus[/eluser]
Hello,

Yo he tenido este problema, y no tengo muy claro que es... En una aplicación sin especificar nada si ponia accentos y ñ no habia problemas. En otra tenia que usar &aacute; &ntilde; etc. Dependerá del charset imagino :S

Un saludo,
Isern

El Forum

[eluser]rossmurphy[/eluser]
Not sure if your trying to make a point or explain something. Sorry forgot to mention, i don't speak spanish.

Thanks Though.

El Forum

[eluser]demogar[/eluser]
He just told that could be the charset.

I had the same problem before and what I did is to only use text in my language files. For example, in my view I used something like:
Code:
View:
<strong><php echo lang('my.lang'); ?&gt;</strong>

Lang:
$lang['my.lang'] = 'Hello';

I think its a better practice too.

El Forum

[eluser]rossmurphy[/eluser]
I extended the lang library so that i could pass in vars and return a line with html. It also takes care of the problem i was having with special chars and html in the same lang line.

Let me know how it could be improved.

Code:
&lt;?php
class MY_Language extends CI_Language{
    
    function line($lang_key, $parameters = NULL){

        $lang_return = parent::line($lang_key);
        
        if($lang_return === false) {
            return "???$lang_key???";
        } else {
        
            // parse variables if passed
            if(!is_null($parameters)) {
                $return_line = $this->parse_lang_vars($lang_return, $parameters);
            }
                
            // set translation table
            $translation_table = get_html_translation_table(HTML_ENTITIES);
            
            // encode trans line
            $encoded_line = strtr($return_line, $translation_table);
            
            // decode html elements
            $line = htmlspecialchars_decode($encoded_line);
            
            return $line;
            
        }
    }
    
    function parse_lang_vars($lang_return, $parameters) {
        
        // check for array
        $parameters = is_array($parameters) ? $parameters : array($parameters);
        
        foreach($parameters as $key => $value) {
            $line = str_replace('{'.$key.'}', $value, $lang_return);
        }

        return $line;
                
    }
    
}

El Forum

[eluser]cahva[/eluser]
I might not understood the actual problem but are you sure you have the right encoding in your language file? I mean for example if you have set the encoding to UTF-8, have you also saved your lang file as UTF-8?

El Forum

[eluser]rossmurphy[/eluser]
I checked my lang file and it wasn't UTF-8. Thanks a lot. That solved my issue. So here is the revised code without the trans table.

Code:
&lt;?php
class MY_Language extends CI_Language{
    
    function line($lang_key, $parameters = NULL){

        $lang_return = parent::line($lang_key);
        
        if($lang_return === false) {
            return "???$lang_key???";
        } else {
        
            // parse variables if passed
            if(!is_null($parameters)) {
                $line = $this->parse_lang_vars($lang_return, $parameters);
            }
            
            return $line;
            
        }
    }
    
    function parse_lang_vars($lang_return, $parameters) {
        
        // check for array
        $parameters = is_array($parameters) ? $parameters : array($parameters);
        
        foreach($parameters as $key => $value) {
            $line = str_replace('{'.$key.'}', $value, $lang_return);
        }

        return $line;
                
    }
    
}