Erik Johansson
Erik Johansson

Reputation: 323

How does gettext figure out what languages are available

I recently spent some time translating a PHP application, running on Linux with Apache2. To get gettext to work I needed to touch system directories for every language I wanted to support, so I wonder is there a easier way to support getting text from gettext in all languages.

My locales files are in a sub directory of my project:

./locale/sv_SE/LC_MESSAGES/default.po
./locale/es_BO/LC_MESSAGES/default.po

and the code: is in ./gettext.php like this:

<?php
setlocale(LC_ALL, "es_BO.utf8");
bindtextdomain("default", "locale/"); # usually works
textdomain("default");
echo echo _("test");
?>

The C code is exactly the same code but with some headers.

This code will not output my Spanish translation if I do not "enable" Bolivian Spanish system wide with this ugly hack, linking the already existing sv_SE locale to es_BO:

sudo ln -s /usr/lib/locale/sv_SE.utf8 /usr/lib/locale/es_BO.utf8

Updates

I think my question was wrong "So How do I avoid making that ln -s command, or just tell me if I'm going about this in the wrong way?" basically what I want to know if why gettext needs those files in /usr/lib/locale" or how I in a simple way are to use gettext to only translate messages.

Update2 I tried using setlocal(LC_MESSAGES, "es_BO.utf8") but it still needs the messages in /usr/lib/locale.

Upvotes: 1

Views: 1130

Answers (2)

u0b34a0f6ae
u0b34a0f6ae

Reputation: 49833

Use locale -a to get a list of generated locales for your system. To add to this list, edit /etc/locale.gen and rerun locale-gen.

(Also, gettext does not figure anything out at all, it tries to use the current locale, and if it fails, it can only fall back to C)

Upvotes: 1

Bort
Bort

Reputation: 2491

For checking whether a certain translation works properly you should invoke the language environment properly as

#!/bin/bash
export LANG=es_BO
./yourprog

I think your current language hack works because LANG=sv_SE in your current system. Otherwise I recommend reading more about gettext .

Upvotes: 1

Related Questions