2 * Copyright (C) 2010 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 2 or version 3 of the License.
8 * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
15 #include <glib/gi18n.h>
17 #include "lightdm/language.h"
25 struct _LightDMLanguage
27 GObject parent_instance;
34 G_DEFINE_TYPE (LightDMLanguage, lightdm_language, G_TYPE_OBJECT);
36 static gboolean have_languages = FALSE;
37 static GList *languages = NULL;
40 update_languages (void)
42 gchar *command = "locale -a";
43 gchar *stdout_text = NULL, *stderr_text = NULL;
51 result = g_spawn_command_line_sync (command, &stdout_text, &stderr_text, &exit_status, &error);
54 g_warning ("Failed to run '%s': %s", command, error->message);
55 g_clear_error (&error);
57 else if (exit_status != 0)
58 g_warning ("Failed to get languages, '%s' returned %d", command, exit_status);
64 tokens = g_strsplit_set (stdout_text, "\n\r", -1);
65 for (i = 0; tokens[i]; i++)
67 LightDMLanguage *language;
70 code = g_strchug (tokens[i]);
74 /* Ignore the non-interesting languages */
75 if (strcmp (command, "locale -a") == 0 && !g_strrstr (code, ".utf8"))
78 language = g_object_new (LIGHTDM_TYPE_LANGUAGE, "code", code, NULL);
79 languages = g_list_append (languages, language);
88 have_languages = TRUE;
92 is_utf8 (const gchar *code)
94 return g_strrstr (code, ".utf8") || g_strrstr (code, ".UTF-8");
97 /* Get a valid locale name that can be passed to setlocale(), so we always can use nl_langinfo() to get language and country names. */
99 get_locale_name (const gchar *code)
101 gchar *locale = NULL, *language;
103 static gchar **avail_locales;
107 return (gchar *) code;
109 if ((at = strchr (code, '@')))
110 language = g_strndup (code, at - code);
112 language = g_strdup (code);
117 GError *error = NULL;
119 if (g_spawn_command_line_sync ("locale -a", &locales, NULL, NULL, &error))
121 avail_locales = g_strsplit (g_strchomp (locales), "\n", -1);
126 g_warning ("Failed to run 'locale -a': %s", error->message);
127 g_clear_error (&error);
133 for (i = 0; avail_locales[i]; i++)
135 gchar *loc = avail_locales[i];
136 if (!g_strrstr (loc, ".utf8"))
138 if (g_str_has_prefix (loc, language))
140 locale = g_strdup (loc);
152 * lightdm_get_language:
154 * Get the current language.
156 * Return value: (transfer none): The current language or #NULL if no language.
159 lightdm_get_language (void)
164 lang = g_getenv ("LANG");
168 for (link = lightdm_get_languages (); link; link = link->next)
170 LightDMLanguage *language = link->data;
171 if (lightdm_language_matches (language, lang))
179 * lightdm_get_languages:
181 * Get a list of languages to present to the user.
183 * Return value: (element-type LightDMLanguage) (transfer none): A list of #LightDMLanguage that should be presented to the user.
186 lightdm_get_languages (void)
193 * lightdm_language_get_code:
194 * @language: A #LightDMLanguage
196 * Get the code of a language (e.g. "de_DE.UTF-8")
198 * Return value: The code of the language
201 lightdm_language_get_code (LightDMLanguage *language)
203 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
204 return language->code;
208 * lightdm_language_get_name:
209 * @language: A #LightDMLanguage
211 * Get the name of a language.
213 * Return value: The name of the language
216 lightdm_language_get_name (LightDMLanguage *language)
218 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
222 gchar *locale = get_locale_name (language->code);
225 gchar *current = setlocale (LC_ALL, NULL);
226 setlocale (LC_IDENTIFICATION, locale);
227 setlocale (LC_MESSAGES, "");
229 gchar *language_en = nl_langinfo (_NL_IDENTIFICATION_LANGUAGE);
230 if (language_en && strlen (language_en) > 0)
231 language->name = g_strdup (dgettext ("iso_639_3", language_en));
233 setlocale (LC_ALL, current);
237 gchar **tokens = g_strsplit_set (language->code, "_.@", 2);
238 language->name = g_strdup (tokens[0]);
243 return language->name;
247 * lightdm_language_get_territory:
248 * @language: A #LightDMLanguage
250 * Get the territory the language is used in.
252 * Return value: The territory the language is used in.
255 lightdm_language_get_territory (LightDMLanguage *language)
257 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
259 if (!language->territory && strchr (language->code, '_'))
261 gchar *locale = get_locale_name (language->code);
264 gchar *current = setlocale (LC_ALL, NULL);
265 setlocale (LC_IDENTIFICATION, locale);
266 setlocale (LC_MESSAGES, "");
268 gchar *country_en = nl_langinfo (_NL_IDENTIFICATION_TERRITORY);
269 if (country_en && strlen (country_en) > 0 && g_strcmp0 (country_en, "ISO") != 0)
270 language->territory = g_strdup (dgettext ("iso_3166", country_en));
272 setlocale (LC_ALL, current);
274 if (!language->territory)
276 gchar **tokens = g_strsplit_set (language->code, "_.@", 3);
277 language->territory = g_strdup (tokens[1]);
282 return language->territory;
286 * lightdm_language_matches:
287 * @language: A #LightDMLanguage
288 * @code: A language code
290 * Check if a language code matches this language.
292 * Return value: #TRUE if the code matches this language.
295 lightdm_language_matches (LightDMLanguage *language, const gchar *code)
297 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), FALSE);
298 g_return_val_if_fail (code != NULL, FALSE);
300 /* Handle the fact the UTF-8 is specified both as '.utf8' and '.UTF-8' */
301 if (is_utf8 (language->code) && is_utf8 (code))
303 /* Match the characters before the '.' */
305 for (i = 0; language->code[i] && code[i] && language->code[i] == code[i] && code[i] != '.' ; i++);
306 return language->code[i] == '.' && code[i] == '.';
309 return g_str_equal (language->code, code);
313 lightdm_language_init (LightDMLanguage *language)
318 lightdm_language_set_property (GObject *object,
323 LightDMLanguage *self = LIGHTDM_LANGUAGE (object);
328 self->code = g_strdup (g_value_get_string (value));
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337 lightdm_language_get_property (GObject *object,
342 LightDMLanguage *self;
344 self = LIGHTDM_LANGUAGE (object);
348 g_value_set_string (value, lightdm_language_get_code (self));
351 g_value_set_string (value, lightdm_language_get_name (self));
354 g_value_set_string (value, lightdm_language_get_territory (self));
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 lightdm_language_class_init (LightDMLanguageClass *klass)
365 GObjectClass *object_class = G_OBJECT_CLASS (klass);
367 object_class->set_property = lightdm_language_set_property;
368 object_class->get_property = lightdm_language_get_property;
370 g_object_class_install_property (object_class,
372 g_param_spec_string ("code",
376 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
377 g_object_class_install_property (object_class,
379 g_param_spec_string ("name",
381 "Name of the language",
384 g_object_class_install_property (object_class,
386 g_param_spec_string ("territory",
388 "Territory the language is from",