0

This is the code that I'm using to set the language:

$locale = 'en_US';
putenv('LANG='.$locale);
setlocale(LC_ALL, $locale);
bindtextdomain($locale, ROOTPATH.'/admin/locale');
bind_textdomain_codeset($locale, 'UTF-8');
textdomain($locale);
echo setlocale(LC_ALL, 0).'<br><br>';
echo _('register_success');

.

Breaking it down:

$locale = 'en_US';

Setting this to 'pt_PT' does the job for Portuguese, but if it is set to other than that, nothing gets translated.

putenv('LANG='.$locale);
setlocale(LC_ALL, $locale);
bindtextdomain($locale, ROOTPATH.'/admin/locale');

(ROOTPATH is the var for the project's root folder path)

bind_textdomain_codeset($locale, 'UTF-8');
textdomain($locale);

This next part always outputs this: LC_COLLATE=C; LC_CTYPE=Portuguese_Portugal.1252; LC_MONETARY=C; LC_NUMERIC=C; LC_TIME=C

echo setlocale(LC_ALL, 0).'<br><br>';

And the string I'm testing

echo _('register_success');

I've already tried setlocale(LC_ALL, NULL) and setlocale(LC_ALL, '') before setting it to 'en_US', but it did not help either, although it made echo setlocale(LC_ALL, 0) print only Portuguese_Portugal.1252.

I've also tried to change my system's language and all, but it was still picking up that Portuguese_Portugal.1252, not quite sure how ...

Here's the folder structure for the .po/.mo files.

root
    - admin/
        - locale/
            - en_US/
                - LC_MESSAGES/
                    - en_US.mo
                    - en_US.po
            - pt_PT/
                - LC_MESSAGES/
                    - pt_PT.mo
                    - pt_PT.po

EDIT

These are the values setlocale() is returning for the different codes (I don't know if it helps, but they didn't seem too consistent to me):

setlocale(LC_ALL, 'pt_PT');
echo setlocale(LC_ALL, '0');

Outputs: LC_COLLATE=C;LC_CTYPE=Portuguese_Portugal.1252;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C

setlocale(LC_ALL, 'PT');
echo setlocale(LC_ALL, '0');

Outputs: PT

setlocale(LC_ALL, 'US');
echo setlocale(LC_ALL, '0');

Outputs: English_United States.1252

setlocale(LC_ALL, 'EN');
echo setlocale(LC_ALL, '0');

Outputs: EN

setlocale(LC_ALL, 'en_US');
echo setlocale(LC_ALL, '0');

Outputs: LC_COLLATE=C;LC_CTYPE=Portuguese_Portugal.1252;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C

P3t3r6
  • 1,398
  • 2
  • 9
  • 12
  • When you say it's defaulting back; at what point? Secondly, do you have a `en_US` LC_MESSAGES folder? Are you sure it's not looking at `en_US` and not `en_US.utf8`? – MackieeE Apr 27 '16 at 20:09
  • I don't think it even sets anything, cause if I echo the setlocale() right after I've set it, the output is still the same (LC_ ... Portuguese, etc, etc ...). Yes, I have. I edited the question to include the folder structure. The only reason that leads me to believe that everything's ok with the files is the fact that the pt_PT works just fine, and the files for en_US are practically equals. – P3t3r6 Apr 27 '16 at 20:51
  • Should I have separate folders with the `.utf8` in the end, for each language? – P3t3r6 Apr 27 '16 at 20:53
  • it all depends on what `setlocale()` is returning, if it's returning `en_US`, it'll look for `locale/en_US` - so it's returning `Portuguese_Portugal.1252` you need a folder `locale/Portuguese_Portugal.1252` as that's where the `bindtextdomain()` is looking at – MackieeE Apr 27 '16 at 21:38
  • But it is working fine for the pt_PT even though it returns `Portuguese_Portugal.1252` and the folder is named `pt_PT` – P3t3r6 Apr 27 '16 at 21:57
  • Your examples are showing you are re-setting `setlocale`, [`setlocale()`](http://php.net/manual/en/function.setlocale.php) *returns* you the newly set locale, for example `$lang = setlocale( LC_ALL, 'fr_FR' )`, `echo $lang` – MackieeE Apr 28 '16 at 07:24

1 Answers1

0
setlocale(LC_ALL, '')

works for me.

softwarevamp
  • 827
  • 10
  • 14