gettext() - on 3 languages 2 works one not - php

I've got a webpage that contains 3 languages - Polsih, English and German. English and German translations works perfect but Polish doesn't work at all. Where could be the problem? Here is configuration of gettext:
$locale = "pl_PL";
if (isSet($_GET["lg"])) $locale = $_GET['lg']."_". strtoupper($_GET['lg']);
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale.'.UTF-8');
bindtextdomain("messages", "./locale");
bind_textdomain_codeset("messages", 'UTF-8');
textdomain("messages");

Your test code is not particularly helpful. We'll simplify it this way:
var_dump( setlocale(LC_ALL, 'pl_PL.UTF-8') );
You vaguely suggest in your comments that you're getting FALSE. What does it mean? According to the documentation (I suppose you've already checked it, I'm just posting it here for completeness):
Returns the new current locale, or FALSE if the locale functionality
is not implemented on your platform, the specified locale does not
exist or the category name is invalid.
So, how do you get a list of available locales? As you've been said:
echo `locale -a`;

Related

Question and a problem about setlocale on windows according to information on php.net

Just a question about some information on setlocale i found on php.net
http://php.net/manual/en/function.setlocale.php
It says:
Note:
On Windows, setlocale(LC_ALL, '') sets the locale names from the system's regional/language settings (accessible via Control Panel).
Does that mean if i add setlocale(LC_ALL, '') to my php, it should be the same local as my system? Because i have done that and it's still English with months.
When i echo out setlocale(LC_ALL, 0) i get this:
LC_COLLATE=C;LC_CTYPE=Norwegian (Bokm�l)_Norway.1252;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C
and when i echo out setlocale(LC_ALL, '') i get this:
Norwegian (Bokm�l)_Norway.1252
So i don't really know what to do from the echo it looks like its set to Norwegian and when i use setlocale(LC_ALL, '') i should also set everything to Norwegian including time, but it does not.
Here is also the code i use to change the time format.
date('d.F', strtotime($row['date2']))
SOLUTION: Use strftime() instead of strtotime().
strtotime does not take into account the set locale.
Before:
date('d.F', strtotime($row['date2']))
Output: 17.February
After:
setlocale(LC_ALL, '');
strftime('%e.%B',strtotime($row['date2']))
Output: 17.februar

Gettext php, idea ?

I installed gettext on a server and want to use for a php script..
I use with 3 languages. The original website is written in english..
Other are french and Italian.
I can without problem change language to French or Italian.. But when I want load the en_EN.mo file to replace the origine text of the php file, the translation are not loaded.
I try many things.. Remove file, replace name.. restart apache..
Nothing.. it work perfectly with all other langage, but impossible to use a english file.
I create new language.. it work too.. but never the en_EN..
All my files langage are in the same tree style
locale
en_EN
LC_MESSAGES
en_EN.mo
fr_FR
LC_MESSAGES
fr_FR.mo
...
if I force to use..
$directory = './locale';
$domain ='en_EN';
$locale ='en_EN';
putenv('LC_ALL='.$locale);
setlocale(LC_ALL, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
no result..
My problem is that I have no idea how to debug it ? Where can I find a log or something to help ? Where see errors ?
It's strange that it work with all lang, but not english.. I use Poedit to create my file, I have create the file many time.. in the same way that other..
Please share your idea :)
I create new language.. it work too.. but never the en_EN..
You misunderstand what the locale string means. It is not “2 language code letters followed by the same letters in uppercase” (that wouldn’t make any sense, would it), it is “language code followed by country code” per ISO 3166. And “EN” is not only not a valid country code for any country that speaks English, it’s not a valid country at all. You’re asking WordPress to run under the English locale (presumably), but only providing it translation files for “English in a made-up country” locale that WP is never going to look for.
You’re thinking of en_US.

PHP Enconding - Formatting cedilla in Portuguese-BR is not working

I'm facing an issue related to charset formatting words in Portuguese-BR which has cedilla in it.
When I set setlocale to 'portuguese-brazil' with utf-8 enconding, PHP seems not understanding it.
As the image shows bellow, I try to convert the current month in English to Portuguese:
As we can see, the value above should show Março (which means March in English) to the final user.
I tried to navigate through Stack Overflow in Portuguese and here, but none of those questions and solutions worked for me.
In my HTML file, my meta tag is defined as following:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
The file it self was saved by UTF-8 w/o BOM formatted as well.
The snippet code which show this month:
<div id="ow-marketplace" class="col-xs-12 col-sm-12 col-md-12 table-responsive">
<?php
setlocale( LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'portuguese-brazilian' );
date_default_timezone_set( 'America/Sao_Paulo' );
$data = strftime( '%B/%y', strtotime('today'));
echo "<h4 class=\"page-header\">Vendas Atuais do mês de ". ucfirst($data) ."</h4>";
?>
<div class="box-content">
I don't know how can I solve this problem, but I think charset seems not working as expected.
Can anyone help me with this?
If I need to post any further information, please advise me
The most likely problem is your locale. PHP will try to set the locales in the given order, setting the first one it can find. If pt_BR is not using UTF-8 on your system, your results won't be UTF-8 encoded. Since you're trying pt_BR.utf-8 as the second choice, PHP may be preferring the non-UTF-8 version over it.
Check what locales you have installed:
$ locale -a
For Windows, look here:
https://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.90%29.aspx
(unconfirmed information, I don't know if there's a more Windows-y way to do this).
Prefer UTF-8 locales first:
setlocale(LC_ALL, 'pt_BR.utf-8', 'pt_BR', 'portuguese-brazilian');
Test what locale was actually chosen:
echo setlocale(..);

Make translations in multiple languages

I have my php gettext default language in English let's say
I would like in one of my controller, to translate some words in 2 other languages and put them all in an array.
ideally I could do
$word_sv = gettext($word, 'sv_SV');
$word_fi = gettext($word, 'fi_FI');
but it doesn't exist.
Is the only way to change the overall gettext settings each time?
function setLang($lang){
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
}
setLang('sv_SV');
$word_sv = gettext($word);
setLang('fi_FI');
$word_fi = gettext($word);
related: saw it on Google after : i18n with gettext but without the locale hassle?
Edit
here are the proposed answered solutions:
https://github.com/Philipp15b/php-i18n (seems best solution)
http://glotpress.trac.wordpress.org/browser/trunk/pomo (could use it if I find a good doc or tuto ;))
change locale on the fly, probably not good
I know the pains of using gettext, but its performance is what keeps me with it !
In your case, you might want to look at this little project ? i'm pretty sure this might help you !
this simply uses .ini files with translations, you can freely switch between files and echo the different languages for the same word.
https://github.com/Philipp15b/php-i18n
If you are bound to gettext here, Let the computer do the work for you.
You either have a list of words you then want to check against all languages, the do the wordlist for each language first. That will spare you some overhead to call the setlanguage function between each word and language.
If you want to go each language, each word, write the functions that way:
function gettext_by_lang($lang, $word) {
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
return gettext($word);
}
$word_sv = gettext_by_lang('sv_SV', $word);
$word_fi = gettext_by_lang('fi_FI', $word);
This would at least make your code more compact. Another idea that comes to mind is using a a parser for PO and MO files so that you can check for the data.
In PHP one of that is shipping with Wordpress / Glotpress:
http://glotpress.trac.wordpress.org/browser/trunk/pomo
Maybe this helps. That library is being maintained.
I guess an obvious answer is to roll your own global function:
function getLocalText($string, $lang)
{
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("myPHPApp", "./locale");
textdomain("myPHPApp");
return gettext($string);
}
$word_fi = getLocalText($word, 'fi_FI');

Gettext not detecting utf8 properly in PHP

I have a PHP application using Gettext as the i18n engine. The translation works fine, the only problem is that I'm having encoding issues with UTF8 characters. My PHP code to load gettext is something like this:
bindtextdomain( $domain, PATH_BASE . DS . "language" . DS );
$this->utf8Encode = strtolower($encoding) == "utf-8";
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
My templates render the pages using the utf8 charset and I've tried just about anything to load the proper charset. For the current locale I'm loading SL_sl, the names appear correctly but have issues with UTF8 chars, so where it should appear Država, it shows up Dr?ava
So, it has happened before, and now it happened again, I found the solution myself! The problem was that like I said to #bozdoz, I was converting UTF8 text already, but I didn't realized that the gettext function returned a UTF8 string, so if you do this:
$encoded = utf8_encode($utf8String);
Then you'll have a really nasty bug when $utf8String is an actual UTF8 string. Therefore I did some modifications to my code and the translation method (simplified) ended up like this:
$translation = gettext($singular);
$encoded = $this->utf8Encode ? $this->Utf8Encode($translation) : $translation;
And the Utf8Encode method is like this:
private function Utf8Encode( $text )
{
if ( mb_check_encoding($text, "utf8") == TRUE ){
return $text;
return utf8_encode($text);
}
I hope that if somebody has the same error this can help!
From the partial information I can suggest you take a look at the actual mo/po files, in poedit there are several warnings about utf8 encoding. Assuming that everything else is correct (meta, headers, etc) it's the only thing left to check
Try encoding it with utf8_encode(). I can't really tell from your code, but perhaps it could be implemented like this:
utf8_encode($domain);

Categories