2

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

bastiandoeen
  • 608
  • 5
  • 10

4 Answers4

1

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
  • 3,950
  • 22
  • 21
  • 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! – bastiandoeen Oct 07 '10 at 13:50
1

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
  • 38
  • 5
  • 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!" – bastiandoeen Oct 11 '10 at 07:48
  • what I am doing is running the show_text.php file in a firefox browser. Are you doing that? – Riki Oct 11 '10 at 20:14
1

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
  • 7,183
  • 12
  • 64
  • 113
0

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
  • 10,650
  • 8
  • 33
  • 35