1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
5 Locales initialisation and gettext wrapper
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
10 Copyright © 2003 - 2008 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
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
35 LOCAL_LOCALE_PATH
= os
.path
.join('build', 'share', 'locale')
38 FALLBACK_LOCALE_CHARSET
= 'iso-8859-1'
40 # Determine "correct" character set
42 # works only in python > 2.3
43 LOCALE_CHARSET
= locale
.getpreferredencoding()
46 LOCALE_CHARSET
= locale
.getdefaultlocale()[1]
49 LOCALE_CHARSET
= sys
.getdefaultencoding()
51 LOCALE_CHARSET
= FALLBACK_LOCALE_CHARSET
52 if LOCALE_CHARSET
in [None, 'ANSI_X3.4-1968']:
53 LOCALE_CHARSET
= FALLBACK_LOCALE_CHARSET
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
):
65 This function coverts something (txt) to string form usable on console.
68 if type(txt
) == type(''):
70 if type(txt
) == type(u
''):
71 return str(CONSOLE_ENCODER(txt
, 'replace')[0])
73 except UnicodeEncodeError:
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
88 if type(txt
) == type(u
''):
90 if type(txt
) == type(''):
91 return unicode(txt
, LOCALE_CHARSET
)
93 except UnicodeEncodeError:
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
114 if type(txt
) == type(u
''):
116 if type(txt
) == type(''):
117 return unicode(txt
, LOCALE_CHARSET
)
119 except UnicodeEncodeError:
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
134 if type(txt
) == type(u
''):
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')
153 def ugettext(self
, message
):
154 result
= gettext
.GNUTranslations
.gettext(self
, message
)
155 if type(result
) == type(''):
156 return unicode(result
, 'utf-8')
160 def gettext(self
, message
):
161 return self
.ugettext(message
)
163 def hgettext(self
, message
):
164 return self
.ugettext(message
)
168 Initialises gettext for wammu domain and installs global function _,
169 which handles translations.
173 if (os
.path
.exists('setup.py') and
174 os
.path
.exists(LOCAL_LOCALE_PATH
) and
176 os
.path
.join('Wammu', '__init__.py')
178 LOCALE_PATH
= LOCAL_LOCALE_PATH
182 print ConsoleStrConv(_('Automatically switched to local locales.'))
186 Use locales from current build dir.
189 LOCALE_PATH
= LOCAL_LOCALE_PATH
192 def ngettext(msgid1
, msgid2
, n
):
198 def ugettext(message
):
201 def lgettext(message
):
204 def hgettext(message
):
208 global LOCALE_PATH
, ngettext
, ugettext
, lgettext
, hgettext
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
219 # No translation found for current locale
220 __builtin__
.__dict
__['_'] = ugettext