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