GeoffEll Posted March 22, 2008 Share Posted March 22, 2008 Dear PHP freaks, I have the following minimal example of my code: ----------------------------------------- if ($Language == 0) {$Help = "Help Me";} if ($Language == 1) {$Help = "Hilfe Mir";} if ($Language == 2) {$Help = "Aidez Moi";} ------------------------------------------ And I would like to simplify things by making a function, say called translate, so that in the future all I need type in is... ------------------------------------------ translate($Help, "Help Me", "Hilfe Mir", "Aidez Moi"); ------------------------------------------ But something always seems to go wrong. Does anyone have any ideas? Many thanks in advance, Geoff. Link to comment https://forums.phpfreaks.com/topic/97425-how-to-set-a-variable-within-a-function-macro/ Share on other sites More sharing options...
Barand Posted March 23, 2008 Share Posted March 23, 2008 I'd advise against hard-coding translations in the program. Better to store them outside. If you don't have a database use a text file. Then you can add phrases without changing the code One way would be :: test01.txt :: [help me] 1=hilfe mir 2=aidez moi [excuse me] 1=enschuldigan sie bitte 2=excusez moi then <?php $translations = parse_ini_file('test01.txt', true); echo translate ('help me', 2); function translate ($str, $lang) { global $translations; if ($lang==0 || !isset($translations[$str])) return $str; else return $translations[$str][$lang]; } ?> Link to comment https://forums.phpfreaks.com/topic/97425-how-to-set-a-variable-within-a-function-macro/#findComment-498513 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.