etrader Posted August 12, 2011 Share Posted August 12, 2011 I want to create a multi-language website. For translating the terms, I have two options. For using define() for each sentence; e.g. DEFINE('WORD1', 'word1 in German'); DEFINE('WORD2', 'word2 in German'); ...... and include this php language file. Another option which is more standard is to create a mo file and call it by php. I prefer the first method as everything is just based on php; but I doubt if it is the best way for a website with 1000+ sentences. Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/244577-language-file/ Share on other sites More sharing options...
JasonLewis Posted August 12, 2011 Share Posted August 12, 2011 Why not define it in a multi-dimensional array, be a hell of a lot more organised. And regardlesss, your going to end up with lots of keys in the array for translating purposes. $lang = array( 'en' => array( 'welcome_text' => 'Your welcome text in english' ) ); Of course you could have separate files for each language and so on. Quote Link to comment https://forums.phpfreaks.com/topic/244577-language-file/#findComment-1256302 Share on other sites More sharing options...
etrader Posted August 12, 2011 Author Share Posted August 12, 2011 Thanks, it seems to be a perfect solution. I can call the translations by $lang['word'] BUT how I can write a function to return (or echo) based on this scheme. I mean function language($word) { echo $lang[$word]; } then using language('word') for displaying the translation. Quote Link to comment https://forums.phpfreaks.com/topic/244577-language-file/#findComment-1256314 Share on other sites More sharing options...
JasonLewis Posted August 12, 2011 Share Posted August 12, 2011 Generally, in most multi-lingual based applications they'll use a function like __() to echo out the translations. Most of these use the dot separated syntax. Take my first array for example, to access the welcome_text property you'd call something like this: echo __('en.welcome_text'); The __() function would then break it apart and check the array for the existence of said keys, and if so return the resulting text. Something to point out is that you shouldn't echo from within a function like that, it's nicer if you return the string. Quote Link to comment https://forums.phpfreaks.com/topic/244577-language-file/#findComment-1256319 Share on other sites More sharing options...
etrader Posted August 12, 2011 Author Share Posted August 12, 2011 Sounds great. I thought that __() only works for mo files as php has a function to get it. However, I did not get your solution echo __('en.welcome_text'); How it can recognize to look into $lang array ? Quote Link to comment https://forums.phpfreaks.com/topic/244577-language-file/#findComment-1256322 Share on other sites More sharing options...
JasonLewis Posted August 12, 2011 Share Posted August 12, 2011 I believe _() is a function in PHP, but not __(). In your __() function you'd need to explode on the dots. Then, you'd need to loop over each bit and see if it exists in the language array. function __($line) { include('lang/en/en.php'); // Explode the $line variable into bits. $bits = explode('.', $line); // Start the phrase off as false, that way the first if statement will evaluate to true, assuming the bit exists in the language array. $phrase = false; foreach($bits as $bit) { if($phrase === false && isset($lang[$bit]) { // Now set $phrase to the current bit in the language array. $phrase = $lang[$bit]; } elseif(isset($phrase[$bit])) { // And continue to update it accordingly. $phrase = $phrase[$bit]; } else { // Could not find the correct phrase. return false; } } // Return the phrase return $phrase; } So again let's use the $lang array I defined earlier. If we were to echo __('en.welcome_text'); our function would break that apart into an array like: Array ( [0] => 'en', [1] => 'welcome_text' ) So we run this through the loop, and the first check is to see if $lang['en'] exists, and it does, so we update $phrase to be $lang['en']. Then the next itteration occurs. And the elseif() is fired which also evaluates to true because $lang['en']['welcome_text'] exists in the $lang array. After the loop it returns this phrase and hey presto we have our phrase we wanted translated. Quote Link to comment https://forums.phpfreaks.com/topic/244577-language-file/#findComment-1256328 Share on other sites More sharing options...
etrader Posted August 14, 2011 Author Share Posted August 14, 2011 As a matter of fact, perfect solution Quote Link to comment https://forums.phpfreaks.com/topic/244577-language-file/#findComment-1257022 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.