Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return variable values in codeigniter language line strings?

To be more clear, none of these lines in default language's general_lang.php work:

$lang['general_welcome_message'] = 'Welcome, %s ( %s )';

or

$lang['general_welcome_message'] = 'Welcome, %1 ( %2 )';

I expect an output like Welcome, FirstName ( user_name ).

I followed the second (not accepted) answer at https://stackoverflow.com/a/10973668/315550.

The code I write in the view is:

<div id="welcome-box">
    <?php echo lang('general_welcome_message',
               $this->session->userdata('user_firstname'),
               $this->session->userdata('username')
               );
    ?>
</div>

I use codeigniter 2.

like image 403
Mohammad Naji Avatar asked Feb 27 '26 07:02

Mohammad Naji


1 Answers

You will need to use php's sprintf function (http://php.net/manual/en/function.sprintf.php)

Example from http://ellislab.com/forums/viewthread/145634/#749634:

//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);
like image 96
Luc Avatar answered Mar 01 '26 02:03

Luc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!