Sunday, December 27, 2009

I am creating a multilingual site in php. How to create the files for it, or anything we want to configure.?

I wouldn't follow the example that answer one points to; using a DB query to fetch the localized version of each piece of text for a page is prohibitively expensive and will likely lead to more DB code than actual code implementing your functionality.





The concept is sound, though. You essentially want to assign a symbolic name to each unique string you want to localize/display. Then the idea is to package up all the strings in a given language and index them via the symbolic name. Then, at access time you need to ';load'; a particular set of strings, based on the desired language. (See Firefox's use of string bundles for a similar concept.)





So if you had english.inc with


%26lt;?php


$_string = array(


'greeting' =%26gt; ';Hello World';,


'userlabel' =%26gt; ';Enter user name';,...


?%26gt;





and spanish.inc with


%26lt;?php


$_string = array(


'greeting' =%26gt; ';Hola Mundo';,


'userlabel' =%26gt; ';incorpore el nombre de usuario';,...


?%26gt;





Then in your page





$sCurrentLanguage = ';english';; // actually set some other way


require ($sCurrentLanguage . ';.inc';);


...


%26lt;p%26gt;%26lt;?php echo $_string['greeting']; ?%26gt;%26lt;/p%26gt;


%26lt;p%26gt;%26lt;?php echo $_string['userlabel]; ?%26gt;%26lt;/p%26gt;








You could actually put all the different languages in one giant array, and supply another level of indexing using the language name instead of separate includes. Up to you.





The point is to abstract the actual strings from the display and give each a meaningful name which you will use in the page, and which will be resolved to the appropriately localized string when the page is built.I am creating a multilingual site in php. How to create the files for it, or anything we want to configure.?
Look here:


http://www.jugglingdb.com/compendium/gee鈥?/a>

No comments:

Post a Comment