boom
boom

Reputation: 6166

localising a text using gettext

I have a text in English which i want to convert it into French.

The sequence that i followed are as under:

  1. generate all the text using xgettext command in terminal, creating .pot file
  2. creating .po file from .pot file
  3. creating .mo file from .po file

I have copy the .mo file in "/usr/share/locale/fr/LC_MESSAGES"

here is my code for main.c file:

int main()

{

    setlocale(LC_ALL,"");
    bindtextdomain("main","/usr/share/locale");
    textdomain("main");
    printf( gettext("Hello world\n"));
    return (0);
}

When i execute the program the French version of the text is not printed in terminal. What can be the reason for this issue?

Here is my fr.po file

# French translations for GNU main package.
# Copyright (C) 2010 THE GNU main'S COPYRIGHT HOLDER
# This file is distributed under the same license as the GNU main package.
#
msgid ""
msgstr ""
"Project-Id-Version: GNU main 0.1\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2010-12-29 10:14+0530\n"
"PO-Revision-Date: 2010-12-29 12:21+0530\n"
"Last-Translator: Lenin\n"
"Language-Team: French\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=ASCII\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: main.c:29
#, c-format
msgid "Hello world\n"
msgstr "Bonjour tout le monde\n"

Here is my call to msgfmt from current directory:

msgfmt -c -v -o main.mo fr.po

Upvotes: 0

Views: 430

Answers (1)

gpoo
gpoo

Reputation: 8638

You do not need to touch the system files/directories. And you should not touch them if you are developing.

I tried the following code:

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

int main(int argc, char **argv)
{
    setlocale(LC_ALL, "");
    bindtextdomain("main", ".");
    textdomain("main");
    printf(gettext("Hello world\n"));
    return (0);
}

Which is quite similar to your code, except I am testing the localization in my development directory (current one).

Create the template for translations

$ xgettext -o main.pot main.c

Create the files to translate using the template:

$ msginit --no-translator -l es -o es.po -i main.pot
Created es.po.
$ msginit --no-translator -l fr -o fr.po -i main.pot 
Created fr.po.

Edit and translate the files. Set Project-Id-Version to main and translate the lines. In this case, the line 23 according to my source code.

Create the target directories for translations:

$ mkdir -p es/LC_MESSAGES
$ mkdir -p fr/LC_MESSAGES

Compile and install the translations:

$ msgfmt -o es/LC_MESSAGES/main.mo es.po 
$ msgfmt -o fr/LC_MESSAGES/main.mo fr.po 

Compile the program:

$ make main
cc     main.c   -o main

Run the program:

$ LANGUAGE=C ./main 
Hello world
$ LANGUAGE=es ./main
Hola mundo
$ LANGUAGE=fr ./main
Bonjour tout le monde

That is.

You might want to compile the program before and fix errors, before translating it.

Upvotes: 4

Related Questions