gettext only translate to English in PHP - php

I'm spanish, and making tests internacionalizing a text width PHP, i only get it translated to english.
I got this structure of files:
locale/en_US/LC_MESSAGES/con los ficheros messages.mo y messages.po
locale/es_ES/LC_MESSAGES/con los ficheros messages.mo y messages.po
locale/fr_FR/LC_MESSAGES/con los ficheros messages.mo y messages.po
Every files have the key word "Servicios" translated to each languaje.
And in PHP i have this code:
<?php
putenv("LANG=en_US");
setlocale(LC_ALL, "en_US");
bindtextdomain("messages", "locale");
textdomain("messages");
?>
When i put the code 'en_US' show the good translation, but when i change it to 'es_ES' or 'fr_FR' that way:
<?php
putenv("LANG=es_ES");
setlocale(LC_ALL, "es_ES");
?>
or
<?php
putenv("LANG=fr_FR");
setlocale(LC_ALL, "fr_FR");
?>
still showing the translation to English
I am working on Widnows 7 and the function
echo $_SERVER['HTTP_ACCEPT_LANGUAGE'] ;
returns to
"es-ES,es;q=0.8"
always,
Which problem could it be?
Thank you

It is quite likely that the languages are not installed on the server your running the script on - do you have shell access to the server? Then try
locale -a
to see which locales are installed. Also have a look here Is it feasible to rely on setlocale, and rely on locales being installed?
NOTE:
be careful with the LC_ALL setting, as it may introduce some unwanted conversions. For example, I used
setlocale (LC_ALL, "Dutch");
to get my weekdays in dutch on the page. From that moment on (as I found out many hours later) my floating point values from MYSQL where interpreted as integers because the Dutch locale wants a comma (,) instead of a point (.) before the decimals. I tried printf, number_format, floatval.... all to no avail. 1.50 was always printed as 1.00 :(
When I set my locale to :
setlocale (LC_TIME, "Dutch");
my weekdays are good now and my floating point values too.

Related

gettext - Cant get the Lithuanian language code to work

I have implimented gettext with my site and am providing 15 different translations which all work but one
Lithuania - I can't seem to figure out the language/country code to make it work.
putenv("LC_ALL=lt_LT");
setlocale(LC_ALL, "lt_LT");
bindtextdomain("messages", "/home/path/to/locale");
textdomain("messages");
bind_textdomain_codeset("messages", 'UTF-8');
I first check my locale -a on my box and I have the following enabled
lt_LT
lt_LT.iso885913
lt_LT.utf8
I have tried all three and they do not work.
Any ideas of any other codes I can try?

Convert en_US to to en-US

I'm writing a PHP application that supports multiple languages.
When setting the locale in PHP, I am required to provide a value defined in, what I believe to be, RFC 1766 / ISO 639, according to the setlocale documentation.
setlocale( LC_ALL, 'en_US' );
var_dump( setlocale( LC_MESSAGES, '0' ) );
// string(5) "en_US"
When using this locale to describe the HTML lang attribute, validation fails because it is not formatted to RFC 5646. The RFC 5646 value for this language is actually en-US (note the use of a hyphen instead of an underscore).
Using this value in PHP's setlocale function, as above, results in the following output:
string(1) "C"
I have no idea why it is returning a value of C, but I presume it is because the locale provided was incorrectly formatted. C being the original server default, which is described as ASCII (thanks to #Cheery for the reference).
So, I'm wondering what I should do about that. I could, feasibly, use PHP's str_replace function to switch - to _ before outputting the lang attribute, like so:
<?php setlocale( 'en_US' ); ?>
<!doctype html>
<html lang="<?= str_replace( '_', '-', setlocale(LC_MESSAGES, '0') ); ?>">
...
But, I'm concerned that there may be other differences between the two language specifications that could yield an unexpected problem down the road. If so, is there a preferred way to translate the language codes already in PHP, or a translation class that can be used?
Bonus question, why does my server default to value of C for the locale?
You need to have in mind that setLocal accept many types of "locale" names like names and mixed things, for example in (from php documentation):
$loc_de = setlocale(LC_ALL, 'de_DE#euro', 'de_DE', 'de', 'ge');
You have 'de_DE#euro' which isn't a valid HTML lang code.
So first, you need to ensure that is in the format lang_region before trying to convert it.
You can set local language codes using "setlocale".
You can find here it's documentation as well as this and other examples
Example where they try different possible locale names for german:
<?php
/* Set locale to Dutch */
setlocale(LC_ALL, 'nld_nld');
/* Output: vrijdag 22 december 1978 */
echo strftime("%A %d %B %Y", mktime(0, 0, 0, 12, 22, 1978));
/* try different possible locale names for german as of PHP 4.3.0 */
$loc_de = setlocale(LC_ALL, 'de_DE#euro', 'de_DE', 'deu_deu');
echo "Preferred locale for german on this system is '$loc_de'";
?>

Trying to use gettext without country code ('es' instead 'es_ES')

I have the next files in my php project:
libraries/locale/es_ES/LC_MESSAGES/messages.po
libraries/locale/es_ES/LC_MESSAGES/messages.mo
libraries/locale/es/LC_MESSAGES/messages.po
libraries/locale/es/LC_MESSAGES/messages.mo
Both are the same file edited with PoEdit just differenced by Catalog->Properties->Language (es and es_ES respectively)
And this code into localization.php file
$language = "es_ES.UTF-8";
putenv("LANG=$language");
setlocale(LC_ALL, $language);
bindtextdomain(STRING_DOMAIN, LOCALE_PATH);
textdomain(STRING_DOMAIN);
echo "Test translation: "._('string to translate');
This code works fine and 'string to translate' is displayed correctly. However if I try to use the generic 'es' code:
$language = "es.UTF-8";
...String is not translated. Seems to be related to locales installed in my ubuntu (es_ES.utf8 exists but not es.utf8)
Can I force gettext to use es.UTF-8 file?
As a workaround, you can always use localedef to compile a new locale based on an existing one. To create es based on es_ES.UTF-8:
localedef -i es_ES -f UTF-8 es.UTF8
But here comes some important questions which covers areas beyond simple loading of translation files. Since your locale-specific information like date and time formats, measurements, etc are depending on installed locales, it is always a good idea to have a plan regarding use of locales. Assuming that you are using es_ES for Spanish (Spain), what is es? Is it intended for a specific flavor of Spanish like es_IC (Canary Islands) or some Latin American flavor?
Here is a clarifying example; if I want to design a customized repertoire of locales to cover various flavors of Spanish, I will do it like this; first I add those locales which are easily installable with locale-gen:
locale-gen es_ES.UTF-8 es_MX.UTF-8 es_AR.UTF-8
Then I would like to have es_US based on es_MX and es_419 (419 is the geographical area code for Latin America) based on es_AR:
localedef -i es_MX -f UTF-8 es_US.UTF-8
localedef -i es_AR -f UTF-8 es_419.UTF-8

Setlocale returns false (WAMP)

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).

Bad encoding when using on strftime in spanish?

I'm trying to echo the date with strftime but I'm getting bad encoding on utf-8 only characters. (accented characters basically)
setlocale(LC_TIME, 'spanish');
define("CHARSET", "iso-8859-1");
echo strftime("%A, %d de %B",strtotime($row['Date']));
Is there any problem in this part of the code? Everything is encoded in utf-8 and echoing a 'á' character above it displays the character correctly.
Try adding utf8_encode()
setlocale(LC_TIME, 'spanish');
define("CHARSET", "iso-8859-1");
echo utf8_encode(strftime("%A, %d de %B",strtotime($row['Date'])));
I'm a bit late, but Googling around I found this post. And the answers weren't appropriate in my case.
I'm experiencing the same problem as the OP, but my locale is fr_FR and everything works fine on my computer but not on the dev server.
If I add an iconv (as most people suggest when you Google this issue), it works on the dev server but not on my computer, so I needed a "bulletproof" solution that would work the same everywhere (as there is also a production server).
So, the issue here is with the setlocale, this function changes the locale on the current execution, but every locale is associated with a charset and if none is specified, it falls back to the default one of your system (I think, in my case it was falling back to ISO-8859-1 when using the fr_FR locale). You can list all available locales on your computer/server with the locale -a command. You will most likely see the locale you want, with ".UTF-8" (in my case "fr_FR.UTF-8"), that's how you must set it: setlocale('fr_FR.UTF-8');
perhaps:
echo iconv("iso-8859-1","utf-8",strftime("%A, %d %B",strtotime($row['Date'])));
For those that don't have iconv, you can use the mb function to convert the stftime encoded string to utf-8
echo mb_convert_encoding(strftime("%A, %d de %B",strtotime($row['Date'])), 'UTF-8', mb_internal_encoding());

Categories