Is gettext the best way to localize a website in php? (only a small localization is required)

Is gettext the best way to localize a website in php? I do not use any frameworks, and not many words to translate, only two different versions in English.

+7
source share
4 answers

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

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

For other languages, say lang_fr.php , it just turned out something like:

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

In a small case, this should be good and no need to migrate with .po files. Alternatively, you can define it as follows:

 function _($l) { return $lang[$l]; } 
+12
source

gettext is the best way. For a small site with a small number of lines that is not expected to grow, implementing something similar to solutions may already be faster than learning how to use gettext and customizing it.

TL; DR:

People have been thinking for many years about creating a translation system that works, you could spend a lot of time reinventing this wheel, or you could spend a little time learning how to use gettext.

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

Here are a few reasons why gettext is better than the usual alternative for storing large lists of translation arrays.

  • It's faster
  • No need to load every single line into memory once in a stream
  • It has a lot of support for translators like poedit, so you don’t rely on translators who understand how to edit PHP code.
  • It has tool support for you, msgmerge, etc., means that you can send your file for translation in the process, adding a new translated text and easily merge it back.
  • Does the translator need to comment on the context of the string? gettext will comment on the code in which it is used and copy it into the translation file.
  • Translation keys - this is the text in English, if it has not yet been translated, the text is still displayed in plain English, none of the solutions provided in this answer to date are eligible.
  • It supports plurals, if the string depends on 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 is not as simple as if (n! = 1), the Russians have 3 different options, and the rule is n%100/10==1 ? 2 : n%10==1 ? 0 : (n+9)%10>3 ? 2 : 1; n%100/10==1 ? 2 : n%10==1 ? 0 : (n+9)%10>3 ? 2 : 1;
+10
source

Performance, gettext Extension is faster than using String-Array, which displays as string identifiers for localized text (for example, "WelcomeText" => "Welcome to our homepage." Will be enabled using something <?= $strings["WelcomeText"] > . A pure PHP implementation of gettext is slower and not recommended if you can use the PHP extension. More details here Localization of PHP websites using gettext and Benchmarking Localization of PHP - is gettext enough?

+3
source

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

For dynamic content, not in the least. Compiling .po files, etc. is the path from above, and I would suggest some kind of JSON or XML solution and templates with cached compiled templates instead.

+1
source

All Articles