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.
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];
}
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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With