8

Is gettext the best way to localise a website in php? I'm not using any frameworks, and there are not many words to translate, just two mildly different versions in English.

Ric
  • 3,195
  • 1
  • 33
  • 51

4 Answers4

12

You can just use a lang_XX.php and include it in your application.

$lang = array(
    "welcome" => "Welcome",
    "bye" => "Bye"
);

For other languages, say lang_fr.php, just have something like:

$lang = array(
    "welcome" => "Accueil",
    "bye" => "Au revoir"
);

For a small use case, this should be fine and no need for going with .po files. Also, you can define it this way:

function _($l)
{
    return $lang[$l];
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 6
    +1 Good solution for a simple site – Tchoupi Oct 04 '12 at 16:15
  • 6
    Though the solution is good, I would still recommend going the gettext route. You never know where your site is going to go. It may get larger fast and you may need to add additional languages. Gettext is not going to take much longer to implement than this proposed solution – eakkas Oct 04 '12 at 23:38
  • Even nicer that @NatlsGleek as this way translation files could be sent to other people to complete – Ric Oct 05 '12 at 07:31
  • 2
    I selected this answer as I only need to localise a few words here and there. For bigger jobs I'd explore the gettext route. – Ric Oct 05 '12 at 07:34
  • I advised that they should come from separate files too, but I didn't do that in the example for readability. I said for the array to come from a JSON file. In all honesty, JSON is probably more human-readable than a PHP array... – Nate Higgins Oct 05 '12 at 09:29
  • You can use [php2po](http://translate.sourceforge.net/wiki/toolkit/php2po) to get the quick benefits of using PHP arrays but still have access to the data in PO files for later Gettext usage and more importantly to make it easy for your translators. PHP could be the worst format for a translators, so this way you can give the translators PO files and they can use real translation tool. – Dwayne Oct 05 '12 at 12:38
  • +1 because it's what it's asked here. I added a bit to make it return the original key in case it doesn't exist. – Francisco Presencia Jan 08 '13 at 18:44
10

gettext is the best way. For a small site with few strings that isn't expected to grow much implementing something similar to the solutions posted already may actually be quicker than learning how to use gettext and configuring it.

tl;dr:

People have been thinking hard over many years to create a translation system that works, you could spend ages re-inventing that wheel, or you could spend a little while learning how to use gettext.

In the open source world, gettext is practically a de-facto standard.

Here are some reasons why gettext is better than the usual alternative of maintaining big lists of translation arrays

  1. It is faster
  2. It does not require loading every single string into memory once per thread
  3. It has lots of tool support for translators, such as poedit, so you are not reliant on your translators understanding how to edit PHP code.
  4. It has tool support for you, msgmerge and so forth, mean you can send off your file to be translated while still working and adding new translatable text and easily merging them back together
  5. Need to give a comment to the translator about the context of a string? gettext will take a comment from the code where it is used and copy it to the translation file
  6. The translation keys are the English text, if it hasn't been translated yet then the text is still displayed in plain English, none of the solutions given on this answer to date get this right.
  7. It supports plurals, if a string depends upon an integer, in english there are only two forms if n != 1 "There was a person" else "There were n people". For other languages the rule isn't as simple as if (n!=1), Russian has 3 different variants and the rule is n%100/10==1 ? 2 : n%10==1 ? 0 : (n+9)%10>3 ? 2 : 1;
dsas
  • 1,650
  • 18
  • 30
  • For large translations I think this is the best option, but in my case it's overkill. – Ric Oct 05 '12 at 07:32
  • 1
    Do you still stand by your recommendation as of today (February, 2015)? Has anything else emerged since 2012? – alexw Mar 01 '15 at 16:13
  • The symfony translation component looks very well thought out, I've never used it though. – dsas Mar 01 '15 at 16:36
4

Performance-wise, gettext Extension is faster than using a String-Array that maps like string ids to localized text (for example "WelcomeText" => "Welcome to our homepage." would be included using something <?= $strings["WelcomeText"] >. The pure PHP implementation of gettext is slower and not recommended if you can use the PHP Extension. More details here Localizing PHP web sites using gettext and Benchmarking PHP Localization – Is gettext fast enough?

chridam
  • 100,957
  • 23
  • 236
  • 235
  • 1
    FYI, both of the bottom links are broken as of _2019_ due to the missing _MySQL_ extension in the _WordPress_ installation... – War10ck Dec 10 '19 at 15:29
  • @War10ck Thanks for letting me know, I have updated the links to use wayback machine – chridam Dec 10 '19 at 15:35
1

For static content that will never / extremely rarely change, sure.

For dynamic content, not in the slightest. Compiling the .po files etc is way over the top, and I'd suggest some kind of JSON or XML solution and templating instead, with cached compiled templates.

danp
  • 14,876
  • 6
  • 42
  • 48