]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blobdiff - liblightdm-gobject/language.c
Revert r2392 - it seems to have broken ABI in liblightdm-gobject
[sojka/lightdm.git] / liblightdm-gobject / language.c
index e929a35c4a0535e2604f37e8189d4fc202d7547c..ac17645cd3c142f88a8acfa2bbcbc04cbf98c746 100644 (file)
@@ -1,23 +1,23 @@
 /*
  * Copyright (C) 2010 Robert Ancell.
  * Author: Robert Ancell <robert.ancell@canonical.com>
- * 
+ *
  * This library is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option) any
- * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
- * license.
+ * Software Foundation; either version 2 or version 3 of the License.
+ * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
  */
 
 #include <string.h>
 #include <locale.h>
 #include <langinfo.h>
+#include <stdio.h>
+#include <glib/gi18n.h>
 
 #include "lightdm/language.h"
 
 enum {
-    PROP_0,
-    PROP_CODE,
+    PROP_CODE = 1,
     PROP_NAME,
     PROP_TERRITORY
 };
@@ -39,6 +39,7 @@ static GList *languages = NULL;
 static void
 update_languages (void)
 {
+    gchar *command = "locale -a";
     gchar *stdout_text = NULL, *stderr_text = NULL;
     gint exit_status;
     gboolean result;
@@ -47,10 +48,15 @@ update_languages (void)
     if (have_languages)
         return;
 
-    result = g_spawn_command_line_sync ("locale -a", &stdout_text, &stderr_text, &exit_status, &error);
-    if (!result || exit_status != 0)
-        g_warning ("Failed to get languages, locale -a returned %d: %s", exit_status, error->message);
-    else
+    result = g_spawn_command_line_sync (command, &stdout_text, &stderr_text, &exit_status, &error);
+    if (error)
+    {
+        g_warning ("Failed to run '%s': %s", command, error->message);
+        g_clear_error (&error);
+    }
+    else if (exit_status != 0)
+        g_warning ("Failed to get languages, '%s' returned %d", command, exit_status);
+    else if (result)
     {
         gchar **tokens;
         int i;
@@ -66,7 +72,7 @@ update_languages (void)
                 continue;
 
             /* Ignore the non-interesting languages */
-            if (strcmp (code, "C") == 0 || strcmp (code, "POSIX") == 0)
+            if (strcmp (command, "locale -a") == 0 && !g_strrstr (code, ".utf8"))
                 continue;
 
             language = g_object_new (LIGHTDM_TYPE_LANGUAGE, "code", code, NULL);
@@ -76,13 +82,72 @@ update_languages (void)
         g_strfreev (tokens);
     }
 
-    g_clear_error (&error);
     g_free (stdout_text);
     g_free (stderr_text);
 
     have_languages = TRUE;
 }
 
+static gboolean
+is_utf8 (const gchar *code)
+{
+    return g_strrstr (code, ".utf8") || g_strrstr (code, ".UTF-8");
+}
+
+/* Get a valid locale name that can be passed to setlocale(), so we always can use nl_langinfo() to get language and country names. */
+static gchar *
+get_locale_name (const gchar *code)
+{
+    gchar *locale = NULL, *language;
+    char *at;
+    static gchar **avail_locales;
+    gint i;
+
+    if (is_utf8 (code))
+        return (gchar *) code;
+
+    if ((at = strchr (code, '@')))
+        language = g_strndup (code, at - code);
+    else
+        language = g_strdup (code);
+
+    if (!avail_locales)
+    {
+        gchar *locales;
+        GError *error = NULL;
+
+        if (g_spawn_command_line_sync ("locale -a", &locales, NULL, NULL, &error))
+        {
+            avail_locales = g_strsplit (g_strchomp (locales), "\n", -1);
+            g_free (locales);
+        }
+        else
+        {
+            g_warning ("Failed to run 'locale -a': %s", error->message);
+            g_clear_error (&error);
+        }
+    }
+
+    if (avail_locales)
+    {
+        for (i = 0; avail_locales[i]; i++)
+        {
+            gchar *loc = avail_locales[i];
+            if (!g_strrstr (loc, ".utf8"))
+                continue;
+            if (g_str_has_prefix (loc, language))
+            {
+                locale = g_strdup (loc);
+                break;
+            }
+        }
+    }
+
+    g_free (language);
+
+    return locale;
+}
+
 /**
  * lightdm_get_language:
  *
@@ -90,13 +155,16 @@ update_languages (void)
  *
  * Return value: (transfer none): The current language or #NULL if no language.
  **/
-const LightDMLanguage *
+LightDMLanguage *
 lightdm_get_language (void)
 {
     const gchar *lang;
     GList *link;
 
     lang = g_getenv ("LANG");
+    if (!lang)
+        return NULL;
+
     for (link = lightdm_get_languages (); link; link = link->next)
     {
         LightDMLanguage *language = link->data;
@@ -124,9 +192,9 @@ lightdm_get_languages (void)
 /**
  * lightdm_language_get_code:
  * @language: A #LightDMLanguage
- * 
- * Get the code of a language.
- * 
+ *
+ * Get the code of a language (e.g. "de_DE.UTF-8")
+ *
  * Return value: The code of the language
  **/
 const gchar *
@@ -139,7 +207,7 @@ lightdm_language_get_code (LightDMLanguage *language)
 /**
  * lightdm_language_get_name:
  * @language: A #LightDMLanguage
- * 
+ *
  * Get the name of a language.
  *
  * Return value: The name of the language
@@ -155,14 +223,25 @@ lightdm_language_get_name (LightDMLanguage *language)
 
     if (!priv->name)
     {
-        char *current = setlocale(LC_ALL, NULL);
-        setlocale(LC_ALL, priv->code);
-#ifdef _NL_IDENTIFICATION_LANGUAGE
-        priv->name = g_strdup (nl_langinfo (_NL_IDENTIFICATION_LANGUAGE));
-#else
-        priv->name = g_strdup ("Unknown");
-#endif
-        setlocale(LC_ALL, current);
+        gchar *locale = get_locale_name (priv->code);
+        if (locale)
+        {
+            gchar *current = setlocale (LC_ALL, NULL);
+            setlocale (LC_IDENTIFICATION, locale);
+            setlocale (LC_MESSAGES, "");
+
+            gchar *language_en = nl_langinfo (_NL_IDENTIFICATION_LANGUAGE);
+            if (language_en && strlen (language_en) > 0)
+                priv->name = g_strdup (dgettext ("iso_639_3", language_en));
+
+            setlocale (LC_ALL, current);
+        }
+        if (!priv->name)
+        {
+            gchar **tokens = g_strsplit_set (priv->code, "_.@", 2);
+            priv->name = g_strdup (tokens[0]);
+            g_strfreev (tokens);
+        }
     }
 
     return priv->name;
@@ -171,9 +250,9 @@ lightdm_language_get_name (LightDMLanguage *language)
 /**
  * lightdm_language_get_territory:
  * @language: A #LightDMLanguage
- * 
+ *
  * Get the territory the language is used in.
- * 
+ *
  * Return value: The territory the language is used in.
  **/
 const gchar *
@@ -185,34 +264,39 @@ lightdm_language_get_territory (LightDMLanguage *language)
 
     priv = GET_PRIVATE (language);
 
-    if (!priv->territory)
+    if (!priv->territory && strchr (priv->code, '_'))
     {
-        char *current = setlocale(LC_ALL, NULL);
-        setlocale(LC_ALL, priv->code);
-#ifdef _NL_IDENTIFICATION_TERRITORY
-        priv->territory = g_strdup (nl_langinfo (_NL_IDENTIFICATION_TERRITORY));
-#else
-        priv->territory = g_strdup ("Unknown");
-#endif
-        setlocale(LC_ALL, current);
+        gchar *locale = get_locale_name (priv->code);
+        if (locale)
+        {
+            gchar *current = setlocale (LC_ALL, NULL);
+            setlocale (LC_IDENTIFICATION, locale);
+            setlocale (LC_MESSAGES, "");
+
+            gchar *country_en = nl_langinfo (_NL_IDENTIFICATION_TERRITORY);
+            if (country_en && strlen (country_en) > 0 && g_strcmp0 (country_en, "ISO") != 0)
+                priv->territory = g_strdup (dgettext ("iso_3166", country_en));
+
+            setlocale (LC_ALL, current);
+        }
+        if (!priv->territory)
+        {
+            gchar **tokens = g_strsplit_set (priv->code, "_.@", 3);
+            priv->territory = g_strdup (tokens[1]);
+            g_strfreev (tokens);
+        }
     }
 
     return priv->territory;
 }
 
-static gboolean
-is_utf8 (const gchar *code)
-{
-    return g_str_has_suffix (code, ".utf8") || g_str_has_suffix (code, ".UTF-8");
-}
-
 /**
  * lightdm_language_matches:
  * @language: A #LightDMLanguage
  * @code: A language code
- * 
+ *
  * Check if a language code matches this language.
- * 
+ *
  * Return value: #TRUE if the code matches this language.
  **/
 gboolean
@@ -244,9 +328,9 @@ lightdm_language_init (LightDMLanguage *language)
 
 static void
 lightdm_language_set_property (GObject      *object,
-                           guint         prop_id,
-                           const GValue *value,
-                           GParamSpec   *pspec)
+                               guint         prop_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
 {
     LightDMLanguage *self = LIGHTDM_LANGUAGE (object);
     LightDMLanguagePrivate *priv = GET_PRIVATE (self);
@@ -264,9 +348,9 @@ lightdm_language_set_property (GObject      *object,
 
 static void
 lightdm_language_get_property (GObject    *object,
-                           guint       prop_id,
-                           GValue     *value,
-                           GParamSpec *pspec)
+                               guint       prop_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
 {
     LightDMLanguage *self;
 
@@ -292,7 +376,7 @@ static void
 lightdm_language_class_init (LightDMLanguageClass *klass)
 {
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
-  
+
     g_type_class_add_private (klass, sizeof (LightDMLanguagePrivate));
 
     object_class->set_property = lightdm_language_set_property;