Sometimes you may want to pass variables from a template to a language call:
Like have a string output:
"Welcome Idealists to VldCrowd.com! You can post up to 5 blog posts today."
Where:
Idealists is a username
VldCrowd.com is app_title
5 is some limit set somewhere (daily_blog_post_limit)
Currently it is not possible to do such language string manipulation within a tpl file itself.
All that is required for this example, is a language definition:
"vldcrowd_welcome_message" => //MOD
"Welcome %1% to %2%! You can post up to %3% blog posts today", //MOD
And then within the tpl file you would call the language string normally except now you pass the variables as such (variable 1 to 3, from left to right):
{lang:"custom","vldcrowd_welcome_message",session.username,settings.app_title, session.daily_blog_post_limit}
Only change is to replace (backup original ofcourse) the function vldext_lang within your ext.lang.php which is under /includes/ext/core with:
function vldext_lang ($mod, $key, $value1 = "", $value2 = "", $value3 = "")
{
global $LANG;
$language_string = isset($LANG[$mod][$key]) ? nl2br($LANG[$mod][$key]) : $key;
$variables = array("%1%", "%2%", "%3%");
$values = array("$value1","$value2","$value3");
$language_string = str_replace($variables, $values, $language_string);
return $language_string;
}
Also this will output the variable name instead of "Unknown field" if the field isn't found within the language definition. A bit more user friendly, and if you name your fields well, it should mean something to a user.
You can pass a max of 3 variables (if you know PHP you can easily increase it)
If you only need to pass 1 variable then it would be like:
{lang:"custom","vldcrowd_welcome_message",session.username}
If you don't need to pass variable it would be the same as old days, like:
{lang:"custom","vldcrowd_welcome_message"}
So no need to change all language calls.





Comments
radioact
Log in to leave a comment