~mvo/software-center/lp957599

« back to all changes in this revision

Viewing changes to softwarecenter/i18n.py

  • Committer: Michael Vogt
  • Date: 2012-03-15 18:20:17 UTC
  • mfrom: (2863.1.1 pep8-test-part1)
  • Revision ID: michael.vogt@ubuntu.com-20120315182017-o14wchqvn8vr40m8
mergedĀ lp:~mvo/software-center/pep8-test-1/

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
FALLBACK = "en"
26
26
# those languages need the full language-code, the other ones
27
27
# can be abbreved
28
 
FULL = ["pt_BR", 
 
28
FULL = ["pt_BR",
29
29
        "zh_CN", "zh_TW"]
30
30
 
 
31
 
31
32
def init_locale():
32
33
    try:
33
34
        locale.setlocale(locale.LC_ALL, "")
52
53
        return [get_language()]
53
54
    return langs
54
55
 
 
56
 
55
57
def get_language():
56
58
    """Helper that returns the current language
57
59
    """
58
60
    try:
59
 
        language = locale.getdefaultlocale(('LANGUAGE','LANG','LC_CTYPE','LC_ALL'))[0]
 
61
        language = locale.getdefaultlocale(
 
62
            ('LANGUAGE', 'LANG', 'LC_CTYPE', 'LC_ALL'))[0]
60
63
    except Exception as e:
61
64
        LOG.warn("Failed to get language: '%s'" % e)
62
65
        language = "C"
67
70
        return language
68
71
    return language.split("_")[0]
69
72
 
 
73
 
70
74
def langcode_to_name(langcode):
71
75
    import xml.etree.ElementTree
72
76
    from gettext import dgettext
73
77
    for iso in ["iso_639_3", "iso_639"]:
74
 
        path = os.path.join("/usr/share/xml/iso-codes/", iso+".xml")
 
78
        path = os.path.join("/usr/share/xml/iso-codes/", iso + ".xml")
75
79
        if os.path.exists(path):
76
80
            root = xml.etree.ElementTree.parse(path)
77
81
            xpath = ".//%s_entry[@part1_code='%s']" % (iso, langcode)
79
83
            if match is not None:
80
84
                return dgettext(iso, match.attrib["name"])
81
85
    return langcode
82