Blame ibus/lang.py

Packit 3ff832
# vim:set et sts=4 sw=4:
Packit 3ff832
#
Packit 3ff832
# ibus - The Input Bus
Packit 3ff832
#
Packit 3ff832
# Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
# Copyright (c) 2007-2010 Red Hat, Inc.
Packit 3ff832
#
Packit 3ff832
# This library is free software; you can redistribute it and/or
Packit 3ff832
# modify it under the terms of the GNU Lesser General Public
Packit 3ff832
# License as published by the Free Software Foundation; either
Packit 3ff832
# version 2.1 of the License, or (at your option) any later version.
Packit 3ff832
#
Packit 3ff832
# This library is distributed in the hope that it will be useful,
Packit 3ff832
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff832
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3ff832
# Lesser General Public License for more details.
Packit 3ff832
#
Packit 3ff832
# You should have received a copy of the GNU Lesser General Public
Packit 3ff832
# License along with this library; if not, write to the Free Software
Packit 3ff832
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit 3ff832
# USA
Packit 3ff832
Packit 3ff832
__all__ = (
Packit 3ff832
        "get_language_name",
Packit 3ff832
    )
Packit 3ff832
Packit 3ff832
import xml.parsers.expat
Packit 3ff832
import locale
Packit 3ff832
import gettext
Packit 3ff832
Packit 3ff832
_ = lambda a: gettext.dgettext("ibus", a)
Packit 3ff832
__languages_dict = {}
Packit 3ff832
Packit 3ff832
def get_language_name(_locale):
Packit 3ff832
    lang = _locale.split("_")[0]
Packit 3ff832
    lang = lang.lower()
Packit 3ff832
    if lang in __languages_dict:
Packit 3ff832
        lang = __languages_dict[lang]
Packit 3ff832
        lang = gettext.dgettext("iso_639", lang)
Packit 3ff832
    else:
Packit 3ff832
        lang = _(u"Other")
Packit 3ff832
        lang = gettext.dgettext("ibus", lang)
Packit 3ff832
    return lang
Packit 3ff832
Packit 3ff832
def __start_element(name, attrs):
Packit 3ff832
    global __languages_dict
Packit 3ff832
    try:
Packit 3ff832
        name = attrs[u"name"]
Packit 3ff832
        for attr_name in (u"iso_639_2B_code", u"iso_639_2T_code", u"iso_639_1_code"):
Packit 3ff832
            if attr_name in attrs:
Packit 3ff832
                attr_value = attrs[attr_name]
Packit 3ff832
                __languages_dict[attr_value] = name
Packit 3ff832
    except:
Packit 3ff832
        pass
Packit 3ff832
Packit 3ff832
def __end_element(name):
Packit 3ff832
    pass
Packit 3ff832
Packit 3ff832
def __char_data(data):
Packit 3ff832
    pass
Packit 3ff832
Packit 3ff832
def __load_lang():
Packit 3ff832
    import os
Packit 3ff832
    import _config
Packit 3ff832
    iso_639_xml = os.path.join(_config.ISOCODES_PREFIX, "share/xml/iso-codes/iso_639.xml")
Packit 3ff832
    p = xml.parsers.expat.ParserCreate()
Packit 3ff832
    p.StartElementHandler = __start_element
Packit 3ff832
    p.EndElementHandler = __end_element
Packit 3ff832
    p.CharacterDataHandler = __char_data
Packit 3ff832
    p.ParseFile(file(iso_639_xml))
Packit 3ff832
Packit 3ff832
__load_lang()
Packit 3ff832
Packit 3ff832
if __name__ == "__main__":
Packit 3ff832
    print get_language_name("mai")
Packit 3ff832
    print get_language_name("zh")
Packit 3ff832
    print get_language_name("ja")
Packit 3ff832
    print get_language_name("ko")