Ok, I recently solved my first problem and managed to make gettext work using this code:
Code:
/* Get setlocale() declaration. */
#include <locale.h>
/* Get printf() declaration. */
#include <stdio.h>
/* Get getpid() declaration. */
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
/* Get gettext(), textdomain(), bindtextdomain() declaration. */
#include "gettext.h"
/* Define shortcut for gettext(). */
#define _(string) gettext (string)
int
main (int argc, char *argv[])
{
int ret;
textdomain ("hello-c");
bindtextdomain ("hello-c", LOCALEDIR);
bind_textdomain_codeset ("hello-c", "UTF-8");
printf("LOCALEDIR=%s\n",LOCALEDIR);
// setlocale (LC_ALL, "or_IN");
setlocale (LC_ALL, "");
/* Change language. */
ret=setenv ("LANGUAGE",argv[1], 1);printf("ret=%d\n",ret);
/* Make change known. */
extern int _nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
printf ("%s\n", _("Hello, world!"));
printf (_("This program is running as process number %d."), getpid ());
putchar ('\n');
ret=setenv ("LANGUAGE","fr", 1);printf("ret=%d\n",ret);
extern int _nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
printf ("%s\n", _("Hello, world!"));
printf (_("This program is running as process number %d."), getpid ());
putchar ('\n');
ret=setenv ("LANGUAGE","de", 1);printf("ret=%d\n",ret);
extern int _nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
printf ("%s\n", _("Hello, world!"));
printf (_("This program is running as process number %d."), getpid ());
putchar ('\n');
ret=setenv ("LANGUAGE","ru", 1);printf("ret=%d\n",ret);
extern int _nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
printf ("%s\n", _("Hello, world!"));
printf (_("This program is running as process number %d."), getpid ());
putchar ('\n');
ret=setenv ("LANGUAGE","or_IN", 1);printf("ret=%d\n",ret);
extern int _nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
printf ("%s\n", _("Hello, world!"));
printf (_("This program is running as process number %d."), getpid ());
putchar ('\n');
return 0;
}
The "gettext.h" is necessary and can be found in the GNU gettext source package.
Here's also the Makefile:
Code:
localedir = /usr/share/locale
#localedir = ../share/locale
DEFS = -DLOCALEDIR=\"$(localedir)\" -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"hello-c\" -DVERSION=\"0\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_UNISTD_H=1 -DENABLE_NLS=1 -DHAVE_GETTEXT=1 -DHAVE_DCGETTEXT=1
all:
gcc $(DEFS) -I. -I. -I. -g -O2 -c hello_international.c
gcc -g -O2 -o ./bin/hello_international hello_international.o
All the variables in DEFS seem to be used by gettext.h. I haven't looked into in detail, but it works.
"localedir" is of course where the .mo files should be placed.
ex: localedir/fr/LC_MESSAGES/hello-c.mo
(the basename of the .mo file must be the one used in the textdomain function)
Note: My program uses the same strings as the gettext source examples, so the same .po/.mo files can be used for it.
The language for the first printed string can be specified as an argument on the command line. (ex: "hello_international de")
I also managed to implement it in a GTK+ app, which wasn't that hard.
But now I have another problem: getting it to work in Windows.
If anybody can help me with that...
I managed to make it work with PHP (using easyPHP), but my goal is to create a C/C++ program with it.
Once I get everything working as I want, I think I'll make a complete tutorial of it (including use in a windows portable GTK app).
I haven't been able to find one which worked perfectly for me.
EDIT:
Problem solved.
I didn't have time to create a tutorial yet, but in the meanwhile you can check out this program here :
http://sourceforge.net/projects/alliancemanager