test.php file echoes some greek letters for testing
<head>
<meta charset = "UTF-8" />
</head>
<?php
$language = "en_US";
$lang_path = "language";
putenv("LC_ALL=$language");
setlocale(LC_ALL, $language);
bindtextdomain("lang", $lang_path);
bind_textdomain_codeset("lang","UTF-8");
textdomain("lang");
echo gettext("α");
echo gettext("β");
echo gettext("γ");
echo gettext("δ");
echo gettext("ε");
echo gettext("ζ");
?>
My folders and files are structured like
language/en_US/LC_MESSAGES/lang.mo and lang.po
mo file is correctly compiled, and it must translate to abcdez. In poedit i have used utf-8 everywhere. The code produces αβcδεζ , so only letter c is translated! I have restarted my server, i have en_US encoding installed in my system and i can’t understand why only one letter is translated..
A simple implementation i wrote reading zend and cakephp code. It does not support plurals, and it simply replaces words, so you can replace not only from one language.
test.php file
gettext.php file
In my code i have default language el, and you get the language with GET[‘lang’]. (You can change that to a COOKIE, SESSION etc. Also the mo files must be called default.mo. (You can change that also in the code). So you can have in your directory:
default is el/default.mo and you can change to en with ?lang=en in your url.