what is the best method to build “multilingual” script in php?

不想你离开。 提交于 2019-11-27 16:34:56

问题


I am building a website and it need to be in 7 languages? I was wondering if there is a good practice can be applied to get multilingual php script?

  • Easy for me
  • Easy for the translators

Also what do you think , should I Store it in DB , XML or in PHP file?


回答1:


There are plenty of options for storing translations:

  • TMX: A relatively new XML format for translations. Seems to be gaining in popularity.
  • Gettext is another open format for translations. Been the de-facto standard for a long time.
  • ini files - easy to edit, very simple format
  • PHP files (arrays) - easy to edit for PHP programmers, good performance
  • CSV format - relatively simple to use.

I'd suggest you use something like Zend_Translate which supports multiple adapters and provides a basic approach to embedding translations in your application.




回答2:


Contrary to daddz I would recommend against using gettext in PHP:

  • The locale setting is per-process. This means that when you are working with a multithreaded apache or any other multithreaded webserver running PHP in-process, calling setlocale in one thread will affect the other threads.

    Because you can't know which thread/process is handling which request, you'll run into awful problems with users intermittently getting the wrong locale.

  • The locale you set in PHP has influence on functions like printf or even strtotime. You will certainly get bit by "strange" number formats arriving in your backend code if you work with gettext/setlocale

Use any of the other solutions lined to by Eran or quickly do something yourself (PHP arrays work very nicely). Also use the intl-extension which will be in core PHP 5.3 for number and date formating and collation.

Using gettext on a web based solution over and over proved to be quite like opening the proverbial can of worms.




回答3:


I'd suggest Gettext.

It's cross-platform, open-source, widely used and available for php: PHP Gettext




回答4:


I have built multilingual CMS. All content was stored in a database, with main tables for common (not language specific values) and separate tables for the language specific content.

For instance, let us imagine storing products - we have a 'products' table (contains unique_id, date created, image urls etc etc) and a 'product_local' table (contains any language specific fields).

Using this method it is very easy to maintain content.




回答5:


I have no experience on gettext so no comment on that topic, but I have built a few multi-lingual sites using the following methods:

METHOD 1

I wouldn't say my format is the best, just that it's effective. I've also used array. Depending on where the content is stored.

For example, I'll have an associative array of text with the indexes identifying which text:

$text['english']['welcome'] = "Welcome to my site. blah blah blah";
$text['english']['login'] = "Please enter your username and password to login";

And maybe set your language with a constant or config variable.

METHOD 2

I've built two sites with identical structures and back-ends but each one used a different database and were maintained separately: data_french, data_english.




回答6:


You may find this article on the topic an interesting read:

http://cubicspot.blogspot.com/2011/12/cross-platform-multilingual-support-in.html

The author advocates a "lazy programmer" strategy - do it only if you need multilingual stuff - and seems to recommend the PHP array approach with IANA language codes. The article is kind of vague though.




回答7:


Check this forum. I think you'd probably need a different approach if you have somebody helps you with translation.

Most efficient approach for multilingual PHP website



来源:https://stackoverflow.com/questions/457586/what-is-the-best-method-to-build-multilingual-script-in-php

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