Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Wikipedia entry for GNU gettext shows an example where the locale is just the lanuage, "fr". Whereas the 'i18n gettext() “hello world” example' in SO 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". This covers making an "es" rather than "'es_MX'" message catalog and invoking the program with environment variable LANG set to "es".But this 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++ -ohellogt hellogt.cxx
xgettext -d hellogt -o hellogt.pot hellogt.cxx
msginit --no-translator -l es -o hellogt_spanish.po -i hellogt.pot
sed --in-place hellogt_spanish.po --expression='/#: /,$ s/""/"hola mundo"/'
sed --in-place hellogt_spanish.po --expression='s/PACKAGE VERSION/hellogt 1.0/'
mkdir -p ./es.utf8/LC_MESSAGES
msgfmt -c -v -o ./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.

share|improve this question

migrated from serverfault.com Feb 13 '11 at 6:04

This question came from our site for system and network administrators.

1  
In which distro are you working on? –  FerranB Jul 3 '09 at 20:29
1  
It's worth noting that there's no standard for locale names. Most unices define locale names of the form xx_YY where xx is a language subtag and YY is a region subtag, sometimes followed by a character set indication. –  Gilles Feb 16 '11 at 20:52

5 Answers 5

Wikipedia is probably not the best reference for stuff like this. It usually has very simple examples that may not be widely applicable, constructed for understanding concepts more than for practical considerations.

Why not use gnu's own documentation?

http://www.gnu.org/software/gettext/manual/gettext.html#Setting-the-POSIX-Locale

You can set LANGUAGE to "es" (or even "es:fr:en" for a priority list), but LANG would still need to be set to es_MX or something like that. The docs explain it fairly clearly.

share|improve this answer
up vote 4 down vote accepted

From Zac Thompson's link to GNU gettext utilities section 2.3 Setting the Locale through Environment Variables the sub-section The LANGUAGE variable:

In the LANGUAGE environment variable, but not in the other environment variables, ‘ll_CC’ combinations can be abbreviated as ‘ll’ to denote the language's main dialect. For example, ‘de’ is equivalent to ‘de_DE’ (German as spoken in Germany), and ‘pt’ to ‘pt_PT’ (Portuguese as spoken in Portugal) in this context.

Makes the point that "es" is an abbreviation that only LANGUAGE but not LANG supports.

share|improve this answer

The locale you use must be generated in the system. Use locale -a to see all generated locales. Locale source files must be present under /usr/share/i18n/locales/, and as far as I can see, all are of type 'language_COUNTRY'. If you really must use 'es' locale, you can prepare necessary files, you can modify /etc/locale.gen to include 'es' and run locale-gen to generate it. Otherwise, use an 'es' locale with a country.

share|improve this answer

Might this be because Spanish is spoken in many different countries and may have variations and quirks between dialects? Same as en_US, en_CA, or en_GB etc.

In fact, here are your options - I think you can guess most of the countries (AR=Argentina, BO=Bolivia, CL=Chile etc)

es_AR
es_AR.iso88591
es_AR.utf8
es_BO
es_BO.iso88591
es_BO.utf8
es_CL
es_CL.iso88591
es_CL.utf8
es_CO
es_CO.iso88591
es_CO.utf8
es_CR
es_CR.iso88591
es_CR.utf8
es_DO
es_DO.iso88591
es_DO.utf8
es_EC
es_EC.iso88591
es_EC.utf8
es_ES
es_ES@euro
es_ES.iso88591
es_ES.iso885915@euro
es_ES.utf8
es_ES.utf8@euro
es_GT
es_GT.iso88591
es_GT.utf8
es_HN
es_HN.iso88591
es_HN.utf8
es_MX
es_MX.iso88591
es_MX.utf8
es_NI
es_NI.iso88591
es_NI.utf8
es_PA
es_PA.iso88591
es_PA.utf8
es_PE
es_PE.iso88591
es_PE.utf8
es_PR
es_PR.iso88591
es_PR.utf8
es_PY
es_PY.iso88591
es_PY.utf8
es_SV
es_SV.iso88591
es_SV.utf8
es_US
es_US.iso88591
es_US.utf8
es_UY
es_UY.iso88591
es_UY.utf8
es_VE
es_VE.iso88591
es_VE.utf8
share|improve this answer

I'm not really sure about this, but I've been working with Joomla and others CMS and the code for Spanish - Spain is: es_ES

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.