0

My question looks almost the same as this Is it possible to put variables inside config files?

I have a config file that controls the language. Also I have a feedback div that is showed for the feedback.

Now I want to assign feedback like this:

$smarty->assign('feedback', 'the_age_of_user_' . $user->name . '_is_changed_to_' . $user->age . '_years');

nl.conf

the_age_of_user_%s_is_changed_to_%s_years = De leeftijd van gebruiker %s gewijzigd naar %s jaar

en.conf

the_age_of_user_%s_is_changed_to_%s_years = The age of user %s is changed to %s years

Does anyone know how I can accomplish this? Or is there a better solution to assign variables to the config file?

Community
  • 1
  • 1
Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82

1 Answers1

0

Sorry, but I don't completely understand what are you trying to accomplish.

In our project, we have following solution for similar problem:

Language file:

the_age_of_user_is_changed = The age of user {user} is changed to {year} years

Notice that there is no %s or any variables on left side. It is always constant.

Option 1:

  1. Smarty/code assigment: $smarty->assign("VARIABLE_TO_USE_IN_TEMPLATE", getLang('the_age_of_user_is_changed', array('user' => $user->name,'year' => $user->age));

  2. Then, in Smarty, you can use following code: {$VARIABLE_TO_USE_IN_TEMPLATE}

Function getLang reads string information from language files and replaces all {symbol} to corresponding data.

Option 2:

  1. In template, we have smarty userspace function: {LANG key='the_age_of_user_is_changed' name=$user->name year=$user->age} Lang is actually calls function getLang from previous example.

  2. In code you have to assign user: $smarty->assign("user", $user);. After that, smarty will do everything by itself.

Oleg Liski
  • 563
  • 4
  • 15
  • Thanks, but in this way it is not dynamic. Feedback is used for all my applications feedback. So for example `File not found` does not have any parameters. – Ron van der Heijden Oct 25 '12 at 09:39
  • Sorry, but what do you mean "not dymanic" ? When you decide which feedback to give - at this point you should know what are variables for current answer. At this point, when you know variables, you can already generate whole feedback string and save it in variable $feedback, for example. Later you can assign this variable to smarty. There is no other options, if list of variables is completely random. – Oleg Liski Oct 25 '12 at 09:45