LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2007, 09:41 PM   #1
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
[SOLVED] gettext: Can't change language


Hi,

I am trying to learn to use gettext.
I ran the hello world example (hello-c) that came with the gettext tarball.
However, I have been unable to change it's language so far.

I tried doing:
Code:
export LANG; LANG=de_DE
and checked that it worked by doing echo LANG.

But when I executed the hello program again, I still got the message in English instead of German.

How do I change the language?
I would also like to be able to change the language interactively within the program.

Here's the tarball I used:
ftp://ftp.gnu.org/pub/gnu/gettext/gettext-0.16.tar.gz

hello.c:
Code:
/* Example for use of GNU gettext.
   Copyright (C) 2003 Free Software Foundation, Inc.
   This file is in the public domain.

   Source code of the C program.  */


/* 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 ()
{
  setlocale (LC_ALL, "");
  textdomain ("hello-c");
  bindtextdomain ("hello-c", LOCALEDIR);

  printf ("%s\n", _("Hello, world!"));
  printf (_("This program is running as process number %d."), getpid ());
  putchar ('\n');

  return 0;
}
de.po:
Code:
msgid ""
msgstr ""
"Project-Id-Version: hello-c 0.13.1\n"
"Report-Msgid-Bugs-To: bug-gnu-gettext@gnu.org\n"
"PO-Revision-Date: 2003-12-18 10:09+0100\n"
"Last-Translator: Karl Eichwalder <ke@gnu.franken.de>\n"
"Language-Team: German <de@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: hello.c:31
msgid "Hello, world!"
msgstr "Hallo Welt!"

#: hello.c:32
#, c-format
msgid "This program is running as process number %d."
msgstr "Dieses Programm läuft mit der Prozess-Nummer %d."
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
See also the simple example code further below for GNU/Linux systems.

Last edited by [KIA]aze; 11-18-2007 at 06:46 AM.
 
Old 05-15-2007, 01:51 PM   #2
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Original Poster
Rep: Reputation: 16
Ok, I still haven't solved my problem (have been doing some other things in the meanwhile).

Now I tried to follow this tutorial:
http://oriya.sarovar.org/docs/gettext/node2.html

hello.c:
Quote:
#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
setlocale( LC_ALL, "or_IN" );
//bindtextdomain( "hello", "/usr/share/locale" );
bindtextdomain( "hello", "/home/me/programming_stuff/gettext_test" );
textdomain( "hello" );
printf( gettext( "Hello, world!\n" ) );
exit(0);
}
oriya.po:
Quote:
# Oriya translations for hello example package.
# Copyright (C) 2004 Gora Mohanty
# This file is distributed under the same license as the hello example package.
# Gora Mohanty <gora_mohanty@yahoo.co.in>, 2004.
#
msgid ""
msgstr ""
"Project-Id-Version: oriya\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2004-06-22 02:22+0530\n"
"PO-Revision-Date: 2004-06-22 10:54+0530\n"
"Last-Translator: Gora Mohanty <gora_mohanty@yahoo.co.in>\n"
"Language-Team: Oriya\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: KBabel 1.3\n"

#: hello.c:10
msgid "Hello, world!\n"
msgstr "Bijour!\n"
The commands I executed (while in the "/home/me/programming_stuff/gettext_test" directory):
Quote:
gcc -o hello hello.c
msgfmt -c -v -o hello.mo oriya.po
mkdir -p or_IN/LC_MESSAGES
mv hello.mo or_IN/LC_MESSAGES
And I still get "Hello World!" instead of a translated string!
Why?!
What am I supposed to do?
 
Old 06-09-2007, 04:55 AM   #3
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Original Poster
Rep: Reputation: 16
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

Last edited by [KIA]aze; 07-01-2007 at 05:10 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change my language arash bakhtiari Linux - Newbie 2 09-03-2005 01:15 PM
How to change the language? linx win Debian 8 02-08-2005 01:45 AM
How to change the language ? wlaw Linux - Newbie 1 09-21-2004 08:46 PM
Change language rogk Linux - Newbie 1 04-19-2004 05:04 AM
under kde3 i change change language, where in gnome? sirpelidor Mandriva 1 12-10-2003 04:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration