[gjs] Add a gettext module



commit 7159637b65bcf787e1d43b26c2f81b85f92334cb
Author: Colin Walters <walters verbum org>
Date:   Fri Jul 24 15:26:09 2009 -0400

    Add a gettext module
    
    Bindings for gettext, dgettext, ngettext, dngettext, and pgettext.
    
    We also expose a function "domain" which returns an "instance" of
    the gettext module specialized for a particular domain, to avoid
    having to pass the domain to every call to gettext; useful
    for code which is not in the default domain.

 Makefile-modules.am      |   15 +++-
 examples/gettext.js      |   14 +++
 modules/gettext-native.c |  277 ++++++++++++++++++++++++++++++++++++++++++++++
 modules/gettext-native.h |   39 +++++++
 modules/gettext.js       |   61 ++++++++++
 5 files changed, 405 insertions(+), 1 deletions(-)
---
diff --git a/Makefile-modules.am b/Makefile-modules.am
index ee0bde3..64e0f08 100644
--- a/Makefile-modules.am
+++ b/Makefile-modules.am
@@ -4,12 +4,13 @@ dist_gjstweener_DATA =			\
 	modules/tweener/tweenList.js
 
 dist_gjsjs_DATA +=		\
+	modules/gettext.js	\
 	modules/lang.js		\
 	modules/jsUnit.js	\
 	modules/signals.js	\
 	modules/dbus.js
 
-gjsnative_LTLIBRARIES += console.la debugger.la gi.la mainloop.la dbusNative.la
+gjsnative_LTLIBRARIES += console.la debugger.la gi.la mainloop.la gettextNative.la dbusNative.la
 
 JS_NATIVE_MODULE_CFLAGS =	\
         $(AM_CFLAGS)		\
@@ -46,6 +47,18 @@ mainloop_la_SOURCES =		\
 	modules/mainloop.h	\
 	modules/mainloop.c
 
+gettextNative_la_CFLAGS = 				\
+	$(JS_NATIVE_MODULE_CFLAGS)
+gettextNative_la_LIBADD = \
+	libgjs-gi.la				\
+	$(JS_NATIVE_MODULE_LIBADD)
+gettextNative_la_LDFLAGS = 				\
+	$(JS_NATIVE_MODULE_LDFLAGS)
+
+gettextNative_la_SOURCES =		\
+	modules/gettext-native.h	\
+	modules/gettext-native.c
+
 console_la_CFLAGS = 				\
 	$(JS_NATIVE_MODULE_CFLAGS)
 console_la_LIBADD = \
diff --git a/examples/gettext.js b/examples/gettext.js
new file mode 100644
index 0000000..144fa34
--- /dev/null
+++ b/examples/gettext.js
@@ -0,0 +1,14 @@
+const Gettext = imports.gettext;
+const Gtk = imports.gi.Gtk;
+const Mainloop = imports.mainloop;
+
+Gettext.bindtextdomain("gnome-panel-2.0", "/usr/share/locale");
+Gettext.textdomain("gnome-panel-2.0");
+
+Gtk.init(0, null);
+
+let w = new Gtk.Window({ type: Gtk.WindowType.TOPLEVEL });
+w.add(new Gtk.Label({ label: Gettext.gettext("Panel") }));
+w.show_all();
+
+Mainloop.run("main");
diff --git a/modules/gettext-native.c b/modules/gettext-native.c
new file mode 100644
index 0000000..e563b53
--- /dev/null
+++ b/modules/gettext-native.c
@@ -0,0 +1,277 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2009  Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "gettext-native.h"
+#include "../gi/closure.h"
+#include <util/log.h>
+#include <gjs/gjs.h>
+#include <jsapi.h>
+#include <glib/gi18n.h>
+
+static JSBool
+gjs_textdomain(JSContext *context,
+               JSObject  *obj,
+               uintN      argc,
+               jsval     *argv,
+               jsval     *retval)
+{
+    char *domain;
+
+    if (!gjs_parse_args(context, "textdomain", "s", argc, argv,
+                        "domain", &domain))
+        return JS_FALSE;
+    
+    textdomain(domain);
+    g_free(domain);
+
+    return JS_TRUE;
+}
+
+static JSBool
+gjs_bindtextdomain(JSContext *context,
+                   JSObject  *obj,
+                   uintN      argc,
+                   jsval     *argv,
+                   jsval     *retval)
+{
+    char *domain;
+    char *location;
+    
+    if (!gjs_parse_args (context, "bindtextdomain", "sF", argc, argv,
+                         "domain", &domain,
+                         "location", &location))
+        return JS_FALSE;
+
+    bindtextdomain(domain, location);
+    /* Always use UTF-8; we assume it internally here */
+    bind_textdomain_codeset(domain, "UTF-8");
+    g_free (domain);
+    g_free (location);
+    return JS_TRUE;
+}
+
+static JSBool
+gjs_gettext(JSContext *context,
+            JSObject  *obj,
+            uintN      argc,
+            jsval     *argv,
+            jsval     *retval)
+{
+    char *msgid;
+    const char *translated;
+    JSBool result;
+    
+    if (!gjs_parse_args (context, "gettext", "s", argc, argv,
+                         "msgid", &msgid))
+      return JS_FALSE;
+
+    translated = gettext(msgid);
+    result = gjs_string_from_utf8(context, translated, -1, retval);
+    g_free (msgid);
+    return result;
+}
+
+static JSBool
+gjs_dgettext(JSContext *context,
+             JSObject  *obj,
+             uintN      argc,
+             jsval     *argv,
+             jsval     *retval)
+{
+    char *domain;
+    char *msgid;
+    const char *translated;
+    JSBool result;
+    
+    if (!gjs_parse_args (context, "dgettext", "zs", argc, argv,
+                         "domain", &domain, "msgid", &msgid))
+      return JS_FALSE;
+
+    translated = dgettext(domain, msgid);
+    g_free (domain);
+
+    result = gjs_string_from_utf8(context, translated, -1, retval);
+    g_free (msgid);
+    return result;
+}
+
+static JSBool
+gjs_ngettext(JSContext *context,
+             JSObject  *obj,
+             uintN      argc,
+             jsval     *argv,
+             jsval     *retval)
+{
+    char *msgid1;
+    char *msgid2;
+    guint32 n;
+    const char *translated;
+    JSBool result;
+    
+    if (!gjs_parse_args (context, "ngettext", "ssu", argc, argv,
+                         "msgid1", &msgid1, "msgid2", &msgid2, "n", &n))
+      return JS_FALSE;
+ 
+    translated = ngettext(msgid1, msgid2, n);
+    
+    result = gjs_string_from_utf8(context, translated, -1, retval);
+    g_free (msgid1);
+    g_free (msgid2);
+    return result;
+}
+
+static JSBool
+gjs_dngettext(JSContext *context,
+              JSObject  *obj,
+              uintN      argc,
+              jsval     *argv,
+              jsval     *retval)
+{
+    char *domain;
+    char *msgid1;
+    char *msgid2;
+    guint n;
+    const char *translated;
+    JSBool result;
+    
+    if (!gjs_parse_args (context, "dngettext", "zssu", argc, argv,
+                         "domain", &domain, "msgid1", &msgid1,
+                         "msgid2", &msgid2, "n", &n))
+      return JS_FALSE;
+
+    translated = dngettext(domain, msgid1, msgid2, n);
+    g_free (domain);
+
+    result = gjs_string_from_utf8(context, translated, -1, retval);
+    g_free (msgid1);
+    g_free (msgid2);
+    return result;
+}
+
+static JSBool
+gjs_pgettext(JSContext *context,
+             JSObject  *obj,
+             uintN      argc,
+             jsval     *argv,
+             jsval     *retval)
+{
+    char *src_context;
+    char *msgid;
+    const char *translated;
+    JSBool result;
+    
+    if (!gjs_parse_args (context, "pgettext", "ss", argc, argv,
+                         "context", &src_context, "msgid", &msgid))
+      return JS_FALSE;
+
+    translated = g_dpgettext2(NULL, src_context, msgid);
+    g_free (src_context);
+ 
+    result = gjs_string_from_utf8(context, translated, -1, retval);
+    g_free (msgid);
+    return result;
+}
+
+static JSBool
+gjs_dpgettext(JSContext *context,
+              JSObject  *obj,
+              uintN      argc,
+              jsval     *argv,
+              jsval     *retval)
+{
+    char *domain;
+    char *src_context;
+    char *msgid;
+    const char *translated;
+    JSBool result;
+  
+    if (!gjs_parse_args (context, "dpgettext", "sss", argc, argv,
+                         "domain", &domain, "context", &src_context,
+                         "msgid", &msgid))
+        return JS_FALSE;
+
+    translated = g_dpgettext2(domain, src_context, msgid);
+    g_free (domain);
+    g_free (src_context);
+
+    result = gjs_string_from_utf8(context, translated, -1, retval);
+    g_free (msgid);
+    return result;
+}
+
+JSBool
+gjs_define_gettext_stuff(JSContext      *context,
+                         JSObject      *module_obj)
+{
+    if (!JS_DefineFunction(context, module_obj,
+                           "textdomain",
+                           gjs_textdomain,
+                           1, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    if (!JS_DefineFunction(context, module_obj,
+                           "bindtextdomain",
+                           gjs_bindtextdomain,
+                           2, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+  
+    if (!JS_DefineFunction(context, module_obj,
+                           "gettext",
+                           gjs_gettext,
+                           1, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    if (!JS_DefineFunction(context, module_obj,
+                           "dgettext",
+                           gjs_dgettext,
+                           2, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    if (!JS_DefineFunction(context, module_obj,
+                           "ngettext",
+                           gjs_ngettext,
+                           3, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    if (!JS_DefineFunction(context, module_obj,
+                           "dngettext",
+                           gjs_dngettext,
+                           4, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    if (!JS_DefineFunction(context, module_obj,
+                           "pgettext",
+                           gjs_pgettext,
+                           2, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    if (!JS_DefineFunction(context, module_obj,
+                           "dpgettext",
+                           gjs_dpgettext,
+                           3, GJS_MODULE_PROP_FLAGS))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
+GJS_REGISTER_NATIVE_MODULE("gettextNative", gjs_define_gettext_stuff)
diff --git a/modules/gettext-native.h b/modules/gettext-native.h
new file mode 100644
index 0000000..bb58346
--- /dev/null
+++ b/modules/gettext-native.h
@@ -0,0 +1,39 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2009  Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef __GJS_GETTEXT_H__
+#define __GJS_GETTEXT_H__
+
+#include <config.h>
+#include <glib.h>
+
+#include <jsapi.h>
+
+G_BEGIN_DECLS
+
+JSBool        gjs_define_gettext_stuff     (JSContext      *context,
+                                            JSObject       *in_object);
+
+G_END_DECLS
+
+#endif  /* __GJS_MAINLOOP_H__ */
diff --git a/modules/gettext.js b/modules/gettext.js
new file mode 100644
index 0000000..aecb512
--- /dev/null
+++ b/modules/gettext.js
@@ -0,0 +1,61 @@
+// Copyright 2009 Red Hat, Inc. All Rights Reserved.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+
+/**
+ * This module provides bindings for the "gettext" family of functions.
+ * Usage:
+ * 
+ * const Gettext = imports.gettext;
+ * 
+ * Gettext.textdomain("myapp");
+ * Gettext.bindtextdomain("myapp", "/usr/share/locale");
+ * 
+ * let translated = Gettext.gettext("Hello world!");
+ */
+
+const Lang = imports.lang;
+
+// Merge stuff defined in native code
+Lang.copyProperties(imports.gettextNative, this);
+
+/**
+ * Create an object with bindings for gettext, ngettext,
+ * and pgettext bound to a particular translation domain.
+ * 
+ * @param domainName Translation domain string
+ * @returns: an object with gettext bindings
+ * @type: function
+ */
+var domain = function(domainName) {
+    return {
+        gettext: function(msgid) {
+            return dgettext(domain, msgid);
+        },
+    
+        ngettext: function(msgid1, msgid2, n) {
+            return dngettext(domain, msgid1, msgid2, n);
+        },
+    
+        pgettext: function(context, msgid) {
+            return dpgettext(domain, context, msgid);
+        }
+    }
+};
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]