doddsey_65 Posted May 15, 2011 Share Posted May 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236440-language-files/ Share on other sites More sharing options...
doddsey_65 Posted May 16, 2011 Author Share Posted May 16, 2011 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1215867 Share on other sites More sharing options...
JasonLewis Posted May 16, 2011 Share Posted May 16, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1215875 Share on other sites More sharing options...
Fadion Posted May 16, 2011 Share Posted May 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1215885 Share on other sites More sharing options...
doddsey_65 Posted May 16, 2011 Author Share Posted May 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1215921 Share on other sites More sharing options...
ignace Posted May 16, 2011 Share Posted May 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236440-language-files/#findComment-1216159 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.