I currently trying to convert English to Chinese word. These is my code.
<?php
$language = "zh_CN";
putenv("LC_ALL=$language");
setlocale(LC_ALL, $language);
$domain = "messages";
bindtextdomain($domain, "Local");
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
echo _("HELLO_WORLD");
?>
And this is the output. It should be displaying 你好世界! instead of Hello World!
I tested with English conversion and it working fine. But when I replace $language = "en_US" to $language = "zh_CN" and restart apache, it still showing the English translation word.
I got print the value of putenv and setlocale and get putenv = 1 and setlocal = empty.
I'm using
Window 10 single language.
Xampp 5.6.12
PHP Version 5.6.12
And this is my directory
Local----->en_US ----->LC_MESSAGES ----->messages.mo
----->messages.po
----->zh_CN ----->LC_MESSAGES ----->messages.mo
----->messages.po
Related
$folder = "locale";
$domain = "messages";
$encoding = "UTF-8";
putenv("LC_ALL=" . $language);
setlocale(LC_ALL, $language);
bindtextdomain($domain, $folder);
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
$language has been previously set to fr_FR (i checked) and whenever i call echo _("HELLO_WORLD"); (for an example), it only returns translation in english (en_US). I've properly set all the folders ( path to french .mo file is locale/fr_FR/LC_MESSAGES/messages.mo ), I've translated "HELLO_WORLD" both into english and french (using Poedit), i tried adding ".UTF-8" to the $language string, but nothing works, it still only shows english translation. I also tried restarting Apache web server every time i made changes to .mo files. Does someone know why does this occur?
I'm using Win10 64bit and XAMPP v3.2.2.
I use a WAMP server on Windows and make a test project.
I have these files in the website folder:
C:\wamp\www\project\locale\ar_EG\LC_MESSAGES\messages.po
C:\wamp\www\project\locale\en_US\LC_MESSAGES\messages.po
Below is the PHP Code:
$language="en_US";
$encoding = "UTF-8";
putenv("LANG=".$language);
setlocale(LC_ALL,$language);
$domain="messages"; // name of PO file
bindtextdomain($domain,"Locale");
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
echo gettext("name");
Above code is working fine.
When I try to replace en_US with ar_EG it also displays the translation for en_US, and when I try to remove the en_US folder and try again it displays the msgid not msgstr.
I searched and found that there is a problem with setlocale on the Windows platform, but I need a solution for it to run on Windows.
setlocale() on Windows has issues.
From setLocale() in PHP Manual:
Windows users will find useful information about locale strings at
Microsoft's MSDN website. Supported language strings can be found in
the » language strings documentation and supported country/region
strings in the » country/region strings documentation.
You might try to use $language = 'english-us'; instead of $language = 'en_US';.
I'm not sure about the language code for ar_eg, maybe Arabic_Egypt or Arabic_Egypt.1256.
If that doesn't work, you still have the option to use a PHP library, which implements gettext,
like https://github.com/oscarotero/Gettext or https://launchpad.net/php-gettext.
I made it work on windows with following code:
$locale = 'en_US';
if(!defined('LC_MESSAGES') || !setlocale(LC_ALL, $locale)) {
putenv("LC_ALL=$locale");
}
$domain = 'messages';
$path = "C:\wamp\www\project\locale";
$codeset = 'UTF-8';
bindtextdomain($domain, $path);
textdomain($domain);
bind_textdomain_codeset($domain, $codeset);
echo gettext("name");
I am dealing with this problem quite a while and this is not my first question to it, but still I can not get localization to work correctly.
I use WAMP server and php gettext.
A simple code I made for demonstration of my problem:
/* dirs */
$directory = realpath('../locale');
$domain = 'messages';
/* trying to find out correct setting - last one is winner */
$locale ="ro";
$locale ="ro_utf8";
$locale ="ro_RO";
$locale ="ro_RO.UTF-8";
$locale ="Romanian_Romania.1250";
putenv("LANG=".$locale);
print setlocale( LC_ALL, $locale);
setlocale( LC_ALL, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
/* STRING TO BE TRANSLATED */
echo _('string');
In ../locale directory there are many folders like this - $language_name/LC_MESSAGES/. These contain corresponding .po and . mo files with translations like this:
msgid "string"
msgstr "string SK"
in sk_SK folder
or this
msgid "string"
msgstr "string RO"
in ro, ro_RO, ro_RO.UTF8, ro_RO.UTF-8, romanian, Romanian_Romania, Romanian_Romania.1250 folders.
And what document looks like:
Romanian_Romania.1250 string SK
So you can see clearly - I setlocale() to romanian, yet my text is translated using the translation in sk_SK directory. What am I doing wrong?
Are there some functions I could use to get informations about $locale strings and directory names my server expects to find?
try to do your code like this:
setlocale(LC_ALL, 'ro_RO','Romanian');
it had worked in my project
You can help :
locale -a
And you can whach the language exist in your computer.
sudo locale-gen pt_PT
sudo locale-gen pt_PT.utf8
sudo update-locale
I have working localization in my project. Working means that my project gets translated to whatever language I have in the locale/sk folder, sk for slovak being my default system language.
Setting to any other language doesn't work. I have tried $lang = 'cs', 'cz', 'en', 'en_UK', 'en_UK.utf8' and others. Still, only the translation in the 'sk' folder is taken and still the setlocale() function returns false. I have tried to change default language in browser - no effect.
This is my code:
putenv("LANG=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("messages", realpath("../localem"));
textdomain("messages");
...
_("Welcome!")
I have also tried these:
putenv("LANGUAGE=$lang");
putenv('LC_ALL=$lang');
Any suggestions are welcome.
Edit:
$loc = array('nor');
if (setlocale(LC_ALL, $loc)==false) print ' false'; else print setlocale(LC_ALL, $loc);
'nor' prints Norwegian (Bokmĺl)_Norway.1252, 'rus' russian, but 'svk' prints false and so does 'cze'.
On the list all of these are mentioned:
http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.80%29.aspx
Windows uses another format for the locale setting, see MSDN: List of Country/Region Strings.
You can send a list of locales to setlocale by sending in an array, such as to get Norwegian month names and time formats:
setlocale(LC_TIME, array('nb_NO.UTF-8', 'no_NO.UTF-8', 'nor'));
Windows might however return strings in another encoding than UTF-8, so you might want to handle this manually (converting from cpXXXX to UTF-8).
I've searched hours for a solution for this including in the documentation.
trying to use gettext for hebrew translations,
using PHP 5.3.1 and wamp,
it prints out "hello world" and not the Hebrew equivalent
$directory = '/locale';
$domain = 'messages';
$locale ="he_IL";
putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows
setlocale( LC_ALL, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
echo _("hello world");
I use poedit to create the mo/po files, they are located on:
./locale/he_IL/LC_MESSAGES/messages.mo
the php file is at "./"
Why don't I get the hebrew text?
Ok solved,
Had to update to PHP version 5.3.5/5.3.10
and because I'm using windows I had to use this locale "Hebrew_Israel.1255"
instead of "he_IL" (that's how windows calls the hebrew locale anyway).
Of course I had to also rename the folders in the ./locale to "Hebrew_Israel.1255"
Now the system successfully chooses the locale
as pointed out by MarcB, PHP isn't magically going to translate the words into different language for you. but there are several different solutions out there to help you out. you can make use of google translator API to convert the text between different languages for example.
try {
$gt = new Gtranslate;
echo "Translating [Hello World] from English to German => ".$gt->english_to_german("hello world")."<br/>";
echo "Translating [Ciao mondo] Italian to English => ".$gt->it_to_en("Ciao mondo")."<br/>";
} catch (GTranslateException $ge) {
echo $ge->getMessage();
}
you can read more about google translator API here http://code.google.com/p/gtranslate-api-php/
You should check that "he_IL" locale is installed in your system. I don't know how to do it in Windows, but in Linux you can run "locale -a"(http://linux.about.com/library/cmd/blcmdl1_locale.htm) to see all locales installed.