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"
30 } LightDMLanguagePrivate;
32 G_DEFINE_TYPE (LightDMLanguage, lightdm_language, G_TYPE_OBJECT);
34 #define GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_LANGUAGE, LightDMLanguagePrivate)
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 GET_PRIVATE (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 LightDMLanguagePrivate *priv;
220 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
222 priv = GET_PRIVATE (language);
226 gchar *locale = get_locale_name (priv->code);
229 gchar *current = setlocale (LC_ALL, NULL);
230 setlocale (LC_IDENTIFICATION, locale);
231 setlocale (LC_MESSAGES, "");
233 gchar *language_en = nl_langinfo (_NL_IDENTIFICATION_LANGUAGE);
234 if (language_en && strlen (language_en) > 0)
235 priv->name = g_strdup (dgettext ("iso_639_3", language_en));
237 setlocale (LC_ALL, current);
241 gchar **tokens = g_strsplit_set (priv->code, "_.@", 2);
242 priv->name = g_strdup (tokens[0]);
251 * lightdm_language_get_territory:
252 * @language: A #LightDMLanguage
254 * Get the territory the language is used in.
256 * Return value: The territory the language is used in.
259 lightdm_language_get_territory (LightDMLanguage *language)
261 LightDMLanguagePrivate *priv;
263 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
265 priv = GET_PRIVATE (language);
267 if (!priv->territory && strchr (priv->code, '_'))
269 gchar *locale = get_locale_name (priv->code);
272 gchar *current = setlocale (LC_ALL, NULL);
273 setlocale (LC_IDENTIFICATION, locale);
274 setlocale (LC_MESSAGES, "");
276 gchar *country_en = nl_langinfo (_NL_IDENTIFICATION_TERRITORY);
277 if (country_en && strlen (country_en) > 0 && g_strcmp0 (country_en, "ISO") != 0)
278 priv->territory = g_strdup (dgettext ("iso_3166", country_en));
280 setlocale (LC_ALL, current);
282 if (!priv->territory)
284 gchar **tokens = g_strsplit_set (priv->code, "_.@", 3);
285 priv->territory = g_strdup (tokens[1]);
290 return priv->territory;
294 * lightdm_language_matches:
295 * @language: A #LightDMLanguage
296 * @code: A language code
298 * Check if a language code matches this language.
300 * Return value: #TRUE if the code matches this language.
303 lightdm_language_matches (LightDMLanguage *language, const gchar *code)
305 LightDMLanguagePrivate *priv;
307 g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), FALSE);
308 g_return_val_if_fail (code != NULL, FALSE);
310 priv = GET_PRIVATE (language);
312 /* Handle the fact the UTF-8 is specified both as '.utf8' and '.UTF-8' */
313 if (is_utf8 (priv->code) && is_utf8 (code))
315 /* Match the characters before the '.' */
317 for (i = 0; priv->code[i] && code[i] && priv->code[i] == code[i] && code[i] != '.' ; i++);
318 return priv->code[i] == '.' && code[i] == '.';
321 return g_str_equal (priv->code, code);
325 lightdm_language_init (LightDMLanguage *language)
330 lightdm_language_set_property (GObject *object,
335 LightDMLanguage *self = LIGHTDM_LANGUAGE (object);
336 LightDMLanguagePrivate *priv = GET_PRIVATE (self);
341 priv->code = g_strdup (g_value_get_string (value));
344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350 lightdm_language_get_property (GObject *object,
355 LightDMLanguage *self;
357 self = LIGHTDM_LANGUAGE (object);
361 g_value_set_string (value, lightdm_language_get_code (self));
364 g_value_set_string (value, lightdm_language_get_name (self));
367 g_value_set_string (value, lightdm_language_get_territory (self));
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376 lightdm_language_class_init (LightDMLanguageClass *klass)
378 GObjectClass *object_class = G_OBJECT_CLASS (klass);
380 g_type_class_add_private (klass, sizeof (LightDMLanguagePrivate));
382 object_class->set_property = lightdm_language_set_property;
383 object_class->get_property = lightdm_language_get_property;
385 g_object_class_install_property (object_class,
387 g_param_spec_string ("code",
391 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
392 g_object_class_install_property (object_class,
394 g_param_spec_string ("name",
396 "Name of the language",
399 g_object_class_install_property (object_class,
401 g_param_spec_string ("territory",
403 "Territory the language is from",