Reputation: 165
I'm working on a project and I need to user translation for it. So I decided to use gettext in php but it's working sometimes.
So I have a folder named lng and in this on I have a php file who call my lang file to translate my page.
Here is the code :
<?php
if(isset($_GET['lang']) != '')
{
setcookie('lang',$_GET['lang'], time() + 365*24*3600, null, null, false, true);
$_SESSION['lang'] = $_GET['lang'];
$language = $_GET['lang'];
}
else if(isset($_SESSION['lang']) != '' )
{
$language = $_SESSION['lang'];
}
else if(isset($_COOKIE['lang']) != '')
{
$_SESSION['lang'] = $_COOKIE['lang'];
$language = $_SESSION['lang'];
}else{
$language = 'fr';
}
putenv("LANG=$language");
setlocale(LC_MESSAGES, $language);
$domain = 'trad';
bindtextdomain($domain, 'locale/');
textdomain($domain);
?>
So I can check my $_SESSION and $_COOKIE, no problem he give me 'en' or 'fr' but he doesn't translate my file and I don't know why.
For the folder in lng it's : locale/en/LC_MESSAGES/trad.po (or .mo). I try with LC_ALL and LC_MESSAGES but that doesn't change the result.
Did I miss something or made a wrong stuff?
Thanks a lot!
J.
Upvotes: 3
Views: 3207
Reputation: 1273
I had the same problem. Sometimes the strings were translated and sometimes don't. I have 3 servers for this application and 5 developers' machines, all with the same problem.
I solve it by removing:
bindtextdomain("domain", "/locale");
And linking the .mo file directly on the gettext default folder:
sudo ln -sf /myproject/locale/en/LC_MESSAGES/domain.mo /usr/share/locale/en/LC_MESSAGES/domain.mo
I am using Ubuntu 14.04.
Upvotes: 0
Reputation: 61
I've got the same problem. Restart Apache Service solved it for me
Upvotes: 6
Reputation: 1
I'm running into the same error. My case is a bit different, and I'm starting to think it has something to do with threading, despite it happens since I changed my code.
I have a language bar:
<?php
include_once (dirname(__FILE__) . "/resources/config.php");
?>
<div id='language_bar'>
<a style="margin-left:50px" href="./index.php?locale=es_ES">
<img src='<?php echo $config['paths']['images']['lang']?>/es_ES.gif'/>
</a>
<a href="./index.php?locale=en_UK">
<img src='<?php echo $config['paths']['images']['lang']?>/en_UK.gif'/>
</a>
<a href="./index.php?locale=de_DE">
<img src='<?php echo $config['paths']['images']['lang']?>/de_DE.gif'/>
</a>
</div>
And a config file with:
if (isset($_GET['locale'])) {
$locale = $_GET['locale'];
setcookie('locale', $locale, time() + 60 * 60 * 24 * 30);
} else {
if(isset($_COOKIE['locale'])) {
error_log('En _COOKIE');
$locale = $_COOKIE['locale'];
}
else {
$locale = $config['localization']['default_locale'];
setcookie('locale', $locale, time() + 60 * 60 * 24 * 30);
}
}
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
error_log('setlocale->'.setlocale(LC_ALL, "0")." Env ->". getenv("LC_ALL"));
error_log(_("Submit"));
My main page has some divs interacting via jQuery, and reloading in cascade. Sometimes, some of them (randomly) give a default string value.
by default, it is in spanish (es_ES), and after a few clicks forcing div refreshes, some string is printed in english (en_UK original string). And what is more. If I switch to german (de_DE), after a first refresh where I get every string in german, gettext starts to reurn strings in spanish, and after a while, in english.
Note that I added debug lines to php log. They are really interesting:
When things go right:
[Thu May 31 00:28:51 2012] [error] [client ::1] setlocale->es_ES Env ->es_ES
[Thu May 31 00:28:51 2012] [error] [client ::1] Aplicar, referer: xxxxxxxx/index.php
When do not:
[Thu May 31 00:29:45 2012] [error] [client ::1] setlocale->es_ES Env ->es_ES, referer: xxxxxxxxx/index.php
[Thu May 31 00:29:45 2012] [error] [client ::1] Submit, referer: xxxxxxxx/index.php
So I guess it's _() function failing (I'm always using the alias). Just in case, I looped for 10.000 times over the function, and it gave or 10.000 hits or 10.000 mistakes while translating, so it fails for a whole http request, or doesn't.
My apologies for writing so much, but I would really appreciate some help pointing me in the right direction. (This error occurs not just @ my localhost, but also at my online test server)
¿might have anything to do with the fact that I'm setting the locale for every connection?
My online "playground" is:
Linux server8.nixiweb.com 2.6.32-71.29.1.el6.x86_64 #1 SMP Mon Jun 27 19:49:27 BST 2011 x86_64
My server:
Linux filete 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 PHP Version 5.3.10-1ubuntu3.1
Note that both are 64bits
Upvotes: 0
Reputation: 3786
Aren't you using windows? If so, you must use windows locale names. Here is part of my class working for me on linux and also on windows (it simply has more options of locale names):
...
private function setLocaleByLang($lang)
{
$map = array(
'cs' => array('cs_CZ.UTF-8', 'cs_CZ', 'cs', 'czech'),
'en' => array('en_US.UTF-8', 'en_US', 'en', 'english'),
'de' => array('de_DE.UTF-8', 'de_DE', 'de', 'german'),
'pl' => array('pl_PL.UTF-8', 'pl_PL', 'pl', 'polish'),
'sk' => array('sk_SK.UTF-8', 'sk_SK', 'sk', 'slovak')
);
$locale = key_exists($lang, $map) ? $map[$lang] : $lang;
setlocale(LC_ALL, $locale);
putenv('LC_ALL=' . $lang); // for windows and gettext
}
...
Upvotes: 0