what is the best method to build "multilingual" script in php? - php

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?

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.

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.

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

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.

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.

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.

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

Related

Is it good practice to make a constant that varies based on the user language?

I'm making constants in my PHP application to organize its responses, and I'm planning to add foreign language support soon. Here's what I'm doing right now: define('SOME_SYSTEM_MESSAGE',array('EN' => 'This is the system message!') [USER_LANGUAGE]); (I know that the [ ] syntax is only supported in PHP 5.5, I have a backwards-compatible function for older versions). Is that good practice for coding, or should I keep the messages in an array (like $en for english messages or $fr for french messages), or some other way?
I think it depends on where you want to go with your application.
Your proposed solution might work if you want to keep it mostly limited to yourself / your team.
But if other people should use your code later on or if you want to be able to translate it into other languages, it would be handy to have files like .po or .csv which translaters can handle.
Constants can not hold arrays, constants are limited to the following types: null, boolean, integer, string or resource.
If you want to localize your application into different languages use gettext or other localization technologies. You should prefer components instead of reinventing something.
As an Example: The Symfony Framework contains an (standalone) Translation-Component.

Adding multiple languages in a website

I want to add multiple languages to my site.
I read somewhere that I can use translator(Google or babelfish) but I don't like this way.
Can anyone suggest me different ways?
You could learn the language and translate it yourself. Besides that you will need to use a translator.
You'll want to read up a bit on internationalization and localization (often referred to as i18n L10n). You'll need code to support serving your various translatinons, based on your users' preferences. You'll also want to give some thought to handling things like date and currency formats.
As far as PHP tools, you've got the gettext stuff, which can be compiled in to PHP. Gettext works, but was designed to handle translating interface text for locally-installed software -- it doesn't transition to web sites/apps terribly well.
There's also Zend_Translate, which is a pretty good library, and can easily be used without most of the rest of the Zend Framework. You might want to look at Zend_Locale and Zend_Date, as the three can play together nicely.
You could integrate a translation interface to your site and let the users of your site create their own translation. This way, you get the translation for free.
Or, as an alternative, you could open your website logic to a community (i.e. make it open source) and let it translate by them...
Another way would be to hire someone to translate it into their language :)
if you have members in your site, do what FB is doing ..
they ask the members to help translating to their language, they put the phrases for them, and collect the translations + votes (whether the translation is good or there's better translation).

Internationalization in PHP

I am currently researching the best methods to integrate i18n into projects.
There's several methods I have thought of doing this, first being a database scheme to store the strings and relevant locale, but the problem with this is that it would not be that easy to select the strings, because i would not like to perform quesries like so:
SELECT text FROM locales WHERE locale = 'en_GB' AND text_id = 245543
Or
SELECT text FROM locales WHERE locale = 'en_GB' AND text_primary = 'hello'
The next method would be to store them within files such as locales/en_gb/login/strings.php and then try and access them via an class specifically developed like so:
$Language = Registry::Construct('Language',array('en_GB'));
echo $Language->login->strings->hello;
The issue with this is I would have to build a system that would update these files via an administration panel witch is very time consuming, not just building the system to manage the strings but actually managing the strings as the site grows
What other methods are there that will be beneficial for a large system
Is there any automated way to do 'Translation' as such
Should I stick with a database method and build a system for users to translate strings with rating / suggest better version ?
What systems have you tried in the past and should I look into them or totally avoid them.
In addition to gettext already mentioned, PHP 5.3 has native Internationalization support
If that's not an option, consider using Zend Framework's Zend_Translate, Zend_Locale and related components for that. Zend_Translate supports a number of adapters, including but not limited to simple arrays, gettext, XmlTm and others.
I've implemented a XML translation utility as part of a bigger project. You can find it here, and a sample translation file is here (en_US).
The most impressive method to study is Drupal's implementation. Second best, would be Wordpress. Both use gettext and .pot/.po/.mo for localization. And, the good thing is that there is a beautiful Open Source .po editor called Poedit. It's available for Windows System users, which gives a wider appeal. It's also available for Mac and Linux. Check it out here: http://www.poedit.net/
Have a look at the Gettext (http://php.net/manual/en/book.gettext.php) library.
Don't put your text into a database. That'll just make life hard on the translation team.

Better to use multiple language files or 1?

From your experience, is it better to use 1 language file or multiple smaller langauge files for each language in a PHP project using the gettext extension? I am not even sure if it is possible to use multiple files, it is hard for me to test since the server caches the language files.
I am doing multiple languages on a social network site, so far just the signup page which is about 1 out of 200 pages to go and it has 35 text strings to translate, at this pace the language file for each language wold be really large so I was thinking maybe it would be better to do different language files for differnt pages or perhaps sections like forums section and blogs section but if it makes no difference then I would ratther not waste my time in making multiple smaller files for each language.
I realize every situation is different and the only real answer is to test it but I am hoping to avoid that this time and just get some oppinions of people more experienced, this is my first time using gettext, thanks
I would have the language files module based. With gettext you need to specify locale for each language. It would fit best to have a separate .po/.mo files for each module or big parts of your site.
That's my opinion. :-)
I typically automate the process and have multiple languages in multiple files by using a database to edit the site (using a simple db lookup). This lets me hire translators to come in and verify the current translation easily. Deploying to production then is simply turning the database into a set of language files.
From experience i would break the languages down on a per file basis as the management overhead becomes heavy and there is great scope for duplication and mistakes.
The other advantage it that by using a directory structure and naming convention the correct language can be selected programatically more easily than the large file and it is easier to write management tools at a later stage in the project.
It is also worth looking at some of the formats other people use. Many of the Frameworks use this sort of structure, Dashcode, Symfony, Zend etc. And there is an xml format xliff which is built to handle translation and integrates with many of the tools that translators use.
Multiple files are the best way to go, but things can get disorganized.
We've just launched a free new service called String which solves most of the problems of managing multiple language files - like a basecamp for localization. You can either import existing files, or start from scratch with keys and strings in the system. When you're ready, you can export the files again to run your app. It works with PHP (array), PHP (define), po, yaml, ini and .strings formats.
String allows you to collaborate with translators easily - you just invite them to a project and set their language permissions. Translators can leave comments and questions on each string if they need more info - and you can revert strings back using the History function if things aren't quite right.
Anyway enough sales pitch!
Check it out at http://mygengo.com/string - we'd love your feedback.

What are good tools/frameworks for i18n of a php codebase?

I have been looking at a few options for enabling localization and internationalization of a dynamic php application. There appears to be a variety of tools available such as gettext and Yahoo's R3 and I am interested in hearing from both developers and translators about which tools are good to use and what functionality is important in easing the task of implementation and translation.
PHP gettext implementation works very smoothly. And po files with po edit and gettext are about as good a way as you can get to deal with localization bearing in mind that no solution of this kind can completely handle the complexities of the various languages. For example, the gettext method is very good on plural forms, but nothing I've seen can handle things like conjugation.
For more info see my post here: How do you build a multi-language web site?
We've been tinkering withZend_Translate, since we use the Zend Framework anyway. It's very well documented and so far extremly solid.
In the past, I've pretty much used my own home-grown solution mostly. Which involves language files with constants or variables which hold all text parts and are just echo'ed in the view/template later on.
As for gettext, in the past I've heard references about PHP's gettext implementation being faulty, but I can't really back that up nor do I have any references right now.
There are a number of useful extensions in pecl:
http://pecl.php.net/packages.php?catpid=28&catname=Internationalization
In particular, you may want to check out php-intl, which provides most of the key i18n functions from International Components for Unicode (ICU)
the database driven solution to show the messages is not always the good one, I worked in a site with more than 15 languages and translations were an issue.
so our design was:
translation app in php-mysql (translation access, etc.)
then translations are written in php arrrays
these arrays are also cached in APC to speed up the site.
so to localize different languages you only need do an include
like
<?php
include('lang/en.php');
include('lang/en_us.php'); // this file overrides few keys from the last one.
?>
Xataface can be used to quite easily internationalize an arbitrary PHP/MySQL application. It support translation of both your static text, and your database data. All you have to do is add a line or 2 of code to a couple of places in your application and it's good to go.
http://xataface.com/documentation/tutorial/internationalization-with-dataface-0.6

Categories