Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force gettext to translate a specific language, independently from current language?

How can i tell the function gettext() to translate a word to a specific language independently from the language currently set?

Let's take an example

Let us assume the default language is english. Let's take the word tree. In italian it will be albero.

If i am browsing the english version of my website, all the content of the page will be translated in english. In particular, if i have in my php code the line echo _('tree');, tree will be outputted.

What i would love to accomplish is to output albero when i'm browsing the english version of the website. I would love to tell _() to translate only that specific word in that specific part of the page in italian, even if the current language is in english. In my head it's something like _('tree', 'it_IT') where it_IT tells the gettext to ignore the English language and override it with with Italian only for that call.

Is it possible to do that with gettext()? If yes, how? If no, any other solutions?

like image 735
Alberto Fontana Avatar asked Feb 15 '23 06:02

Alberto Fontana


1 Answers

Unfortunately, GNU gettext does not support this.

You can fake it by temporarily changing your locale:

setlocale(LC_ALL, 'it_IT');
echo _('tree');
setlocale(LC_ALL, 'en_US');
like image 168
Chris Avatar answered Feb 16 '23 20:02

Chris