1

I have a very annoying problem with the language/locale for our site. I've been looking around here but haven't found anything similar.

So we support a few different languages and have everything set up with .po and .mo files which works fine most of the time. Sometimes it switches language when you load something or refresh a page. There's not really a pattern to it either, sometimes 6 'en' in a row and then 'sv' and sometimes every second 'sv' and 'en'.

Ex. If you want to edit a user you get a popup which starts by including authorize.php where we check the language and set it accordingly with this function:

function _set_locale ($lang){
    switch($lang) {
        case 'sv':  $locale = "sv_SE"; break;
        case 'en':  $locale = "en_SG"; break;
        case 'nl':  $locale = "nl_NL"; break;
        case 'dk':  $locale = "da_DK"; break;
        case 'vi':  $locale = "vi_VN"; break;
        case 'zh':  $locale = "zh_CN"; break;
        default:    $locale = "en_SG"; break;
        }
    putenv("LANGUAGE=$locale");
    setlocale(LC_ALL, "$locale.UTF-8");
    bindtextdomain("messages", "./locale/");
    textdomain("messages");

I was running an account with Swedish, 'sv' set and refreshed the edit user popup 10 times and debugged this function, $lang was 'sv' and I logged setlocale(LC_ALL, 0) which returned 'sv_SE.UTF-8' every time but the page was displayed in English 5-6 times.

It seems like it's only switching from the current language to English which is the original language, so I figure that it's not getting translated. It doesn't look like we're setting the wrong/default language, it just ignores/don't have time? to set the language.

The language is not saved in cookies and some accounts just have 1 language (which is not English) and still gets this.

In the above ex. when we return from authorize.php we don't use any language variable in the actual 'edit user' page. So it shouldn't be able to change it there. (I also debugged and checked the language when returning from auth. and it was Swedish every time).

I don't expect anyone to be able to solve this by the info I've given I'm just interested if someone has experienced this or have any idea why it sometimes 'switches' please let me know if I can attach some more code to sort this out.

Thanks!

Sup3rgnu
  • 153
  • 4
  • Are you aware of the process-based caveats listed on [the `setlocale` manual page](http://php.net/manual/en/function.setlocale.php)? It sounds like you might be encountering a case of inherited locales from other requests. – Charles Jan 17 '12 at 00:58
  • Thanks for answering, yes I've read that but it says "If you are running PHP on a multithreaded server API like IIS or Apache on Windows" and we're running on linux. I don't know much about what's going on behind the scenes tho, so maybe we're running some weird configuration or something that allows this? – Sup3rgnu Jan 17 '12 at 15:52
  • 1
    I've heard stories -- unconfirmed ones, mind you -- that manipulating the locale through `setlocale` and `putenv` actually impact the Apache child instead of just the single PHP request, which can cause the next request to hit that Apache child to be in an unexpected locale by default. Unfortunately I don't recall more details, but maybe it might be enough to help you somehow... – Charles Jan 17 '12 at 17:08
  • Did you ever fix this? I am having the same kind of issue. It looks like the requested lang file isn't loaded. The nl_NL is loaded corretly every 1 out 3 page loads, en_GB loads correct everytime – Parcye Feb 25 '15 at 22:50
  • @Parcye Hi sorry for a late reply. I'm afraid that I don't remember what we ended up doing to solve this and I'm no longer working on that project. I hope you'll find a way! – Sup3rgnu Apr 24 '15 at 11:37

1 Answers1

0

I had a similar intermittent problem PHP gettext and vagrant running ubuntu, it was showing the right text on the 3rd request.

Try one of the following, I think it will depend how you have PHP running with Apache

sudo service php5-fpm restart

sudo service apache2 restart

I think it stemmed from me playing around with the value passed to setLocale()

Community
  • 1
  • 1
Carlton
  • 5,533
  • 4
  • 54
  • 73