EllisLab text mark
Advanced Search
     
Using variables in Language files
Posted: 13 February 2010 08:19 PM
Joined: 2009-09-21
46 posts

Hi there!

I was wondering if it’s possible to display variables in the language files.
For example:

<?php

$lang[
'unread_messages'"You have ".$this->my_model->unread_messages()." unread messages";

?> 

Can anyone point me in the right direction? smile
Would appreciate it!

Kind regards,

Philo

 
Posted: 14 February 2010 03:10 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-07-20
262 posts

You can use placeholders like this:

$lang['unread_messages'"You have %s unread messages"

Then when calling it, use:

$number $this->my_model->unread_messages();
$message sprintf($this->lang->line('unread_messages'), $number); 
 Signature 

I heard that big fonts are in this summer? All the cool kids seem to be doing it!  | Mario Visic - Freelance Web Programmer | HumorList - It’s got stripes!

 
Posted: 02 May 2010 04:31 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2010-01-28
3 posts

cro_crx, you can certainly do that but differences in language grammar might result in the variable positions changing.

What I did was overload the line() function in the Language class to allow number-based arguments:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Language extends CI_Language
{

    
function MY_Language()
    
{
        parent
::CI_Language();
    
}
    
    
/**
     * Fetch a single line of text from the language array. Takes variable number
     * of arguments and supports wildcards in the form of '%1', '%2', etc.
     * Overloaded function.
     *
     * @access public
     * @return mixed false if not found or the language string
     */
    
public function line()
    
{
        
//get the arguments passed to the function
        
$args func_get_args();
        
        
//count the number of arguments
        
$c count($args);
        
        
//if one or more arguments, perform the necessary processing
        
if ($c)
        
{
            
//first argument should be the actual language line key
            //so remove it from the array (pop from front)
            
$line array_shift($args);
            
            
//check to make sure the key is valid and load the line
            
$line = ($line == '' OR ! isset($this->language[$line])) ? FALSE $this->language[$line];
            
            
//if the line exists and more function arguments remain
            //perform wildcard replacements
            
if ($line && $args)
            
{
                $i 
1;
                foreach (
$args as $arg)
                
{
                    $line 
preg_replace('/\%'.$i.'/'$arg$line);
                    
$i++;
                
}
            }
        }
        
else
        
{
            
//if no arguments given, no language line available
            
$line false;
        
}
        
        
return $line;
    
}
    
    
}

?> 


It works the same way as the original function. Simply put MY_Language.php into your application/libraries folder and you can do stuff like this:

//in english
$lang['some_key''Some string with %1 %2.';

//in another language
$lang['some_key''Some string %2 with %1.';

//the actual usage
echo $this->lang->line('some_key','first','second'); 


Hope this helps. smile

 
Posted: 12 May 2010 08:16 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-10-18
334 posts

Interesting, thanks for sharing. I had this problem once and had to change my redaction =s

 Signature 

Wallpapers and Images Site: Desktop Wallpapers

 
Posted: 11 June 2010 09:12 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-01-14
31 posts

Regarding internationalisation, the quicker solution is to use php’s ability argument swapping functionality in the sprintf function (see http://php.net/manual/en/function.sprintf.php)

//in english
$lang[‘unread_messages’] = “You have %1$s unread messages, %2$s”;

//in another language
$lang[‘unread_messages’] = “Hi %2$s, You have %1$s unread messages”;

$message = sprintf($this->lang->line(‘unread_messages’), $number, $name);

 Signature 

Judd Muir
juddmuir.com
Yabadoo!
BookaBed

 
Posted: 23 June 2010 11:08 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2010-01-28
3 posts

Yes, but then you would be writing sprintf($this->lang->line(.....)) every single time.

Overload once, use as normal. Easier. wink

 
Posted: 01 March 2011 10:50 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2010-02-02
13 posts

Will this work for Codeigniter Reactor 2.0 version too?