views:

233

answers:

1

Wikipedia entry for GNU gettext shows an example where the locale is just the lanuage, "fr". Whereas the i18n gettext() “hello world” example has the locale value with both the language and country, "es_MX". I have modified the "es_MX" example to use just the lanuage, "es", but it produces the English text rather the expected Spanish.

cat >hellogt.cxx <<EOF
// hellogt.cxx
#include <libintl.h>
#include <locale.h>
#include <iostream>
int main (){
    setlocale(LC_ALL, "");
    bindtextdomain("hellogt", ".");
    textdomain( "hellogt");
    std::cout << gettext("hello, world!") << std::endl;
}
EOF
g++ -o hellogt hellogt.cxx
xgettext --package-name hellogt --package-version 1.2 --default-domain hellogt --output hellogt.pot hellogt.cxx
msginit --no-translator --locale es --output-file hellogt_spanish.po --input hellogt.pot
sed --in-place hellogt_spanish.po --expression='/"hello, world!"/,/#: / s/""/"hola mundo"/'
mkdir --parents ./es.utf8/LC_MESSAGES
msgfmt --check --verbose --output-file ./es.utf8/LC_MESSAGES/hellogt.mo hellogt_spanish.po
LANG=es.utf8 ./hellogt

According to Controlling your locale with environment variables:

environment variable, LANGUAGE, which is used only by GNU gettext ... If defined, LANGUAGE takes precedence over LC_ALL, LC_MESSAGES, and LANG.

LANGUAGE=es.utf8 ./hellogt

produces the expected Spanish text rather than English.

But this does not explain why "LANG=es" does not work.

+3  A: 

Hmmm... it sounds like a platform problem. I did precisely the same thing under Mac OSX and got the expected results. I suspect that your version of linux doesn't like LANG=es.utf8. Check your local system documentation surrounding things like setlocale(3) or xlocale(3) if the latter is even supported.

IIRC, locale identifiers containing only a primary identifier which was an ISO-639 language code had the ISO-3166 country code of the primary speaking country appended to them. Then the resulting locale lookup was performed by looking in /usr/share/locale. I looked in the locale directory on my MacBook and there is a directory named es as well as es_ES. It may be a simple system configuration issue.

I would highly recommend using ICU instead of gettext and the locale stuff offered by the Standard Library if you are developing a commercial application. Gettext will work for simple message lookup but it doesn't offer nearly what you need to really do a fully internationalized application.

D.Shawley