Gettext’s bindtextdomain() ignoring directory

If you use strace on a program using the gettext internationalization library and you see something like the below.

open("/usr/lib/locale/ru/LC_MESSAGES", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)

This means gettext is looking in the default locale directory which is /usr/lib/locale on my ubuntu machine. This was a bug for me because I had used bindtextdomain() to change the directory for the locale’s .mo files.

The cause for gettext ignoring my custom locale directory was that I had set my LANG enviromental variable to just the language code. For example LANG=ja

Instead LANG should be a full xx_CC format, for example:

LANG=ja_JP

You can check what your terminal’s locale environmental variables are set to use `locale`. An example of this is below.

danieru@danieru-x1:~$ locale
LANG=ja_JP.UTF-8
LANGUAGE=ja:en_CA:en
LC_CTYPE="ja_JP.UTF-8"
LC_NUMERIC=en_CA.UTF-8
LC_TIME=en_CA.UTF-8
LC_COLLATE="ja_JP.UTF-8"
LC_MONETARY=en_CA.UTF-8
LC_MESSAGES="ja_JP.UTF-8"
LC_PAPER=en_CA.UTF-8
LC_NAME=en_CA.UTF-8
LC_ADDRESS=en_CA.UTF-8
LC_TELEPHONE=en_CA.UTF-8
LC_MEASUREMENT=en_CA.UTF-8
LC_IDENTIFICATION=en_CA.UTF-8
LC_ALL=

Above was a correct and valid set of locale variables. What caused gettext to not find my translation’s .mo files was the following broken set.

danieru@danieru-x1:~$ locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=ja
LANGUAGE=ja:en_CA:en
LC_CTYPE="ja"
LC_NUMERIC=en_CA.UTF-8
LC_TIME=en_CA.UTF-8
LC_COLLATE="ja"
LC_MONETARY=en_CA.UTF-8
LC_MESSAGES="ja"
LC_PAPER=en_CA.UTF-8
LC_NAME=en_CA.UTF-8
LC_ADDRESS=en_CA.UTF-8
LC_TELEPHONE=en_CA.UTF-8
LC_MEASUREMENT=en_CA.UTF-8
LC_IDENTIFICATION=en_CA.UTF-8
LC_ALL=

Leave a Reply

Your email address will not be published. Required fields are marked *