views:

507

answers:

4

So far the only 2 good things that I've seen about using gettext instead of arrays is that I don't have to create the "greeting" "sub-array" (or whatever its called). And I don't have to create a folder for the "default language".

Are there other pros and cos of using gettext and php arrays for multilingual websites?

USING GETTEXT:

spanish/messages.po:

#: test.php:3
msgid "Hello World!"
msgstr "Hola Mundo"

index.php:

<?php echo _("Hello World!"); ?>

index.php?lang=spanish:

<?php echo _("Hello World!"); ?> turns to Hola Mundo

USING PHP ARRAYS:

lang.en.php

<?php
$lang = array(
    "greeting" => "Hello World",
);
?>

lang.es.php

<?php
$lang = array(
    "greeting" => "Hola Mundo",
);
?>

index.php:

<?php echo $lang['greeting']; ?> greeting turns to Hello World

index.php?lang=spanish

<?php echo $lang['greeting']; ?> greeting turns to Hola Mundo

(I first started with gettext, but it wasn't supported in my shared free hosting Zymic. I didn't want to use Zend_translate, I found it too complicated to my simple task, so I finally ended up using php define, but later on someone told me I should use arrays)

A: 

In my opinion it does not make too much sense to use a binary format (gettext's .mo files) for a dynamical language such as php.

However, a pro of gettext is the existence of a huge ecosystem of tools that translators of your software are able to use (e.g. Poedit). Downside is that you always have to compile your .po to .mo before you deploy it while you can just replace a php file containing an array on the fly.

Another con is, as you've already recognized, it is not available on all installations of php.

middus
+2  A: 

Using GNU gettext you get support for placeholders like with printf and international plural forms. Placeholders order can be changed in translation or skipped.

Example from PHP documentation:


<?php
setlocale(LC_ALL, 'cs_CZ');
printf(ngettext("%d window", "%d windows", 1), 1); // gives "1 okno"
printf(ngettext("%d window", "%d windows", 2), 2); // gives "2 okna"
printf(ngettext("%d window", "%d windows", 5), 5); // gives "5 oke"
?>

Another pro is that you can use standard tools for terminology management, translations memory and machine translation as pointed by @middus.

For shared environments there is a great php-gettext library from Danilo Segan.

Goran Rakic
When you're using arrays, you can still use pladeholders using printf, can't you? (You don't have the plural forms, though)
middus
Yes you can. :)
Goran Rakic
+1  A: 

The most obvious pro for using gettext() is of course that the source string is positioned where it belongs. I.e. it makes much more sense to write this

echo _("This is a string");

than

echo $lang['a_string'];

Not to mention that you have to craft a new variable placeholder for every possible translation. With gettext() the translation string itself acts as the index.

tobefound
@tobefound I agree with you, but I found 2 pros of using echo $lang['a_string']; (actually now I used l('string_a);): 1. Since content is outside of the index page the code looks more readable and compact. 2. If I want to translate the whole content of the web page I can copy paste the content in a translator (since the lang.php only contains phrases and words.
janoChen
A: 

i recommend using gettext, i am doing that in php for 5 years with good results

first of all if you use echo _('my text to translate') and have no translation for it, you will see the original string in the output, which is good. using arrays like echo $translation['were is my translation'] and there is none, you will just see nothing. but beware, using poedit and doing a echo _(''); is not a good idea, poedit uses the msgid "" for the project information which most likely you dont want to show your audience, so one must take care of not trying to translate empty strings :)

also its very fast and has some additional features for plurals and stuff, also poedit for example makes the life easier by having a translation db so you must not translate the same stuff over and over again, those you did already will be prefilled and marked as "check if its right". very comfortable.

theby middus mentioned downside that you have to compile the po file while you could easy overwrite a php-file when using arrays - well you just overwrite your mo file too, and if using poedit, it does the compiling after saving the file. so in fact you hit save and copy the file, sam as with editing a php-file.

but a real downside is, if you use mod_php. be aware that with mod_php it is not threadsafe, though i never had any hard problems.

its mostly just that you have to restart your apache when using mod_php or your gettext calls will fail (sometimes no translations returned, sometimes you end up with the beloved white page with no contents). but by using something like mod_itk (i believe cgi/fastcgi can do this to) you wont even have this problem no more.

ingo