10

I have my php gettext default language in English let's say

I would like in one of my controller, to translate some words in 2 other languages and put them all in an array.

ideally I could do

$word_sv = gettext($word, 'sv_SV');
$word_fi = gettext($word, 'fi_FI');

but it doesn't exist.

Is the only way to change the overall gettext settings each time?

function setLang($lang){
    putenv("LC_ALL=$lang");
    setlocale(LC_ALL, $lang);
    bindtextdomain("myPHPApp", "./locale");
    textdomain("myPHPApp");
}

setLang('sv_SV');
$word_sv = gettext($word);
setLang('fi_FI');
$word_fi = gettext($word);

related: saw it on Google after : i18n with gettext but without the locale hassle?


Edit

here are the proposed answered solutions:

Community
  • 1
  • 1
  • Just a note, you can use Google's superior search to search specific sites by using a query string like: `site:stackoverflow.com php translate`. It'll search just SO for `php translate`. – Josh Oct 19 '12 at 14:04
  • Have you seen http://www.php.net/manual/en/function.dcgettext.php ? It might be of some help. – hakre Oct 28 '12 at 14:24
  • 1
    Gettext is clearly not designed for such a use case. The idea is that you set the language once and get all messages translated by calling the gettext function. What is the use case here? – Dave Kok Nov 01 '12 at 14:52
  • @DaveKok thx, the idea was to propose a word in all translations possible by a loop (have several lang dictionaries, they are in lang/sv_SV.utf8/LC_MESSAGES/messages.po lang/fi_FI.utf8/LC_MESSAGES/messages.po ... dcgettext can't help to switch between them I think –  Nov 01 '12 at 16:58

3 Answers3

1

I know the pains of using gettext, but its performance is what keeps me with it !

In your case, you might want to look at this little project ? i'm pretty sure this might help you !

this simply uses .ini files with translations, you can freely switch between files and echo the different languages for the same word.

https://github.com/Philipp15b/php-i18n

Mostafa Berg
  • 3,211
  • 22
  • 36
  • True, I've been on the verge of writing my own translation system, but whatever i write cannot compare to how fast gettext is, so i'm bound to it for the time being, but this is because my facebook game serves thousands of simultaneous views, so performance is very important to me, depending on your case, the project I posted might be of good use :) good luck and hack the gibson! ;) – Mostafa Berg Nov 01 '12 at 19:03
1

If you are bound to gettext here, Let the computer do the work for you.

You either have a list of words you then want to check against all languages, the do the wordlist for each language first. That will spare you some overhead to call the setlanguage function between each word and language.

If you want to go each language, each word, write the functions that way:

function gettext_by_lang($lang, $word) {
    putenv("LC_ALL=$lang");
    setlocale(LC_ALL, $lang);
    bindtextdomain("myPHPApp", "./locale");
    textdomain("myPHPApp");
    return gettext($word);
}

$word_sv = gettext_by_lang('sv_SV',  $word);
$word_fi = gettext_by_lang('fi_FI',  $word);

This would at least make your code more compact. Another idea that comes to mind is using a a parser for PO and MO files so that you can check for the data.

In PHP one of that is shipping with Wordpress / Glotpress:

Maybe this helps. That library is being maintained.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

I guess an obvious answer is to roll your own global function:

function getLocalText($string, $lang)
{
    putenv("LC_ALL=$lang");
    setlocale(LC_ALL, $lang);
    bindtextdomain("myPHPApp", "./locale");
    textdomain("myPHPApp");

    return gettext($string);
}

$word_fi = getLocalText($word, 'fi_FI');
Josh
  • 12,448
  • 10
  • 74
  • 118
  • 2
    yes if I don't forget to set it back to the initial locale at the end, is there another option to gettext? –  Oct 19 '12 at 14:18
  • 1
    else your answer doesn't really help me –  Oct 28 '12 at 11:19