[geocode-glib] lib: Add locale aware search results



commit 1080d3f2378c42077e8a128c21af2ef7e0831961
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Oct 18 15:54:22 2012 +0200

    lib: Add locale aware search results
    
    So that accents and other appropriate alphabets are used.

 geocode-glib/geocode-glib.c |   84 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 84 insertions(+), 0 deletions(-)
---
diff --git a/geocode-glib/geocode-glib.c b/geocode-glib/geocode-glib.c
index 11afdc1..19f5c65 100644
--- a/geocode-glib/geocode-glib.c
+++ b/geocode-glib/geocode-glib.c
@@ -22,6 +22,7 @@
 
 #include <string.h>
 #include <errno.h>
+#include <locale.h>
 #include <gio/gio.h>
 #include <json-glib/json-glib.h>
 #include <libsoup/soup.h>
@@ -142,6 +143,83 @@ geocode_object_cache_load (GFile  *query,
 	return ret;
 }
 
+static gboolean
+parse_lang (const char *locale,
+	    char      **language_codep,
+	    char      **territory_codep)
+{
+	GRegex     *re;
+	GMatchInfo *match_info;
+	gboolean    res;
+	GError     *error;
+	gboolean    retval;
+
+	match_info = NULL;
+	retval = FALSE;
+
+	error = NULL;
+	re = g_regex_new ("^(?P<language>[^_  [:space:]]+)"
+			  "(_(?P<territory>[[:upper:]]+))?"
+			  "(\\.(?P<codeset>[-_0-9a-zA-Z]+))?"
+			  "(@(?P<modifier>[[:ascii:]]+))?$",
+			  0, 0, &error);
+	if (re == NULL) {
+		g_warning ("%s", error->message);
+		goto out;
+	}
+
+	if (!g_regex_match (re, locale, 0, &match_info) ||
+	    g_match_info_is_partial_match (match_info)) {
+		g_warning ("locale '%s' isn't valid\n", locale);
+		goto out;
+	}
+
+	res = g_match_info_matches (match_info);
+	if (! res) {
+		g_warning ("Unable to parse locale: %s", locale);
+		goto out;
+	}
+
+	retval = TRUE;
+
+	*language_codep = g_match_info_fetch_named (match_info, "language");
+
+	*territory_codep = g_match_info_fetch_named (match_info, "territory");
+
+	if (*territory_codep != NULL &&
+	    *territory_codep[0] == '\0') {
+		g_free (*territory_codep);
+		*territory_codep = NULL;
+	}
+
+out:
+	g_match_info_free (match_info);
+	g_regex_unref (re);
+
+	return retval;
+}
+
+static char *
+geocode_object_get_lang_for_locale (const char *locale)
+{
+	char *lang;
+	char *territory;
+
+	if (parse_lang (locale, &lang, &territory) == FALSE)
+		return NULL;
+
+	return g_strdup_printf ("%s%s%s",
+				lang,
+				territory ? "_" : "",
+				territory ? territory : "");
+}
+
+static char *
+geocode_object_get_lang (void)
+{
+	return geocode_object_get_lang_for_locale (setlocale (LC_MESSAGES, NULL));
+}
+
 struct {
 	const char *tp_attr;
 	const char *gc_attr; /* NULL to ignore */
@@ -544,6 +622,7 @@ get_query_for_params (GeocodeObject *object)
 	if (object->priv->type == GEOCODE_GLIB_LOOKUP_FORWARD ||
 	    object->priv->type == GEOCODE_GLIB_LOOKUP_REVERSE) {
 		GHashTable *ht;
+		char *locale;
 		char *params, *uri;
 
 		ht = dup_ht (object->priv->ht);
@@ -554,8 +633,13 @@ get_query_for_params (GeocodeObject *object)
 		if (object->priv->type == GEOCODE_GLIB_LOOKUP_REVERSE)
 			g_hash_table_insert (ht, "gflags", "R");
 
+		locale = geocode_object_get_lang ();
+		if (locale)
+			g_hash_table_insert (ht, "locale", locale);
+
 		params = soup_form_encode_hash (ht);
 		g_hash_table_destroy (ht);
+		g_free (locale);
 
 		uri = g_strdup_printf ("http://where.yahooapis.com/geocode?%s";, params);
 		g_free (params);



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