ed_i18n.py
5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
###############################################################################
# Name: ed_i18n.py #
# Purpose: I18n utilities and services #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2007 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
This file is a module for managing translations and the internationalization of
the program.
METHODS:
- L{GetAvailLocales}: Returns a list of canonical names of available locales
- L{GetLocaleDict}: Returns a dictionary consisting of canonical names for
keys and language ids for values.
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: ed_i18n.py 71189 2012-04-12 00:37:04Z CJP $"
__revision__ = "$Revision: 71189 $"
#--------------------------------------------------------------------------#
# Imports
import os
import wx
import wx.lib.langlistctrl as langlist
import wx.combo
import glob
# Editra Imports
import ed_glob
#----------------------------------------------------------------------------#
# Global Variables
OPT_NO_OP = 0
OPT_DESCRIPT = 1
#----------------------------------------------------------------------------#
#---- Helper Functions used by the classes in this module ----#
def GetAvailLocales():
"""Gets a list of the available locales that have been installed
for the editor. Returning a list of strings that represent the
canonical names of each language.
@return: list of all available local/languages available
"""
avail_loc = list()
loc = glob.glob(os.path.join(ed_glob.CONFIG['LANG_DIR'], "*"))
for path in loc:
the_path = os.path.join(path, "LC_MESSAGES", ed_glob.PROG_NAME + ".mo")
if os.path.exists(the_path):
avail_loc.append(os.path.basename(path))
return avail_loc
def GetLocaleDict(loc_list, opt=OPT_NO_OP):
"""Takes a list of cannonical locale names and by default returns a
dictionary of available language values using the canonical name as
the key. Supplying the Option OPT_DESCRIPT will return a dictionary
of language id's with languages description as the key.
@param loc_list: list of locals
@keyword opt: option for configuring return data
@return: dict of locales mapped to wx.LANGUAGE_*** values
"""
lang_dict = dict()
for lang in [x for x in dir(wx) if x.startswith("LANGUAGE_")]:
langId = getattr(wx, lang)
langOk = False
try:
langOk = wx.Locale.IsAvailable(langId)
except wx.PyAssertionError:
continue
if langOk:
loc_i = wx.Locale.GetLanguageInfo(langId)
if loc_i:
if loc_i.CanonicalName in loc_list:
if opt == OPT_DESCRIPT:
lang_dict[loc_i.Description] = langId
else:
lang_dict[loc_i.CanonicalName] = langId
return lang_dict
def GetLangId(lang_n):
"""Gets the ID of a language from the description string. If the
language cannot be found the function simply returns the default language
@param lang_n: Canonical name of a language
@return: wx.LANGUAGE_*** id of language
"""
if lang_n == "Default":
# No language set, default to English
return wx.LANGUAGE_ENGLISH_US
lang_desc = GetLocaleDict(GetAvailLocales(), OPT_DESCRIPT)
return lang_desc.get(lang_n, wx.LANGUAGE_DEFAULT)
#---- Language List Combo Box----#
class LangListCombo(wx.combo.BitmapComboBox):
"""Combines a langlist and a BitmapComboBox"""
def __init__(self, parent, id_, default=None):
"""Creates a combobox with a list of all translations for the
editor as well as displaying the countries flag next to the item
in the list.
@param default: The default item to show in the combo box
"""
lang_ids = GetLocaleDict(GetAvailLocales()).values()
lang_items = langlist.CreateLanguagesResourceLists(langlist.LC_ONLY, \
lang_ids)
wx.combo.BitmapComboBox.__init__(self, parent, id_,
size=wx.Size(250, 26),
style=wx.CB_READONLY)
for lang_d in lang_items[1]:
bit_m = lang_items[0].GetBitmap(lang_items[1].index(lang_d))
self.Append(lang_d, bit_m)
if default:
self.SetValue(default)
#-----------------------------------------------------------------------------#
if __name__ == '__main__':
APP = wx.PySimpleApp(False)
# Print a list of Canonical names useful for seeing what codes to
# use when naming po files
OUT = list()
for LANG in [x for x in dir(wx) if x.startswith("LANGUAGE")]:
LOC_I = wx.Locale.GetLanguageInfo(getattr(wx, LANG))
if LOC_I:
OUT.append((LOC_I.Description, LOC_I.CanonicalName))
for LANG in sorted(OUT):
print LANG