Issue
I have made my first steps with NLS. After a few mistakes typical for beginners, it works for German (my native language). Next, I selected it for another language (French). But that did not work. So I installed fr_FR with locale-gen. After that, setlocale() accepted fr_FR.UTF-8. Now my problem: The display is in German!?! Where is my mistake? Thanks for your help Michael
My Code:
#include <stdio.h>
#include <locale.h>
#include <libintl.h>
#define _(String) gettext(String)
#define PACKAGE "nls-test"
#define LOCALEDIR "/usr/share/locale/"
int main(int argc, char *argv[])
{
printf("LOCALE = %s\n",argv[1]);
setlocale(LC_ALL, argv[1]);
printf("check=%s\n",setlocale(LC_ALL, NULL));
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
printf(_("Hello World\n"));
printf(_("My computer has hung up. My laundry never does that\n"));
}
The mo.files are in /usr/locale/de_DE or .../fr_FR under LC_MESSAGES.
The mo-file for french shows:
$:strings /usr/share/locale/fr_FR/LC_MESSAGES/nls-test.mo
Hello World
My computer has hung up. My laundry never does that
Project-Id-Version: PACKAGE VERSION
Report-Msgid-Bugs-To:
PO-Revision-Date: 2022-08-08 16:55+0200
Last-Translator: Michael Adam <[email protected]>
Language-Team: French
Language: fr
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Plural-Forms: nplurals=2; plural=(n > 1);
bonjour le monde
Mon ordinateur a raccroch
. Mon linge ne fait jamais
Solution
I built a simple "hello" program similar to yours on my Linux virtual machine and ran into the same issue with U.S. English being my base language. Even though I had an ".mo" file set up in the proper place, I would still get the English text when executing the code with the qualification of the French language. Checking other Stack Overflow issues, I found where someone had run a trace while executing the program. When I did that, I spotted that the system was still referencing U.S. English.
openat(AT_FDCWD, "/home/craig/C_Programs/Console/Language/en_US/LC_MESSAGES/hello.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/craig/C_Programs/Console/Language/en/LC_MESSAGES/hello.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale-langpack/en_US/LC_MESSAGES/hello.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale-langpack/en/LC_MESSAGES/hello.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0), ...}) = 0
write(1, "Hello World\n", 12Hello World
) = 12
exit_group(0) = ?
+++ exited with 0 +++
When I checked what other environment variables might be tripping me up, I found that I had an environment variable named "LANGUAGE".
@Una:~/C_Programs/Console/Language$ printenv LANGUAGE
en_US
When I unset that environment variable, I could then get either English or French to be outputted to the terminal. Here is how your program reacted as I iterated through the tests.
@Una:~/C_Programs/Console/Language/bin/Release$ ./Language
LOCALE = (null)
check=C
Hello World
My computer has hung up. My laundry never does that
@Una:~/C_Programs/Console/Language/bin/Release$ ./Language fr_FR.utf8
LOCALE = fr_FR.utf8
check=fr_FR.utf8
Hello World
My computer has hung up. My laundry never does that
Then, I unset the language code within my terminal session and retried the command.
@Una:~/C_Programs/Console/Language/bin/Release$ unset LANGUAGE
@Una:~/C_Programs/Console/Language/bin/Release$ ./Language fr_FR.utf8
LOCALE = fr_FR.utf8
check=fr_FR.utf8
Bonjour le monde
Mon ordinateur a raccroché. Ma lessive ne fait jamais ça
You might want to test out a different method of unsetting the environment variable, but give this a try and see if that moves you forward.
Answered By - NoDakker Answer Checked By - David Marino (WPSolving Volunteer)