LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   setlocale() returns NULL (https://www.linuxquestions.org/questions/programming-9/setlocale-returns-null-379838/)

mic 11-04-2005 03:33 AM

setlocale() returns NULL
 
I need to call setlocale to use gettext from my program. The target system where my program will run is busybox based embedded linux. I compiled busybox with enabled locale support.

This is the testing program code:
Code:

#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

#define PACKAGE "hello"

int main(void)
{
        fprintf(stderr, "setlocale: \"%s\"\n", setlocale(LC_CTYPE, ""));
        fprintf(stderr, "setlocale: \"%s\"\n", setlocale(LC_MESSAGES, ""));
        perror("setlocale");
       
        fprintf(stderr, "bindtextdomain: \"%s\"\n", bindtextdomain(PACKAGE, "locale"));
        fprintf(stderr, "textdomain: \"%s\"\n\n", textdomain(PACKAGE));
       
        fprintf(stderr, gettext("Hello, world!\n"));
       
        return 0;
}

I created the mo file and put it in locale/xx/LC_MESSAGES/ in the program's directory.

On a normal desktop distro it works: "Hello world" is displayed in the correct translated language according to the environment variable LC_MESSAGES. On the target system it does not work. setlocale works only for C and POSIX. For others, even for en_US, it returns NULL and perror prints:
No such file or directory

Of course gettext then always returns only the original version of text.
I noticed it does not work also on Mepis linux on my desktop PC, although I have the locale packages installed. So I think this is probably not related to busybox/embedded linux

What I am supposed to do to make it work?

jim mcnamara 11-04-2005 12:06 PM

Each locale is a separate file, you have to have the file installed in
/usr/lib/nls/loc/locales

From the shell
Code:

locale -a
shows them - if you have shell support in your OS.

mic 11-08-2005 03:49 AM

Sorry for late reply... thank you for helping me. "locale -a" showed me what was wrong: I indeed did not have any locales installed but C and POSIX.

All locales take a lot of space and I cannot afford that. It took me a while before I figured out exactly which files I need and which not. It turned out the only file I really need is /usr/lib/locale/locale-archive. Even this one alone is enormous so I regenerated only the needed locales with locale-gen.


All times are GMT -5. The time now is 08:04 PM.