(Solved) Gettext and C

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

(Solved) Gettext and C

#1 Post by RSH »

Hi.

I have a little knowledge of C and almost each little tool I've created using C, was based on a C example I've got from 01micko.

By now I need always to build two versions, one for DE and one for EN.

So I need to learn how to use gettext in C.

Can anybody show me a quick example of how to use gettext in C, or at least point me to some C source codes wherein gettext is used, so that I can examine the code?

Thanks,

RSH
Last edited by RSH on Fri 15 Aug 2014, 01:49, edited 1 time in total.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#2 Post by 01micko »

Hi RSH

Take a look at network_tray and some of the others that Barry originally wrote. rodin.s gettext'd them, I think with help of L18L. Don't pay too much attention to the actual C code, it needs work, but you can see the _(bla) format where the underscore flags the string to be translated. There is also relevant stuff in the 'compile' script(s).

http://distro.ibiblio.org/puppylinux/sources/ .. which is quite incomplete but there is network_tray under /n/ and a few others.
Puppy Linux Blog - contact me for access

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#3 Post by RSH »

Hi 01micko.

Thanks for the reply.

I have examined network_tray and included the necessary parts into my C code file. Gettext is installed and works - checked this by running Screeny, which appears in DE.

Though my C Application won't show in DE:

Code: Select all

//#############################################################################
// System Tray Refresh Desktop Function
// 2014-08-14 RSH for LazY Puppy
//#############################################################################

// Include Files
#include <string.h>
#include <libintl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <glib/gstdio.h>
#include <dirent.h>
#include <errno.h>
#define _(STRING)    gettext(STRING)

// Icons
GdkPixbuf *a_pixbuf;
GtkStatusIcon *tray_icon;

// Error Check on File Open
GError *gerror = NULL;

//#############################################################################
// Click Functions called from Menu
//#############################################################################

// Run /usr/local/bin/refresh_desktop
void  view_popup_menu_run_refreshdesktop (GtkWidget *menuitem, gpointer user_data)
{
    system("/usr/local/bin/refresh_desktop &");
}

//#############################################################################
// System Tray Icon
//#############################################################################

// Create Tray Icon
static GtkStatusIcon *create_tray_icon()
{
	tray_icon = gtk_status_icon_new();
	g_signal_connect(G_OBJECT(tray_icon), "activate", G_CALLBACK(view_popup_menu_run_refreshdesktop), NULL);
    	g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(view_popup_menu_run_refreshdesktop), NULL);
	a_pixbuf=gdk_pixbuf_new_from_file("/usr/share/pixmaps/restore.png",&gerror);
	gtk_status_icon_set_from_pixbuf(tray_icon,a_pixbuf);
	gtk_status_icon_set_tooltip(tray_icon, _("Refresh the Desktop"));
	gtk_status_icon_set_visible(tray_icon, TRUE);
	return tray_icon;
}

//#############################################################################
// Main Program Functions
//#############################################################################

// Main Program
int main(int argc, char **argv)
{
	gtk_init(&argc, &argv);

	setlocale( LC_ALL, "" );
	bindtextdomain( "refresh_my_desktop", "/usr/share/locale" );
	textdomain( "refresh_my_desktop" );

	tray_icon = create_tray_icon();
	gtk_main();
	return 0;
}

//#############################################################################
// End of System Tray Refresh Desktop Function
//#############################################################################
Here is how I did compile:

Code: Select all

lxterminal -e gcc -o refresh_my_desktop refresh_my_desktop.c `pkg-config --libs --cflags gtk+-2.0`
xgettext --keyword="_" refresh_my_desktop.c -o refresh_my_desktop.pot
Content of the created .pot (.mo) file:

Code: Select all

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-08-15 02:39+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: refresh_my_desktop.c:55
msgid "Refresh the Desktop"
msgstr "Arbeitsfläche aktualisieren"
Note: msgstr "Arbeitsfläche aktualisieren" was edited after .pot creation, then made a .mo file from this and copied into /usr/share/locale/de/LC_MESSAGES.

Though the tool-tip still shows EN message, Refresh the Desktop.

What am I doing wrong here??? :cry:
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#4 Post by 01micko »

RSH wrote:What am I doing wrong here??? :cry:
I dunno, but it works fine here.
Make sure
  • you have de.po created from the .pot
    that UTF-8 is set for charset=
    compile with msgfmt de.po -o refresh_my_desktop.mo
(I just changed icon for my convenience)
Attachments
rsh_app.png
(22.79 KiB) Downloaded 455 times
Puppy Linux Blog - contact me for access

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#5 Post by RSH »

Make sure

you have de.po created from the .pot
that UTF-8 is set for charset=
compile with msgfmt de.po -o refresh_my_desktop.mo
UTF-8 was the missing point.

Works now over here as well. :D

So I'm prepared now...

Thank you very much! 8)
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#6 Post by zigbert »

I have tried to follow your guidelines, but I still fail to translate the pMusic trayapp correct
- built pot file with the command xgettext --keyword="_" pmusic_trayapp.c -o pmusic_trayapp.pot
- Translated the pot file to nb_NO.po
- Set charset=UTF-8 in po file
- Compiled with msgfmt nb_NO.po -o pmusic_trayapp.mo
- Placed the pmusic_trayapp.mo in /usr/share/locale/nb_NO/LC_MESSAGES/pmusic_trayapp.mo
- set LANG, and run trayapp

The attached screenshot shows the details.


Thank you for any help
Sigmund
Attachments
pmusic_trayapp.gz
compiled in Slacko 5.7
(3.52 KiB) Downloaded 177 times
pmusic_trayapp.pot.gz
(543 Bytes) Downloaded 151 times
forum.png
Screenshot
(76.26 KiB) Downloaded 235 times

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#7 Post by SFR »

Hey Sigmund

Try LANGUAGE=... instead of LANG=....
I've learned it here: http://www.murga-linux.com/puppy/viewtopic.php?t=92929

Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#8 Post by zigbert »

Yepp! :D

Thank you
Sigmund

Post Reply