views:

445

answers:

2

We are in the process of making our website international, allowing multiple languages.

I've looked into php's "gettext" however, if I understand it right, I see a big flaw:

If my webpage has let's say "Hello World" as a static text. I can put the string as <?php echo gettext("Hello World"); ?>, generate the po/mo files using a tool. Then I would give the file to a translator to work on.

A few days later we want to change the text in English to say "Hello Small World"? Do I change the value in gettext? Do I create an english PO file and change it there? If you change the gettext it will consider it as a new string and you'll instantly loose the current translation ...

It seems to me that gradually, the content of the php file will have old text everywhere. Or people translating might have to be told "when you see Hello World, instead, translate Hello Small World".

I don't know I'm getting confused.

In other programming languages, I've seen that they use keywords such as web.home.featured.HelloWorld.

What is the best way to handle translations in PHP?

Thanks

+3  A: 

You basically asked and answered your own question, the answer might just be having a slightly better understanding of how PO files work.

Within the PO file you have a msgid and a msgstr. The msgid is the value which is replaced with the msgstr within the PHP file depending on the localization.

Now you can make those msgid's anything you would like, you could very well make it:

<?php echo _("web.home.featured.HelloWorld"); ?>

And then you would never touch this string again within the source, you only edit the string through the PO files.

So basically the answer to your question is you make the gettext values identifiers for what the string should say, however the translators typically use the default language files text as the basis for conversion, not the identifier itself.

I hope this is clear.

evolve
If it can't find web.home.featured.HelloWorld for the given language, will it display "web.home.featured.HelloWorld", or the English version for that msgid?
nute
@nute: It will display `web.home.featured.HelloWorld`.
Alix Axel
Using this method, how can I show: Showing %s to %s of %s results (where %s are some variables)?
nute
Cases where you need to pass variable to the string are indeed an issue. If your application needs extensive localization you might want to either roll a custom solution or take a look at a framework that has localization built in (Zend_Translate, yii-i18n, etc.).
evolve
A: 

Currently, I am dealing with the same issue. The common practice with gettext is to use the English text as the key. Recently, our copy editor changed whole bunch of English text (other languages are hardly touched) so we have to change all the source code all the PO files.

We are switching to a neutral key. Since we already have some sites on Java. We will use the same property name format.

ZZ Coder