[libgweather] Add dynamic localization support



commit b2c6bc01e5600e115cbd70f1765cd8d6d268819b
Author: Evgeny Bobkin <evgen ibqn gmail com>
Date:   Sun Jul 28 16:23:24 2013 +0200

    Add dynamic localization support
    
    https://bugzilla.gnome.org/show_bug.cgi?id=704021

 libgweather/gweather-location.c |   48 ++++++++++++++++++++++++++++++---------
 libgweather/parser.c            |   21 +++++++++++++++-
 libgweather/parser.h            |    1 +
 libgweather/weather-priv.h      |    2 +-
 4 files changed, 58 insertions(+), 14 deletions(-)
---
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index 89010cb..6809250 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <math.h>
 #include <locale.h>
+#include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 #include <libxml/xmlreader.h>
 
@@ -138,6 +139,7 @@ location_new_from_xml (GWeatherParser *parser, GWeatherLocationLevel level,
     loc->parent = parent;
     loc->level = level;
     loc->ref_count = 1;
+    loc->msgctxt = NULL;
     if (level == GWEATHER_LOCATION_WORLD)
        loc->metar_code_cache = g_hash_table_ref (parser->metar_code_cache);
     children = g_ptr_array_new ();
@@ -154,14 +156,14 @@ location_new_from_xml (GWeatherParser *parser, GWeatherLocationLevel level,
 
        tagname = (const char *) xmlTextReaderConstName (parser->xml);
        if (!strcmp (tagname, "name") && !loc->name) {
-           value = _gweather_parser_get_localized_value (parser);
+            loc->msgctxt = _gweather_parser_get_msgctxt_value (parser);
+           value = _gweather_parser_get_value (parser);
            if (!value)
                goto error_out;
            loc->name = value;
            normalized = g_utf8_normalize (loc->name, -1, G_NORMALIZE_ALL);
            loc->sort_name = g_utf8_casefold (normalized, -1);
            g_free (normalized);
-
        } else if (!strcmp (tagname, "iso-code") && !loc->country_code) {
            value = _gweather_parser_get_value (parser);
            if (!value)
@@ -356,6 +358,7 @@ gweather_location_unref (GWeatherLocation *loc)
     g_return_if_fail (loc->level != GWEATHER_LOCATION_WORLD);
 
     g_free (loc->name);
+    g_free (loc->msgctxt);
     g_free (loc->sort_name);
     g_free (loc->country_code);
     g_free (loc->tz_hint);
@@ -415,7 +418,17 @@ const char *
 gweather_location_get_name (GWeatherLocation *loc)
 {
     g_return_val_if_fail (loc != NULL, NULL);
-    return loc->name;
+
+    const char *ret;
+
+    if (loc->msgctxt) {
+       ret = (const char*) g_dpgettext2 ("libgweather-locations",
+                                      (char*) loc->msgctxt, (char*) loc->name);
+    } else {
+       ret = (const char*) g_dgettext ("libgweather-locations", (char*) loc->name);
+    }
+
+    return g_strdup (ret);
 }
 
 /**
@@ -708,15 +721,28 @@ gweather_location_get_city_name (GWeatherLocation *loc)
 {
     g_return_val_if_fail (loc != NULL, NULL);
 
+    const char *ret;
+
     if (loc->level == GWEATHER_LOCATION_CITY ||
-       loc->level == GWEATHER_LOCATION_DETACHED)
-       return g_strdup (loc->name);
-    else if (loc->level == GWEATHER_LOCATION_WEATHER_STATION &&
-            loc->parent &&
-            loc->parent->level == GWEATHER_LOCATION_CITY)
-       return g_strdup (loc->parent->name);
-    else
-       return NULL;
+        loc->level == GWEATHER_LOCATION_DETACHED) {
+        if (loc->msgctxt) {
+            ret = (const char*) g_dpgettext2 ("libgweather-locations", (char*) loc->msgctxt, (char*) 
loc->name);
+        } else {
+            ret = (const char*) g_dgettext ("libgweather-locations", (char*) loc->name);
+        }
+
+        return g_strdup (ret);
+    } else if (loc->level == GWEATHER_LOCATION_WEATHER_STATION &&
+               loc->parent &&
+               loc->parent->level == GWEATHER_LOCATION_CITY) {
+        if (loc->parent->msgctxt) {
+            ret = (const char*) g_dpgettext2 ("libgweather-locations", (char*) loc->parent->msgctxt, (char*) 
loc->parent->name);
+        } else {
+            ret = (const char*) g_dgettext ("libgweather-locations", (char*) loc->parent->name);
+        }
+        return g_strdup (ret);
+    } else
+        return NULL;
 }
 
 void
diff --git a/libgweather/parser.c b/libgweather/parser.c
index 4ff01ef..4397293 100644
--- a/libgweather/parser.c
+++ b/libgweather/parser.c
@@ -26,10 +26,10 @@
 #include "weather-priv.h"
 
 #include "parser.h"
-
 #include <string.h>
 #include <glib.h>
 #include <libxml/xmlreader.h>
+#include <glib/gi18n-lib.h>
 
 /*
  * _gweather_parser_get_value:
@@ -90,13 +90,30 @@ _gweather_parser_get_localized_value (GWeatherParser *parser)
     char *untranslated_value = _gweather_parser_get_value (parser);
     char *ret;
 
-    ret = (char*) dgettext ("libgweather-locations", (char*) untranslated_value);
+    ret = (char*) g_dgettext ("libgweather-locations", (char*) untranslated_value);
 
     ret = g_strdup (ret);
     xmlFree (untranslated_value);
     return ret;
 }
 
+char *
+_gweather_parser_get_msgctxt_value (GWeatherParser *parser)
+{
+    const char *value;
+    const char *name;
+
+    while(xmlTextReaderMoveToNextAttribute(parser->xml)) {
+       name = (const char *)xmlTextReaderConstName(parser->xml);
+       if (!strcmp (name, "msgctxt")) {
+           value = (const char *)xmlTextReaderConstValue(parser->xml);
+           return g_strdup (value);
+       }
+    }
+
+    return NULL;
+}
+
 static void
 gweather_location_list_free (gpointer list)
 {
diff --git a/libgweather/parser.h b/libgweather/parser.h
index 02a0922..5af49a5 100644
--- a/libgweather/parser.h
+++ b/libgweather/parser.h
@@ -34,6 +34,7 @@ GWeatherParser *_gweather_parser_new                 (void);
 void            _gweather_parser_free                (GWeatherParser *parser);
 
 char           *_gweather_parser_get_value           (GWeatherParser *parser);
+char           *_gweather_parser_get_msgctxt_value   (GWeatherParser *parser);
 char           *_gweather_parser_get_localized_value (GWeatherParser *parser);
 
 /* from gweather-timezone.c */
diff --git a/libgweather/weather-priv.h b/libgweather/weather-priv.h
index aca3d7e..01d81c5 100644
--- a/libgweather/weather-priv.h
+++ b/libgweather/weather-priv.h
@@ -42,7 +42,7 @@ void        _gweather_gettext_init (void);
 #define N_(str) (str)
 
 struct _GWeatherLocation {
-    char *name, *sort_name;
+    char *name, *msgctxt, *sort_name;
     GWeatherLocation *parent, **children;
     GWeatherLocationLevel level;
     char *country_code, *tz_hint;


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