[Commits] click2trans branch master updated.

C. Scott Ananian cscott at laptop.org
Wed Nov 19 12:52:54 EST 2008


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "/home/cscott/public_git/click2trans".

The branch, master has been updated
       via  87bdf8d84f6efef5d947632a1605b561b4df337e (commit)
      from  1439feca573f673586427e158a3f59be016d62ba (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

 ExtraLang.deps   |    1 +
 ExtraLang.vapi   |    4 ++++
 Makefile         |    6 +++---
 TransWidget.vala |   27 +++++++++++++++++++++++++--
 extralang.c      |   14 ++++++++++++++
 extralang.h      |    1 +
 gtktrans.c       |    3 ++-
 7 files changed, 50 insertions(+), 6 deletions(-)
 create mode 100644 ExtraLang.deps
 create mode 100644 ExtraLang.vapi
 create mode 100644 extralang.c
 create mode 100644 extralang.h

- Log -----------------------------------------------------------------
commit 87bdf8d84f6efef5d947632a1605b561b4df337e
Author: C. Scott Ananian <cscott at laptop.org>
Date:   Wed Nov 19 12:26:48 2008 -0500

    Support on-the-fly language switching.

diff --git a/ExtraLang.deps b/ExtraLang.deps
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/ExtraLang.deps
@@ -0,0 +1 @@
+
diff --git a/ExtraLang.vapi b/ExtraLang.vapi
new file mode 100644
index 0000000..8d45069
--- /dev/null
+++ b/ExtraLang.vapi
@@ -0,0 +1,4 @@
+namespace ExtraLang {
+	[CCode (cname="switch_lang", cheader_filename="extralang.h")]
+	public static void switch_lang(string newlang);
+}
diff --git a/Makefile b/Makefile
index 8b237b2..3293f6b 100644
--- a/Makefile
+++ b/Makefile
@@ -11,15 +11,15 @@ all: gtktrans.so
 
 gtktrans.o: TransWidget.h GetTextInfo.h
 
-gtktrans.so: gtktrans.o TransWidget.o GetTextInfo.o
+gtktrans.so: gtktrans.o TransWidget.o GetTextInfo.o extralang.o
 	$(CC) -shared -o $@ $^ $(shell pkg-config --libs $(PKGS)) -ldl
 
 # vala app standalone, for testing.
 transwidget: TransWidget.vala GetTextInfo.vala
 	$(VALAC) $(VALA_FLAGS) -X -I. -X -DGETTEXT_PACKAGE=\"gtktrans\" -o $@ $^
 # vala app(s) as library.
-TransWidget.c TransWidget.h TransWidget.vapi TransWidget.gidl: TransWidget.vala GetTextInfo.vapi
-	$(VALAC) $(VALA_FLAGS) --pkg GetTextInfo --vapidir=. -C --library TransWidget --basedir ./ $<
+TransWidget.c TransWidget.h TransWidget.vapi TransWidget.gidl: TransWidget.vala GetTextInfo.vapi ExtraLang.vapi
+	$(VALAC) $(VALA_FLAGS) --pkg GetTextInfo --pkg ExtraLang --vapidir=. -C --library TransWidget --basedir ./ $<
 TransWidget.o: TransWidget.h
 
 GetTextInfo.c GetTextInfo.h GetTextInfo.vapi GetTextInfo.gidl: GetTextInfo.vala
diff --git a/TransWidget.vala b/TransWidget.vala
index 8dc1e18..815e1f3 100644
--- a/TransWidget.vala
+++ b/TransWidget.vala
@@ -5,6 +5,7 @@ using Gtk;
 using DBus;
 using GetTextInfo;
 using Gee;
+using ExtraLang;
 
 public class TransWidget : Gtk.Dialog {
 	// XXX USE REAL FILENAME
@@ -62,6 +63,22 @@ public class TransWidget : Gtk.Dialog {
 		}
 	}
 
+	private static string _current_lang;
+	private static bool _switching=false;
+	private static void switch_lang(Gtk.MenuItem _ignore1, void * _ignore2) {
+		_switching=true;
+		string newlang = (_current_lang == "en_US.UTF-8") ? "es_GT.UTF-8" : "en_US.UTF-8";
+		ExtraLang.switch_lang(newlang);
+		_current_lang = newlang;
+		foreach (Gtk.Label l in gtimap.get_keys()) {
+			GetTextInfo gti = gtimap.get(l);
+			gti.result = _(gti.msgid1);
+			l.set_label(gti.result);
+		}
+		_switching=false;
+	}
+
+
 	private static void translate_callback(Gtk.MenuItem m, Gtk.Label l) {
 		stdout.printf("Translating %s\n", l.get_label());
 		var t = new TransWidget(l);
@@ -121,10 +138,15 @@ public class TransWidget : Gtk.Dialog {
 		menuitem.show();
 		m.prepend(menuitem);
 
-		menuitem = new Gtk.MenuItem.with_label(_("Translate..."));
+		menuitem = new Gtk.MenuItem.with_label(_("Switch..."));
 		menuitem.show();
 		m.prepend(menuitem);
+		GLib.Signal.connect(menuitem, "activate",
+							(GLib.Callback) switch_lang, null);
 
+		menuitem = new Gtk.MenuItem.with_label(_("Translate..."));
+		menuitem.show();
+		m.prepend(menuitem);
 		GLib.Signal.connect(menuitem, "activate",
 							(GLib.Callback) translate_callback, l);
 	}
@@ -132,6 +154,7 @@ public class TransWidget : Gtk.Dialog {
 	private static GLib.KeyFile lookaside;
 	private static Gee.HashMap<Gtk.Label,GetTextInfo> gtimap;
 	public static void init() {
+		_current_lang = GLib.Intl.setlocale(LocaleCategory.MESSAGES, null);
 		lookaside = new GLib.KeyFile();
 		try {
 			lookaside.load_from_file(LOOKASIDE_FILENAME,
@@ -146,7 +169,7 @@ public class TransWidget : Gtk.Dialog {
 	}
 
 	public static void label_change(Gtk.Label l, ParamSpec ps) {
-		if (l.get_label() != null) {
+		if (l.get_label() != null && !_switching) {
 			label_update(l);
 		}
 	}
diff --git a/extralang.c b/extralang.c
new file mode 100644
index 0000000..1112b7a
--- /dev/null
+++ b/extralang.c
@@ -0,0 +1,14 @@
+#include <stdlib.h>
+#include <libintl.h>
+#include <locale.h>
+#include "extralang.h"
+
+void switch_lang(const char *newlang) {
+    extern int  _nl_msg_cat_cntr;
+    setenv("LC_MESSAGES", newlang, 1);
+    setenv("LANGUAGE", newlang, 1);
+    setenv("LANG", newlang, 1);
+    setlocale(LC_MESSAGES, newlang);
+    ++_nl_msg_cat_cntr;
+    printf("SWITCHING %s\n %s\n", newlang, gettext("Brighter"));
+}
diff --git a/extralang.h b/extralang.h
new file mode 100644
index 0000000..6ad87f9
--- /dev/null
+++ b/extralang.h
@@ -0,0 +1 @@
+extern void switch_lang(const char *newlang);
diff --git a/gtktrans.c b/gtktrans.c
index 54c29ab..cba8465 100644
--- a/gtktrans.c
+++ b/gtktrans.c
@@ -187,11 +187,12 @@ char *gettext(const char *msgid) {
 char *dgettext(const char *domain_name, const char *msgid) {
     static char * (*func)(const *, const char *) = NULL;
     char *result;
+    if (strcmp(domain_name, "gtktrans")==0) domain_name=NULL;
     if (!func)
 	func = (char *(*)(const char *, const char *))
 	    dlsym_ORIG(RTLD_NEXT, "dgettext");
     result = func(domain_name, msgid);
-    printf("DGETTEXT: %s %s -> %s\n", domain_name, msgid, result);
+    //printf("DGETTEXT: %s %s -> %s\n", domain_name, msgid, result);
     if (_inited && msgid && result)
 	result = trans_widget_record_gettext(domain_name, msgid, result);
     return result;
-----------------------------------------------------------------------


--
/home/cscott/public_git/click2trans


More information about the Commits mailing list