How to build PHP application with multiple language support (English, French, Chinese etc..)

时光毁灭记忆、已成空白 提交于 2019-12-11 03:33:13

问题


I'm building a web application that uses LAMP and the Smarty template framework. The website will have plenty of static content (about us page, error alerts, confirmation emails etc...).

The web application must support multiple languages. Is the following approach appropriate?

1) All static copy on Smarty template pages (html pages) will be replaced by calls to smarty plugins such as {lang file='about.xml' getTerm='title'} which returns a string for the title tag in the appropriate language

2) The file about.xml is an xml document on the webserver that acts as a multiple language dictionary. So it will have xml nodes such as

<term value="title">
   <en>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; is a great site</en>
   <fr>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; c'est website magnifique</fr>
   <ch>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; complex looking chinese characters I don't udnerstand</ch>
</term>
<term value="signupmessage">
   <en>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; great deals!</en>
   <fr>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; magnifique deals!</fr>
   <ch>&lt;a href=&quot;http://stackoverflow.com&quot;&rt;Stackoverflow&lt;/a&rt; complex looking chinese characters I don't udnerstand</ch>
</term>

3) The url structure for the site will be http://mywebsite.com/language/index.php. To do this, my webserver will have symbolic links such as ln -s ./en ./ so that http://mywebsite.com/en/index.php points to the same php page as http://mywebsite.com/fr/index.php, but I will check the url for language settings. My hope is that google will index both versions of the site.

Is this a good approach? Is xml the best format for storing content of different languages even though it has htmlentity encoded html tags? Did I overlook anything?


回答1:


Not sure about the details of your question but you may want to take a look at Zend_Translate.




回答2:


XML is a container format and this use of it isn't too bad. It has the advantage of being relatively human readable except for the HTML entities. You could try using CDATA sections for that. One advantage of using XML for this is that you could use a DTD to validate that all languages have translations present for all keys. Make sure you encode the file using UTF-8 or UTF-16 and it probably helps if you put the encoding at the top of the file. I'd recommend UTF-8.

As for how you load the XML data and incorporate it into the page, I don't know "smarty" but the overall approach sounds reasonable. The only downside I can see is that you have to release all the translations simultaneously (difficult to avoid this problem).

One thing I note is that you use ch as the language code for Chinese. You should use zh. That is the ISO code for it. ch is something else.



来源:https://stackoverflow.com/questions/691218/how-to-build-php-application-with-multiple-language-support-english-french-ch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!