~paolorotolo/software-center/fix-for-963309

« back to all changes in this revision

Viewing changes to softwarecenter/i18n.py

  • Committer: Danny Tamez
  • Date: 2012-01-19 16:17:34 UTC
  • mfrom: (2682 trunk)
  • mto: This revision was merged to the branch mainline in revision 2683.
  • Revision ID: danny.tamez@canonical.com-20120119161734-8viwvv83ph9d4kfx
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# this program; if not, write to the Free Software Foundation, Inc.,
17
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 
 
19
import locale
19
20
import logging
20
21
import os
21
22
LOG = logging.getLogger(__name__)
26
27
# can be abbreved
27
28
FULL = ["pt_BR", 
28
29
        "zh_CN", "zh_TW"]
 
30
 
 
31
def init_locale():
 
32
    try:
 
33
        locale.setlocale(locale.LC_ALL, "")
 
34
        # we need this for bug #846038, with en_NG setlocale() is fine
 
35
        # but the next getlocale() will crash (fun!)
 
36
        locale.getlocale()
 
37
    except:
 
38
        LOG.exception("setlocale failed, resetting to C")
 
39
        locale.setlocale(locale.LC_ALL, "C")
 
40
 
 
41
 
29
42
def get_languages():
30
43
    """Helper that returns the split up languages"""
31
 
    if not "LANGUAGE" in os.environ:
 
44
    langs = []
 
45
    if "LANGUAGE" in os.environ:
 
46
        langs = os.environ["LANGUAGE"].split(":")
 
47
        for lang in langs[:]:
 
48
            if "_" in lang and not lang in FULL:
 
49
                langs.remove(lang)
 
50
    # fallback
 
51
    if not langs:
32
52
        return [get_language()]
33
 
    langs = os.environ["LANGUAGE"].split(":")
34
 
    for lang in langs[:]:
35
 
        if "_" in lang and not lang in FULL:
36
 
            langs.remove(lang)
37
53
    return langs
38
54
 
39
55
def get_language():
51
67
    if language in FULL:
52
68
        return language
53
69
    return language.split("_")[0]
 
70
 
 
71
def langcode_to_name(langcode):
 
72
    import xml.etree.ElementTree
 
73
    from gettext import dgettext
 
74
    for iso in ["iso_639_3", "iso_639"]:
 
75
        path = os.path.join("/usr/share/xml/iso-codes/", iso+".xml")
 
76
        if os.path.exists(path):
 
77
            root = xml.etree.ElementTree.parse(path)
 
78
            xpath = ".//iso_639_3_entry[@part1_code='%s']" % langcode
 
79
            match = root.find(xpath)
 
80
            if match is not None:
 
81
                return dgettext(iso, match.attrib["name"])
 
82
    return langcode