Jump to content

Recommended Posts

At the minute i am using variables in my tpl files in place of plain text so users can change the language the page is shown in. eg. the tpl file would include:

$lang->hello;

and the replacement would be

$lang['hello'] = 'hello';

the php file would include the language class which replaces the variable in the language file with the array value in the relevant language file.

but what happens when i have a phrase with a needed variable in the middle?

$lang['msgs'] = 'you have '.$msgs.' messages';

i cant have 'you have' and 'messages' as seperate lang variables as phrases are constructed differently in other languages? so how do i accomplish this? hopefully i habe explained this ok.

Link to comment
https://forums.phpfreaks.com/topic/236440-language-files/
Share on other sites

When creating things like this you can't exactly use plain old echo to display things. You'd need to use sprintf

Here is what you'd do, in your language file you'd have something like this:

 

$lang['msgs'] = 'you have %d messages';

 

Then when you are formatting the text all you need to do is:

$total_messages = 8;
$messages = sprintf($lang['msgs'], $total_messages);

Link to comment
https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1215875
Share on other sites

While sprintf() will work perfectly as ProjectFear suggested, there is one problem with it: type specifiers are meaningless to someone not related to programming. If your projects involves more than just you or you want to have variables with meaning inside languages strings, than you could go for another way. Definitely, sprintf() will work faster, so be advised.

 

The "system" I just wrote involves a function that searches and replaces special variables inside language strings and replaces them with the appropriate value. It looks like this:

 

<?php
function replaceVars ($text, $vars) {
if (!is_array($vars)) {
	$vars = array($vars);	
}

if (preg_match_all('|{(.+?)}|', $text, $matches)) {
	foreach ($matches[0] as $key=>$match) {
		$text = str_replace($match, $vars[$key], $text);
	}
	return $text;
}
}
?>

 

What it does is take 2 parameters: the language string and the variables to be replaced (as string or array). The string is matched against a regular expression, looped through all the matches and replace each one with the supplied variables. The replacement will be incremental, meaning that the first matched occurrence will be replaced with the first element of the variables array.

 

Usage examples:

<?php
//First example: 1 variable to replace
$lang['msgs'] = 'you have {num_msg} messages';
echo replaceVars($lang['msgs'], 10);
//will output: you have 10 messages

//Second example: 2 variables to replace
$lang['msgs'] = 'you have {num_msg} messages in your {folder_name} folder';
echo replaceVars($lang['msgs'], array(10, 'Inbox'));
//will output: you have 10 messages in your Inbox folder
?>

 

While it certainly has it's overhead, I guess it's better giving names to your variables for the translators sake than just %d or %s.

Link to comment
https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1215885
Share on other sites

thanks for the replies. both very good ideas, but i think i will go with the latter. this system is form my forum software so the poeple editing the language files will be users who contribute new languages or users who want to edit certain phrases. therefore i think the variables with meaning will work best.

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1215921
Share on other sites

Using GetText in combination with POEdit allows you to sync your source with your translations. Considering that a forum has lots of text that's the way you want to go in combination with GuiltyGear's advice. If not POEdit find some other program that allows you to sync your source with your translations or write your own, nevertheless manually managing your translations becomes a burden quickly not to mention what happens when you start collaborating.

Link to comment
https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1216159
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.