[network-manager-applet] po/build: use iso-codes package to get translated country names (bgo #658653)



commit 2f821638bcfc02cf6a81c11ed5f46b84018b0631
Author: JiÅÃ KlimeÅ <jklimes redhat com>
Date:   Tue Sep 13 16:00:10 2011 +0200

    po/build: use iso-codes package to get translated country names (bgo #658653)
    
    'iso-codes' provides ISO country, language, script and currency codes and their
    translations in gettext format.
    Even it's another dependency, it's very convenient, because there's no need to
    duplicate translations of countries in nm-aplet.

 configure.ac                     |   32 +++++++++-
 m4/translate_countries.m4        |    7 --
 po/POTFILES.in                   |    6 --
 po/Rules-iso3166                 |   20 ------
 src/utils/nmn-mobile-providers.c |  129 ++++++++++++++++++++++----------------
 5 files changed, 104 insertions(+), 90 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index ed39e79..8805c79 100644
--- a/configure.ac
+++ b/configure.ac
@@ -21,6 +21,7 @@ dnl
 AC_PROG_CC
 AM_PROG_CC_C_O
 AC_PROG_INSTALL
+PKG_PROG_PKG_CONFIG
 
 dnl Define _GNU_SOURCE for various things like strcasestr()
 AC_GNU_SOURCE
@@ -57,8 +58,35 @@ GETTEXT_PACKAGE=nm-applet
 AC_SUBST(GETTEXT_PACKAGE)
 AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Gettext package])
 AM_GLIB_GNU_GETTEXT
-# for translation of country names from /usr/share/zoneinfo/iso3166.tab
-TRANSLATE_COUNTRY_NAMES([po])
+
+# Check for iso-codes for country names translation
+AC_MSG_CHECKING([whether to disable iso-codes at build-time])
+AC_ARG_ENABLE([iso-codes],
+    AS_HELP_STRING([--disable-iso-codes],[do not check for iso-codes at build-time]),
+    [],[disable_iso_codes_check=no])
+if test x$disable_iso_codes_check = xno ; then
+    AC_MSG_RESULT([no])
+else
+    AC_MSG_RESULT([yes])
+fi
+
+if test x$disable_iso_codes_check = "xno" ; then
+    AC_MSG_CHECKING([whether iso-codes has iso_3166 domain])
+    if $PKG_CONFIG --variable=domains iso-codes | grep iso_3166 >/dev/null ; then
+        AC_MSG_RESULT([yes])
+    else
+        AC_MSG_RESULT([no])
+    fi
+    AC_DEFINE_UNQUOTED([ISO_CODES_PREFIX],["`$PKG_CONFIG --variable=prefix iso-codes`"],[ISO codes prefix])
+    PKG_CHECK_MODULES(ISO_CODES, [iso-codes],
+        [],
+        [echo -e "\n$ISO_CODES_PKG_ERRORS.\n"
+         echo "Consider installing the package or adjusting the PKG_CONFIG_PATH environment variable."
+         echo "You can also disable build-time check for 'iso-codes' via --disable-iso-codes";
+         exit 1;])
+else
+    AC_DEFINE_UNQUOTED([ISO_CODES_PREFIX],["$prefix"],[ISO codes prefix])
+fi
 
 dnl
 dnl Make sha1.c happy on big endian systems
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4e548cc..4afbe21 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -15,12 +15,6 @@ src/applet.c
 src/applet.h
 [type: gettext/glade]src/gsm-unlock.ui
 [type: gettext/glade]src/info.ui
-
-# Generated file for country names translation
-# (generated from /usr/share/zoneinfo/iso3166.tab)
-# see Rules-iso3166
-po/iso3166.tab.h
-
 [type: gettext/glade]src/wired-8021x.ui
 src/connection-editor/ce-page.c
 [type: gettext/glade]src/connection-editor/ce-ip4-routes.ui
diff --git a/src/utils/nmn-mobile-providers.c b/src/utils/nmn-mobile-providers.c
index 8a244e8..05b9af0 100644
--- a/src/utils/nmn-mobile-providers.c
+++ b/src/utils/nmn-mobile-providers.c
@@ -35,76 +35,95 @@
 #define MOBILE_BROADBAND_PROVIDER_INFO DATADIR"/mobile-broadband-provider-info/serviceproviders.xml"
 #endif
 
-#define ISO_3166_COUNTRY_CODES DATADIR"/zoneinfo/iso3166.tab"
+#define ISO_3166_COUNTRY_CODES ISO_CODES_PREFIX"/share/xml/iso-codes/iso_3166.xml"
+#define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX"/share/locale"
 
-static GHashTable *
-read_country_codes (void)
+
+/* XML Parser for iso_3166.xml */
+
+static void
+iso_3166_parser_start_element (GMarkupParseContext *context,
+                               const gchar *element_name,
+                               const gchar **attribute_names,
+                               const gchar **attribute_values,
+                               gpointer data,
+                               GError **error)
 {
-    GHashTable *table;
-    GIOChannel *channel;
-    GString *buffer;
-    GError *error = NULL;
-    GIOStatus status;
+    int i;
+    const char *country_code = NULL;
+    const char *common_name = NULL;
+    const char *name = NULL;
+    GHashTable *table = (GHashTable *) data;
 
-    channel = g_io_channel_new_file (ISO_3166_COUNTRY_CODES, "r", &error);
-    if (!channel) {
-        if (error) {
-            g_warning ("Could not read " ISO_3166_COUNTRY_CODES ": %s", error->message);
-            g_error_free (error);
-        } else
-            g_warning ("Could not read " ISO_3166_COUNTRY_CODES ": Unknown error");
+    if (!strcmp (element_name, "iso_3166_entry")) {
+        for (i = 0; attribute_names && attribute_names[i]; i++) {
+            if (!strcmp (attribute_names[i], "alpha_2_code"))
+                country_code = attribute_values[i];
+            else if (!strcmp (attribute_names[i], "common_name"))
+                common_name = attribute_values[i];
+            else if (!strcmp (attribute_names[i], "name"))
+                name = attribute_values[i];
+        }
+        if (!country_code) {
+            g_warning ("%s: missing mandatory 'alpha_2_code' atribute in '%s'"
+                       " element.", __func__, element_name);
+            return;
+        }
+        if (!name) {
+            g_warning ("%s: missing mandatory 'name' atribute in '%s'"
+                       " element.", __func__, element_name);
+            return;
+        }
 
-        return NULL;
+        g_hash_table_insert (table, g_strdup (country_code), g_strdup (dgettext ("iso_3166", common_name ? common_name : name)));
     }
+}
 
-    table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-    buffer = g_string_sized_new (32);
+static const GMarkupParser iso_3166_parser = {
+    iso_3166_parser_start_element,
+    NULL, /* end element */
+    NULL, /* text */
+    NULL, /* passthrough */
+    NULL  /* error */
+};
 
-    status = G_IO_STATUS_NORMAL;
-    while (status == G_IO_STATUS_NORMAL) {
-        status = g_io_channel_read_line_string (channel, buffer, NULL, &error);
+static GHashTable *
+read_country_codes (void)
+{
+    GHashTable *table = NULL;
+    GMarkupParseContext *ctx;
+    GError *error = NULL;
+    char *buf;
+    gsize buf_len;
 
-        switch (status) {
-        case G_IO_STATUS_NORMAL:
-            if (buffer->str[0] != '#') {
-                char **pieces;
-                char *country_name;
-
-                pieces = g_strsplit (buffer->str, "\t", 2);
-
-                /* Hack for rh#556292; iso3166.tab is just wrong */
-                pieces[1] = pieces[1] ? g_strchomp (pieces[1]) : NULL;
-                if (pieces[1] && !strcmp (pieces[1], "Britain (UK)"))
-                    country_name = g_strdup (_("United Kingdom"));
-                else
-                    country_name = g_strdup (gettext (pieces[1]));
-
-                g_hash_table_insert (table, pieces[0], country_name);
-                g_free (pieces[1]);
-                g_free (pieces);
-            }
+    /* Set domain to iso_3166 for country name translation */
+    bindtextdomain ("iso_3166", ISO_CODES_LOCALESDIR);
+    bind_textdomain_codeset ("iso_3166", "UTF-8");
 
-            g_string_truncate (buffer, 0);
-            break;
-        case G_IO_STATUS_EOF:
-            break;
-        case G_IO_STATUS_ERROR:
-            g_warning ("Error while reading: %s", error->message);
+    if (g_file_get_contents (ISO_3166_COUNTRY_CODES, &buf, &buf_len, &error)) {
+        table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+        ctx = g_markup_parse_context_new (&iso_3166_parser, 0, table, NULL);
+
+        if (!g_markup_parse_context_parse (ctx, buf, buf_len, &error)) {
+            g_warning ("Failed to parse '%s': %s\n", ISO_3166_COUNTRY_CODES, error->message);
             g_error_free (error);
-            break;
-        case G_IO_STATUS_AGAIN:
-            /* FIXME: Try again a few times, but really, it never happes, right? */
-            break;
+            g_hash_table_destroy (table);
+            table = NULL;
         }
-    }
 
-    g_string_free (buffer, TRUE);
-    g_io_channel_unref (channel);
+        g_markup_parse_context_free (ctx);
+        g_free (buf);
+    } else {
+        g_warning ("Failed to load '%s': %s\n Consider installing 'iso-codes'\n",
+                   ISO_3166_COUNTRY_CODES, error->message);
+        g_error_free (error);
+    }
 
     return table;
 }
 
-/* XML Parser */
+
+/* XML Parser for serviceproviders.xml */
 
 typedef enum {
     PARSER_TOPLEVEL = 0,



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