-
Notifications
You must be signed in to change notification settings - Fork 0
/
localize.py
123 lines (104 loc) · 3.72 KB
/
localize.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
""" Switch between gettext locales on the fly
docs.python.org/2/library/userdit.html#module-UserDict.DictMixin
docs.python.org/2/library/collections.html#collections.MutableMapping
"""
from __future__ import print_function
import sys, gettext
import locale, glob, os.path
import collections
try:
from collections import MutableMapping
except ImportError:
from UserDict import DictMixin as MutableMapping
class Localize(dict, MutableMapping):
""" Switch between gettext locales on the fly"""
def __init__(self, locale_path="", app_name="msgu"):
self.app_name = app_name
if len(locale_path)!=0:
self.locale_path = locale_path
else:
self.locale_path = os.path.join(sys.prefix, 'local', 'share', 'locale')
self._languages = {}
self._language = None
# http://stackoverflow.com/questions/10094335/how-to-bind-a-text-domain-to-a-local-folder-for-gettext-under-gtk3
# https://github.com/dieterv/elib.intl/blob/master/lib/elib/intl/__init__.py
locale.setlocale(locale.LC_ALL, '')
locale.bindtextdomain(self.app_name, self.locale_path)
locale.bind_textdomain_codeset(self.app_name, 'UTF-8')
locale.textdomain(self.app_name)
gettext.bindtextdomain(self.app_name, self.locale_path)
gettext.bind_textdomain_codeset(self.app_name, 'UTF-8')
gettext.textdomain(self.app_name)
#_ = gettext.gettext
print( "Using locale folder: {}".format(self.locale_path) )
self.install()
#self.update(locale_path, app_name)
pass
def __getitem__(self, strLocale):
if type(key) not in [str, unicode]:
raise TypeError("locale must be a string")
if len ( self._languages.get(strLocale, "") )==0:
#No translation by that name
raise KeyError("No locale named %(locale)s" % {'locale': strLocale} )
return self._languages.get(strLocale, "")
def __contains__(self, strLocale):
try:
o = self._languages[strLocale]
except KeyError:
return False
return o is not None
def __repr(self):
return "<%(class)s at %(id)s>" % {'id': id(self), 'class': self.__class__.__name__}
def __iter__(self):
return iter(self._languages)
try:
items = MutableMapping.iteritems
except AttributeError:
#Python 3
items = MutableMapping.items
def keys(self):
return self._languages.keys()
def __setitem__(self, strLocale, translation):
if type(strLocale) not in [str, unicode]:
raise TypeError("locale must be a string")
if len ( self._languages.get(strLocale, "") )==0:
#No translation by that name
raise KeyError("No locale named %(locale)s" % {'locale': strLocale} )
self._languages[strLocale] = translation
def __delitem__(self, strLocale):
if type(strLocale) not in [str, unicode]:
raise TypeError("locale must be a string")
if len ( self._languages.get(strLocale, "") )==0:
#No translation by that name
raise KeyError("No locale named %(locale)s" % {'locale': strLocale} )
del self._languages[strLocale]
def __len__(self):
return len(self._languages)
@property
def language(self):
""" Current language"""
return self._language
@language.setter
def language(self, loc):
""" Change current language"""
try:
self._languages[loc].install()
self._language = loc
except KeyError as why:
pass
def install(self):
gettext.install(self.app_name, self.locale_path, unicode=True)
languagePath = glob.glob( os.path.join(self.locale_path, '*') )
locales = [ os.path.basename(loc) for loc in languagePath ]
for strLocale in locales:
try:
t = gettext.translation(self.app_name, self.locale_path, languages=[strLocale] )
except IOError:
pass
else:
self._languages[strLocale] = t
#print(self.__class__.__name__ + ".install translation", strLocale, self._languages[strLocale])
pass
del languagePath, locales