Translated using Weblate.
[wammu.git] / Wammu / Locales.py
blobaa02569cbc2ec1c5b60a0bb23b782495ced971d7
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Locales initialisation and gettext wrapper
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright © 2003 - 2010 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 import os
27 import gettext
28 import locale
29 import codecs
30 import __builtin__
31 import wx
32 import sys
35 LOCAL_LOCALE_PATH = os.path.join('build', 'share', 'locale')
36 LOCALE_PATH = None
38 FALLBACK_LOCALE_CHARSET = 'iso-8859-1'
40 # Determine "correct" character set
41 try:
42 # works only in python > 2.3
43 LOCALE_CHARSET = locale.getpreferredencoding()
44 except:
45 try:
46 LOCALE_CHARSET = locale.getdefaultlocale()[1]
47 except:
48 try:
49 LOCALE_CHARSET = sys.getdefaultencoding()
50 except:
51 LOCALE_CHARSET = FALLBACK_LOCALE_CHARSET
52 if LOCALE_CHARSET in [None, 'ANSI_X3.4-1968']:
53 LOCALE_CHARSET = FALLBACK_LOCALE_CHARSET
55 try:
56 CONSOLE_CHARSET = sys.stdout.encoding
57 except AttributeError:
58 CONSOLE_CHARSET = None
59 if CONSOLE_CHARSET is None:
60 CONSOLE_CHARSET = LOCALE_CHARSET
61 CONSOLE_ENCODER = codecs.getencoder(CONSOLE_CHARSET)
63 def ConsoleStrConv(txt):
64 """
65 This function coverts something (txt) to string form usable on console.
66 """
67 try:
68 if type(txt) == type(''):
69 return txt
70 if type(txt) == type(u''):
71 return str(CONSOLE_ENCODER(txt, 'replace')[0])
72 return str(txt)
73 except UnicodeEncodeError:
74 return '???'
76 def StrConv(txt):
77 """
78 This function coverts something (txt) to string form usable by wxPython. There
79 is problem that in default configuration in most distros (maybe all) default
80 encoding for unicode objects is ascii. This leads to exception when converting
81 something different than ascii. And this exception is not catched inside
82 wxPython and leads to segfault.
84 So if wxPython supports unicode, we give it unicode, otherwise locale
85 dependant text.
86 """
87 try:
88 if type(txt) == type(u''):
89 return txt
90 if type(txt) == type(''):
91 return unicode(txt, LOCALE_CHARSET)
92 return str(txt)
93 except UnicodeEncodeError:
94 return '???'
96 # detect html charset
97 HTML_CHARSET = LOCALE_CHARSET
99 # prepare html encoder
100 HTML_ENCODER = codecs.getencoder(HTML_CHARSET)
102 def HtmlStrConv(txt):
104 This function coverts something (txt) to string form usable by wxPython
105 html widget. There is problem that in default configuration in most distros
106 (maybe all) default encoding for unicode objects is ascii. This leads to
107 exception when converting something different than ascii. And this
108 exception is not catched inside wxPython and leads to segfault.
110 So if wxPython supports unicode, we give it unicode, otherwise locale
111 dependant text.
113 try:
114 if type(txt) == type(u''):
115 return txt
116 if type(txt) == type(''):
117 return unicode(txt, LOCALE_CHARSET)
118 return str(txt)
119 except UnicodeEncodeError:
120 return '???'
122 def UnicodeConv(txt):
124 This function coverts something (txt) to string form usable by wxPython. There
125 is problem that in default configuration in most distros (maybe all) default
126 encoding for unicode objects is ascii. This leads to exception when converting
127 something different than ascii. And this exception is not catched inside
128 wxPython and leads to segfault.
130 So if wxPython supports unicode, we give it unicode, otherwise locale
131 dependant text.
133 try:
134 if type(txt) == type(u''):
135 return txt
136 if type(txt) == type(''):
137 return unicode(txt, LOCALE_CHARSET)
138 return unicode(str(txt), LOCALE_CHARSET)
139 except UnicodeEncodeError:
140 return unicode('???')
142 class WammuTranslations(gettext.GNUTranslations):
144 Wrapper for gettext returning always "correct" charset.
146 def ngettext(self, msgid1, msgid2, n):
147 result = gettext.GNUTranslations.ngettext(self, msgid1, msgid2, n)
148 if type(result) == type(''):
149 return unicode(result, 'utf-8')
150 else:
151 return result
153 def ugettext(self, message):
154 result = gettext.GNUTranslations.gettext(self, message)
155 if type(result) == type(''):
156 return unicode(result, 'utf-8')
157 else:
158 return result
160 def gettext(self, message):
161 return self.ugettext(message)
163 def hgettext(self, message):
164 return self.ugettext(message)
166 def Init():
168 Initialises gettext for wammu domain and installs global function _,
169 which handles translations.
171 global LOCALE_PATH
172 switch = False
173 if (os.path.exists('setup.py') and
174 os.path.exists(LOCAL_LOCALE_PATH) and
175 os.path.exists(
176 os.path.join('Wammu', '__init__.py')
178 LOCALE_PATH = LOCAL_LOCALE_PATH
179 switch = True
180 Install()
181 if switch:
182 print ConsoleStrConv(_('Automatically switched to local locales.'))
184 def UseLocal():
186 Use locales from current build dir.
188 global LOCALE_PATH
189 LOCALE_PATH = LOCAL_LOCALE_PATH
190 Install()
192 def ngettext(msgid1, msgid2, n):
193 if n == 1:
194 return msgid1
195 else:
196 return msgid2
198 def ugettext(message):
199 return message
201 def lgettext(message):
202 return message
204 def hgettext(message):
205 return message
207 def Install():
208 global LOCALE_PATH, ngettext, ugettext, lgettext, hgettext
209 try:
210 trans = gettext.translation('wammu',
211 class_ = WammuTranslations,
212 localedir = LOCALE_PATH)
213 __builtin__.__dict__['_'] = trans.gettext
214 ngettext = trans.ngettext
215 ugettext = trans.ugettext
216 lgettext = trans.lgettext
217 hgettext = trans.hgettext
218 except IOError:
219 # No translation found for current locale
220 __builtin__.__dict__['_'] = ugettext
221 pass