tags:

views:

44

answers:

4

Hi!

I use smarty to allow different languages on my site, which works OK so far. I store the texts in config files in different sections.

But then there are sentences like this:

"You have 6 new mails!", which would be in german "Sie haben 6 neue Mails!"

Now there's text before the number and behind the number, which is loaded from the database. And I would like to put it into the config file and just load the number on its own.

so I have this in my "text.conf"

[en]
mail_count = "You have $NUMBER new mails!"
[de]
mail_count = "Sie haben $NUMBER neue Mails!"

and this in my "show_text.php"

$smarty->assign('NUMBER', 6);

Is something like this possible? Maybe with Smarty 3.0?

Thanks in advance, BH

A: 

I don't have Smarty to test this right now but it should work if you properly declare the variable in your config entry, like:

ail_count = "You have {$NUMBER} new mails!"
Steve Claridge
Nope, sadly not. I tried this already, the output is "You have {$NUMBER} new mails!"
bastianhuebner
+1  A: 

You can use the sprintf sintax. This example comes from a pager-like thing:

results = "Results %s to %s of %s total"

{#results#|sprintf:$start:$end:$total}
djn
It kind of works, but it is still not the desired effect: You still have to add this sprintf stuff and can't directly give the variable to smarty. A solution I already found is close to this one, but would use "replace" instead of "sprintf". Thanks anyway!
bastianhuebner
+1  A: 

I just tried this and it works, but it's rather ugly...

  • create a file "number.tpl" which contains

{$NUMBER}

  • in your conf file go like this

mail_count = "You have {include file='number.tpl'} new mails!"

I guess it's because the smarty variables only work in tpl files.

Riki
This somehow doesn't work for me at all, the inner include is not executed, I get the text instead "You have {include file='number.tpl'} new mails!"
bastianhuebner
what I am doing is running the show_text.php file in a firefox browser. Are you doing that?
Riki
A: 

When reading the config file, you need to open it using

$cfg = $smarty->fetch('path/to/file');

After that you have the whole files content ind the $cfg variable, with {$NUMBER} replaced.

JochenJung