views:

1164

answers:

6

Hello guys,

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?

+2  A: 

I'd suggest Gettext.

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

daddz
+4  A: 

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.

Eran Galperin
+2  A: 

Although a valid question, this has been asked and answered before.

See: http://stackoverflow.com/questions/450687/programming-multi-language-php-applications.

Aron Rotteveel
+2  A: 

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.

pilif
A: 

I have no experience on gettext so no comment, but I have built a few multi-lingual sites.

METHOD 1

I wouldn't say my format is the best, just that's 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

Hope that helps.

jerebear
A: 

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.

BrynJ