Page 1 of 1
Variables in language file...
Posted: Tue Mar 07, 2006 11:17 pm
by neophyte
I have separated my language into a file. The problem is that a line like:
Code: Select all
$lang->something = 'something'.$foo.'nothing'.$bar;
This will produce undefined variable notices/warnings.
What is a good solution to this problem?
The simple solution is to just make a language variable for 'something' or 'nothing' but in more complex language this can be impossible.
Is there a solution to this problem?
Posted: Tue Mar 07, 2006 11:31 pm
by feyd

Not sure I'm understanding...
Posted: Tue Mar 07, 2006 11:51 pm
by neophyte
I'll try again.
I've separated my language out from my code. These language variables are all present/available during execution regardless of whether they need to be or not. (Much like the language file in phpBB). This works great except when a string/sentence needs to have additional variables inserted into it. What if the following were inserted in a language file:
$lang = "There are $six $members on line at $somedomain."
$six, $members, $somedomain are not always defined so PHP spits out a undefined variable warining/notice. I could make language file entries for every non-variable word/phrase ($lang['there'] = 'there', $lang['members']='members' ... and so forth). But even if I did this sentence structure might not work in another language.
I hope this explains the problem.
Has anyone else encountered this problem, how did you solve it?
Posted: Wed Mar 08, 2006 12:00 am
by feyd
You could use template variable replacement techniques. i.e.
Code: Select all
$lang['some_foo'] = 'There are {number} {descriptor} on line at {domain}.';
with some str_replace() scheme.
Posted: Wed Mar 08, 2006 9:25 pm
by neophyte
Here's my solution to the problem...
Code: Select all
function lang_var_insert( $array, $subject ){
foreach($array as $key => $val ){
//preg_replace or str_replace will work.
//$string = str_replace('{'.$key.'}', $val, $subject );
$string = preg_replace('#{{1}('.$key.')}{1}#i', $val, $subject);
$subject = $string;
}
return $string;
}
usage:
Code: Select all
$lang['blab'] = 'some boring {dribble} about {nothing}';
echo lang_var_insert( array('dribble' => 'talk', 'nothing' => 'something'), $lang['blab']);
Should output:
some boring talk about something