Gettext works sometimes

I am working on a project, and for this I need a translation. So I decided to use gettext in php, but it sometimes works.

So, I have a folder called lng, and I have a php file that calls 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, it gave me "en" or "fr" without problems, but it does not translate my file, and I do not know why.

For a folder in lng this is: locale / en / LC_MESSAGES / trad.po (or .mo). I am trying to use LC_ALL and LC_MESSAGES, but that does not change the result.

Am I missing something or doing the wrong stuff?

Thanks a lot!

J.

0
source share
4 answers

I have the same problem. Restarting Apache service resolved this for me

+2
source

Don't you use windows? If so, you should use the Windows locale names . Here is the part of my class that works for me on linux as well as on windows (it just has more options for 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 } ... 
0
source

I came across the same error. My business is a little different, and I'm starting to think that this has something to do with streaming, despite this, 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 the configuration 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")); 

On my main page there are some divs interacting via jQuery and reload in cascade. Sometimes, some of them (randomly) give a string value by default.

by default, it is in Spanish (es_ES), and after a few clicks of the mouse, causing the div to be updated, some lines are printed in English (original line en_UK). And further. If I switch to German (de_DE), after the first update, where I get each line in German, gettext starts returning lines in Spanish and after a while in English.

Please note that I have added debug lines to the php log. They are really interesting:

When everything goes 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 

If this is not the case:

 [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 think the _ () function is not working (I always use an alias). Just in case, I looped 10,000 times over the function, and it gave either 10,000 hits or 10,000 errors in the translation, so it fails for the whole HTTP request or not.

My apologies for the fact that you wrote so much, but I am very grateful for the help sent to me in the right direction. (This error occurs not only @my localhost, but also on my online test server)

¿May have anything to do with the fact that I set the locale for each connection?

My online site:

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-generi # 39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 PHP Version 5.3.10-1ubuntu3.1

Please note that both are 64 bits

0
source

I had the same problem. Sometimes the lines were translated, and sometimes not. I have 3 servers for this application and 5 development machines, all with the same problem.

I solve it by removing:

 bindtextdomain("domain", "/locale"); 

And linking the .mo file directly to the default gettext 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.

0
source

Source: https://habr.com/ru/post/915117/


All Articles