Variables in language file...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Variables in language file...

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:? Not sure I'm understanding...
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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
Post Reply