kla0005 Posted January 13, 2011 Share Posted January 13, 2011 Hi guys, i was wondering.. Languages? How does that excactly work? Where do you save the different language strings, and that stuff? Do you have php files' for each language, and just includes them? Or,.. Anyone who can explain me? thanks =) Quote Link to comment https://forums.phpfreaks.com/topic/224356-languages/ Share on other sites More sharing options...
kla0005 Posted January 14, 2011 Author Share Posted January 14, 2011 1, 2, 3 - GO! Quote Link to comment https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159316 Share on other sites More sharing options...
AbraCadaver Posted January 14, 2011 Share Posted January 14, 2011 Two simple ways are to store language files that define constants for common words or phrases that you use in your application interface: //lang-eng.php define('SUBMIT', 'Submit'); //lang-esp.php define('SUBMIT', 'Presentar'); You could also do this in the DB which is nice because other content such as large blocks of text, news articles, etc. would probably be better in the DB: xlate_table id lang key text 1 eng SUBMIT Submit 2 esp SUBMIT Presentar news_table id lang title text 8 eng Some news Just some news text here 9 esp Algunas noticias Sólo un poco de noticias de texto aquí These are just simple examples. Quote Link to comment https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159401 Share on other sites More sharing options...
The Little Guy Posted January 14, 2011 Share Posted January 14, 2011 I would do it like this: index.tpl: <b>{DICT.SomeText}</b><i>{DICT.SomeOtherText}</i> I would put this in some method or function: Note: I didn't test the query $tpl = file_get_contents('index.tpl'); preg_match_all("/{DICT.(.+?)}/", $tpl, $matches); $indexes = $matches[1]; switch(LANGUAGE){ case 'en': $lan = 'EN'; break; case 'sp': $lan = 'SP'; break; default: $lan = 'EN'; break; } $searchStr = implode(',', $indexes); $sql = mysql_query("SELECT t.translation, t.code FROM translations t LEFT JOIN languages l ON (t.lan_id = l.id) WHERE t.code IN($searchStr) AND l.code = '$lan'"); $dict = array(); while($row = mysql_fetch_assoc($sql)){ $dict[$row['code']] = $row[$lan]; } foreach($dict as $code => $translation){ $tpl = preg_replace("/{DICT.$code}/", $translation, $tpl); } echo $tpl; So basically you have 2 tables "languages" with fields: - id (auto_increment) - code (EN, SP, JP, CN, etc.) And "translations" with fields: - id (auto_increment) - lan_id (relates to id in languages table) - code (SomeText, SomeOtherText, etc.) - translation (the actual text to be displayed) That is a basic route to take (Similar to AbraCadaver second method). Quote Link to comment https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159419 Share on other sites More sharing options...
lastkarrde Posted January 14, 2011 Share Posted January 14, 2011 gettext to the rescue! Quote Link to comment https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159530 Share on other sites More sharing options...
ignace Posted January 15, 2011 Share Posted January 15, 2011 The obvious problem with TLG's solution is: what if there's a missing translation? Currently the customer would see {DICT.somethingHere} or whitespace, worst case would be that your entire menu would be invisible due to missing translations or file permissions. Personally I think the best approach is to simply write the website in a default language, pass it through some function that returns the input if it fails to load a translation. <li><?php print $this->translate('Home'); ?></li> <li><?php print $this->translate('About us'); ?></li> <li><?php print $this->translate('Contact us'); ?></li> Quote Link to comment https://forums.phpfreaks.com/topic/224356-languages/#findComment-1159799 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.