4

I have the next files in my php project:

libraries/locale/es_ES/LC_MESSAGES/messages.po
libraries/locale/es_ES/LC_MESSAGES/messages.mo
libraries/locale/es/LC_MESSAGES/messages.po
libraries/locale/es/LC_MESSAGES/messages.mo

Both are the same file edited with PoEdit just differenced by Catalog->Properties->Language (es and es_ES respectively)

And this code into localization.php file

$language = "es_ES.UTF-8";
putenv("LANG=$language");
setlocale(LC_ALL, $language);
bindtextdomain(STRING_DOMAIN, LOCALE_PATH);
textdomain(STRING_DOMAIN);

echo "Test translation: "._('string to translate');

This code works fine and 'string to translate' is displayed correctly. However if I try to use the generic 'es' code:

$language = "es.UTF-8";

...String is not translated. Seems to be related to locales installed in my ubuntu (es_ES.utf8 exists but not es.utf8)

Can I force gettext to use es.UTF-8 file?

Gonzalo Cao
  • 2,286
  • 1
  • 24
  • 20

1 Answers1

2

As a workaround, you can always use localedef to compile a new locale based on an existing one. To create es based on es_ES.UTF-8:

 localedef -i es_ES -f UTF-8 es.UTF8

But here comes some important questions which covers areas beyond simple loading of translation files. Since your locale-specific information like date and time formats, measurements, etc are depending on installed locales, it is always a good idea to have a plan regarding use of locales. Assuming that you are using es_ES for Spanish (Spain), what is es? Is it intended for a specific flavor of Spanish like es_IC (Canary Islands) or some Latin American flavor?

Here is a clarifying example; if I want to design a customized repertoire of locales to cover various flavors of Spanish, I will do it like this; first I add those locales which are easily installable with locale-gen:

  locale-gen es_ES.UTF-8 es_MX.UTF-8 es_AR.UTF-8

Then I would like to have es_US based on es_MX and es_419 (419 is the geographical area code for Latin America) based on es_AR:

  localedef -i es_MX -f UTF-8 es_US.UTF-8
  localedef -i es_AR -f UTF-8 es_419.UTF-8
Shervin
  • 1,936
  • 17
  • 27