1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
4 * Copyright (C) 2008-2011 Peng Huang <shawn.p.huang@gmail.com>
5 * Copyright (C) 2010-2011 Takao Fujiwara <takao.fujiwara1@gmail.com>
6 * Copyright (C) 2008-2011 Red Hat, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 #include <glib/gstdio.h>
40 static GHashTable *__languages_dict;
43 _iso_codes_parse_xml_node (XMLNode *node)
48 if (G_UNLIKELY (g_strcmp0 (node->name, "iso_639_entries") != 0)) {
52 for (p = node->sub_nodes; p != NULL; p = p->next) {
53 XMLNode *sub_node = (XMLNode *)p->data;
54 gchar **attributes = NULL;
60 { "iso_639_2B_code", NULL },
61 { "iso_639_2T_code", NULL },
62 { "iso_639_1_code", NULL },
65 if (sub_node->attributes == NULL) {
69 attributes = sub_node->attributes;
70 for (i = 0; attributes[i]; i += 2) {
71 if (g_strcmp0 (attributes[i], "name") == 0) {
72 for (j = 0; j < G_N_ELEMENTS (entries); j++) {
73 if (entries[j].value == NULL)
75 g_hash_table_insert (__languages_dict,
76 (gpointer) g_strdup (entries[j].value),
77 (gpointer) g_strdup (attributes[i + 1]));
78 entries[j].value = NULL;
81 for (j = 0; j < G_N_ELEMENTS (entries); j++) {
82 if (g_strcmp0 (attributes[i], entries[j].key) == 0 &&
83 attributes[i + 1] != NULL) {
84 entries[j].value = attributes[i + 1];
101 __languages_dict = g_hash_table_new_full (g_str_hash,
102 g_str_equal, g_free, g_free);
103 filename = g_build_filename (ISOCODES_PREFIX,
104 "share/xml/iso-codes/iso_639.xml",
106 if (g_stat (filename, &buf) != 0) {
107 g_warning ("Can not get stat of file %s", filename);
112 node = ibus_xml_parse_file (filename);
119 _iso_codes_parse_xml_node (node);
120 ibus_xml_free (node);
124 ibus_get_language_name(const gchar *_locale) {
129 if (__languages_dict == NULL ) {
132 if ((p = strchr (_locale, '_')) != NULL) {
133 p = g_strndup (_locale, p - _locale);
135 p = g_strdup (_locale);
137 lang = g_ascii_strdown (p, -1);
139 retval = (const gchar *) g_hash_table_lookup (__languages_dict, lang);
141 if (retval != NULL) {
143 return dgettext("iso_639", retval);
150 return dgettext(GETTEXT_PACKAGE, N_("Other"));