Locale is switching/not loading properly (sometimes)

943 views Asked by At

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!

1

There are 1 answers

0
Carlton On

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