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"
31 } LightDMLanguagePrivate;
33 G_DEFINE_TYPE (LightDMLanguage, lightdm_language, G_TYPE_OBJECT);
35 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_LANGUAGE, LightDMLanguagePrivate)
37 static gboolean have_languages = FALSE;
38 static GList *languages = NULL;
41 update_languages (void)
43 gchar *command = "locale -a";
44 gchar *stdout_text = NULL, *stderr_text = NULL;
52 result = g_spawn_command_line_sync (command, &stdout_text, &stderr_text, &exit_status, &error);
55 g_warning ("Failed to run '%s': %s", command, error->message);
56 g_clear_error (&error);
58 else if (exit_status != 0)
59 g_warning ("Failed to get languages, '%s' returned %d", command, exit_status);
65 tokens = g_strsplit_set (stdout_text, "\n\r", -1);
66 for (i = 0; tokens[i]; i++)
68 LightDMLanguage *language;
71 code = g_strchug (tokens[i]);
75 /* Ignore the non-interesting languages */
76 if (strcmp (command, "locale -a") == 0 && !g_strrstr (code, ".utf8"))
79 language = g_object_new (LIGHTDM_TYPE_LANGUAGE, "code", code, NULL);
80 languages = g_list_append (languages, language);
89 have_languages = TRUE;
93 is_utf8 (const gchar *code)
95 return g_strrstr (code, ".utf8") || g_strrstr (code, ".UTF-8");
98 /* Get a valid locale name that can be passed to setlocale(), so we always can use nl_langinfo() to get language and country names. */
100 get_locale_name (const gchar *code)
102 gchar *locale = NULL, *language;
104 static gchar **avail_locales;
108 return (gchar *) code;
110 if ((at = strchr (code, '@')))
111 language = g_strndup (code, at - code);
113 language = g_strdup (code);
118 GError *error = NULL;
120 if (g_spawn_command_line_sync ("locale -a", &locales, NULL, NULL, &error))
122 avail_locales = g_strsplit (g_strchomp (locales), "\n", -1);
127 g_warning ("Failed to run 'locale -a': %s", error->message);
128 g_clear_error (&error);
134 for (i = 0; avail_locales[i]; i++)
136 gchar *loc = avail_locales[i];
137 if (!g_strrstr (loc, ".utf8"))
139 if (g_str_has_prefix (loc, language))
141 locale = g_strdup (loc);
153 * lightdm_get_language:
155 * Get the current language.
157 * Return value: (transfer none): The current language or #NULL if no language.
160 lightdm_get_language (void)
165 lang = g_getenv ("LANG");
169 for (link = lightdm_get_languages (); link; link = link->next)
171 LightDMLanguage *language = link->data;
172 if (lightdm_language_matches (language, lang))
180 * lightdm_get_languages:
182 * Get a list of languages to present to the user.
184 * Return value: (element-type LightDMLanguage) (transfer none): A list of #LightDMLanguage that should be presented to the user.
187 lightdm_get_languages (void)
194 * lightdm_language_get_code:
195 * @language: A #LightDMLanguage
197 * Get the code of a language (e.g. "de_DE.UTF-8")
199 * Return value: The code of the language
202 lightdm_language_get_code (LightDMLanguage *language)
204 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
205 return GET_PRIVATE (language)->code;
209 * lightdm_language_get_name:
210 * @language: A #LightDMLanguage
212 * Get the name of a language.
214 * Return value: The name of the language
217 lightdm_language_get_name (LightDMLanguage *language)
219 LightDMLanguagePrivate *priv;
221 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
223 priv = GET_PRIVATE (language);
227 gchar *locale = get_locale_name (priv->code);
230 gchar *current = setlocale (LC_ALL, NULL);
231 setlocale (LC_IDENTIFICATION, locale);
232 setlocale (LC_MESSAGES, "");
234 gchar *language_en = nl_langinfo (_NL_IDENTIFICATION_LANGUAGE);
235 if (language_en && strlen (language_en) > 0)
236 priv->name = g_strdup (dgettext ("iso_639_3", language_en));
238 setlocale (LC_ALL, current);
242 gchar **tokens = g_strsplit_set (priv->code, "_.@", 2);
243 priv->name = g_strdup (tokens[0]);
252 * lightdm_language_get_territory:
253 * @language: A #LightDMLanguage
255 * Get the territory the language is used in.
257 * Return value: The territory the language is used in.
260 lightdm_language_get_territory (LightDMLanguage *language)
262 LightDMLanguagePrivate *priv;
264 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
266 priv = GET_PRIVATE (language);
268 if (!priv->territory && strchr (priv->code, '_'))
270 gchar *locale = get_locale_name (priv->code);
273 gchar *current = setlocale (LC_ALL, NULL);
274 setlocale (LC_IDENTIFICATION, locale);
275 setlocale (LC_MESSAGES, "");
277 gchar *country_en = nl_langinfo (_NL_IDENTIFICATION_TERRITORY);
278 if (country_en && strlen (country_en) > 0 && g_strcmp0 (country_en, "ISO") != 0)
279 priv->territory = g_strdup (dgettext ("iso_3166", country_en));
281 setlocale (LC_ALL, current);
283 if (!priv->territory)
285 gchar **tokens = g_strsplit_set (priv->code, "_.@", 3);
286 priv->territory = g_strdup (tokens[1]);
291 return priv->territory;
295 * lightdm_language_matches:
296 * @language: A #LightDMLanguage
297 * @code: A language code
299 * Check if a language code matches this language.
301 * Return value: #TRUE if the code matches this language.
304 lightdm_language_matches (LightDMLanguage *language, const gchar *code)
306 LightDMLanguagePrivate *priv;
308 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), FALSE);
309 g_return_val_if_fail (code != NULL, FALSE);
311 priv = GET_PRIVATE (language);
313 /* Handle the fact the UTF-8 is specified both as '.utf8' and '.UTF-8' */
314 if (is_utf8 (priv->code) && is_utf8 (code))
316 /* Match the characters before the '.' */
318 for (i = 0; priv->code[i] && code[i] && priv->code[i] == code[i] && code[i] != '.' ; i++);
319 return priv->code[i] == '.' && code[i] == '.';
322 return g_str_equal (priv->code, code);
326 lightdm_language_init (LightDMLanguage *language)
331 lightdm_language_set_property (GObject *object,
336 LightDMLanguage *self = LIGHTDM_LANGUAGE (object);
337 LightDMLanguagePrivate *priv = GET_PRIVATE (self);
342 priv->code = g_strdup (g_value_get_string (value));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 lightdm_language_get_property (GObject *object,
356 LightDMLanguage *self;
358 self = LIGHTDM_LANGUAGE (object);
362 g_value_set_string (value, lightdm_language_get_code (self));
365 g_value_set_string (value, lightdm_language_get_name (self));
368 g_value_set_string (value, lightdm_language_get_territory (self));
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 lightdm_language_class_init (LightDMLanguageClass *klass)
379 GObjectClass *object_class = G_OBJECT_CLASS (klass);
381 g_type_class_add_private (klass, sizeof (LightDMLanguagePrivate));
383 object_class->set_property = lightdm_language_set_property;
384 object_class->get_property = lightdm_language_get_property;
386 g_object_class_install_property (object_class,
388 g_param_spec_string ("code",
392 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
393 g_object_class_install_property (object_class,
395 g_param_spec_string ("name",
397 "Name of the language",
400 g_object_class_install_property (object_class,
402 g_param_spec_string ("territory",
404 "Territory the language is from",