Package Gnumed :: Package pycommon :: Module gmI18N
[frames] | no frames]

Source Code for Module Gnumed.pycommon.gmI18N

  1  """GNUmed client internationalization/localization. 
  2   
  3  All i18n/l10n issues should be handled through this modules. 
  4   
  5  Theory of operation: 
  6   
  7  To activate proper locale settings and translation services you need to 
  8   
  9  - import this module 
 10  - call activate_locale() 
 11  - call install_domain() 
 12   
 13  The translating method gettext.gettext() will then be 
 14  installed into the global (!) namespace as _(). Your own 
 15  modules thus need not do _anything_ (not even import gmI18N) 
 16  to have _() available to them for translating strings. You 
 17  need to make sure, however, that gmI18N is imported in your 
 18  main module before any of the modules using it. In order to 
 19  resolve circular references involving modules that 
 20  absolutely _have_ to be imported before this module you can 
 21  explicitly import gmI18N into them at the very beginning. 
 22   
 23  The text domain (i.e. the name of the message catalog file) 
 24  is derived from the name of the main executing script unless 
 25  explicitly passed to install_domain(). The language you 
 26  want to translate to is derived from environment variables 
 27  by the locale system unless explicitly passed to 
 28  install_domain(). 
 29   
 30  This module searches for message catalog files in 3 main locations: 
 31   
 32   - standard POSIX places (/usr/share/locale/ ...) 
 33   - below "${YOURAPPNAME_DIR}/po/" 
 34   - below "<directory of binary of your app>/../po/" 
 35   
 36  For DOS/Windows I don't know of standard places so probably 
 37  only the last option will work. I don't know a thing about 
 38  classic Mac behaviour. New Macs are POSIX, of course. 
 39   
 40  It will then try to install candidates and *verify* whether 
 41  the translation works by checking for the translation of a 
 42  tag within itself (this is similar to the self-compiling 
 43  compiler inserting a backdoor into its self-compiled 
 44  copies). 
 45   
 46  If none of this works it will fall back to making _() a noop. 
 47   
 48  @copyright: authors 
 49  """ 
 50  #=========================================================================== 
 51  __version__ = "$Revision: 1.50 $" 
 52  __author__ = "H. Herb <hherb@gnumed.net>, I. Haywood <i.haywood@ugrad.unimelb.edu.au>, K. Hilbert <Karsten.Hilbert@gmx.net>" 
 53  __license__ = "GPL (details at http://www.gnu.org)" 
 54   
 55   
 56  # stdlib 
 57  import sys, os.path, os, re as regex, locale, gettext, logging, codecs 
 58   
 59   
 60  _log = logging.getLogger('gm.i18n') 
 61  _log.info(__version__) 
 62   
 63  system_locale = '' 
 64  system_locale_level = {} 
 65   
 66   
 67  _translate_original = lambda x:x 
 68   
 69  # ********************************************************** 
 70  # == do not remove this line =============================== 
 71  # it is needed to check for successful installation of 
 72  # the desired message catalog 
 73  # ********************************************************** 
 74  __orig_tag__ = u'Translate this or i18n will not work properly !' 
 75  # ********************************************************** 
 76  # ********************************************************** 
 77   
 78  # Q: I can't use non-ascii characters in labels and menus. 
 79  # A: This can happen if your Python's sytem encoding is ascii and 
 80  #    wxPython is non-unicode. Edit/create the file sitecustomize.py 
 81  #    (should be somewhere in your PYTHONPATH), and put these magic lines: 
 82  # 
 83  #       import sys 
 84  #       sys.setdefaultencoding('iso8859-1') # replace with encoding you want to be the default one 
 85   
 86  #=========================================================================== 
87 -def __split_locale_into_levels():
88 """Split locale into language, country and variant parts. 89 90 - we have observed the following formats in the wild: 91 - de_DE@euro 92 - ec_CA.UTF-8 93 - en_US:en 94 - German_Germany.1252 95 """ 96 _log.debug('splitting canonical locale [%s] into levels', system_locale) 97 98 global system_locale_level 99 system_locale_level['full'] = system_locale 100 # trim '@<variant>' part 101 system_locale_level['country'] = regex.split('@|:|\.', system_locale, 1)[0] 102 # trim '_<COUNTRY>@<variant>' part 103 system_locale_level['language'] = system_locale.split('_', 1)[0] 104 105 _log.debug('system locale levels: %s', system_locale_level)
106 #---------------------------------------------------------------------------
107 -def __log_locale_settings(message=None):
108 _setlocale_categories = {} 109 for category in 'LC_ALL LC_CTYPE LC_COLLATE LC_TIME LC_MONETARY LC_MESSAGES LC_NUMERIC'.split(): 110 try: 111 _setlocale_categories[category] = getattr(locale, category) 112 except: 113 _log.warning('this OS does not have locale.%s', category) 114 115 _getlocale_categories = {} 116 for category in 'LC_CTYPE LC_COLLATE LC_TIME LC_MONETARY LC_MESSAGES LC_NUMERIC'.split(): 117 try: 118 _getlocale_categories[category] = getattr(locale, category) 119 except: 120 pass 121 122 if message is not None: 123 _log.debug(message) 124 125 _log.debug('current locale settings:') 126 _log.debug('locale.get_locale(): %s' % str(locale.getlocale())) 127 for category in _getlocale_categories.keys(): 128 _log.debug('locale.get_locale(%s): %s' % (category, locale.getlocale(_getlocale_categories[category]))) 129 130 for category in _setlocale_categories.keys(): 131 _log.debug('(locale.set_locale(%s): %s)' % (category, locale.setlocale(_setlocale_categories[category]))) 132 133 try: 134 _log.debug('locale.getdefaultlocale() - default (user) locale: %s' % str(locale.getdefaultlocale())) 135 except ValueError: 136 _log.exception('the OS locale setup seems faulty') 137 138 _log.debug('encoding sanity check (also check "locale.nl_langinfo(CODESET)" below):') 139 pref_loc_enc = locale.getpreferredencoding(do_setlocale=False) 140 loc_enc = locale.getlocale()[1] 141 py_str_enc = sys.getdefaultencoding() 142 sys_fs_enc = sys.getfilesystemencoding() 143 _log.debug('sys.getdefaultencoding(): [%s]' % py_str_enc) 144 _log.debug('locale.getpreferredencoding(): [%s]' % pref_loc_enc) 145 _log.debug('locale.getlocale()[1]: [%s]' % loc_enc) 146 _log.debug('sys.getfilesystemencoding(): [%s]' % sys_fs_enc) 147 if loc_enc is not None: 148 loc_enc = loc_enc.upper() 149 loc_enc_compare = loc_enc.replace(u'-', u'') 150 else: 151 loc_enc_compare = loc_enc 152 if pref_loc_enc.upper().replace(u'-', u'') != loc_enc_compare: 153 _log.warning('encoding suggested by locale (%s) does not match encoding currently set in locale (%s)' % (pref_loc_enc, loc_enc)) 154 _log.warning('this might lead to encoding errors') 155 for enc in [pref_loc_enc, loc_enc, py_str_enc, sys_fs_enc]: 156 if enc is not None: 157 try: 158 codecs.lookup(enc) 159 _log.debug('<codecs> module CAN handle encoding [%s]' % enc) 160 except LookupError: 161 _log.warning('<codecs> module can NOT handle encoding [%s]' % enc) 162 _log.debug('on Linux you can determine a likely candidate for the encoding by running "locale charmap"') 163 164 _log.debug('locale related environment variables (${LANG} is typically used):') 165 for var in 'LANGUAGE LC_ALL LC_CTYPE LANG'.split(): 166 try: 167 _log.debug('${%s}=%s' % (var, os.environ[var])) 168 except KeyError: 169 _log.debug('${%s} not set' % (var)) 170 171 _log.debug('database of locale conventions:') 172 data = locale.localeconv() 173 for key in data.keys(): 174 if loc_enc is None: 175 _log.debug(u'locale.localeconv(%s): %s', key, data[key]) 176 else: 177 try: 178 _log.debug(u'locale.localeconv(%s): %s', key, unicode(data[key])) 179 except UnicodeDecodeError: 180 _log.debug(u'locale.localeconv(%s): %s', key, unicode(data[key], loc_enc)) 181 _nl_langinfo_categories = {} 182 for category in 'CODESET D_T_FMT D_FMT T_FMT T_FMT_AMPM RADIXCHAR THOUSEP YESEXPR NOEXPR CRNCYSTR ERA ERA_D_T_FMT ERA_D_FMT ALT_DIGITS'.split(): 183 try: 184 _nl_langinfo_categories[category] = getattr(locale, category) 185 except: 186 _log.warning('this OS does not support nl_langinfo category locale.%s' % category) 187 try: 188 for category in _nl_langinfo_categories.keys(): 189 if loc_enc is None: 190 _log.debug('locale.nl_langinfo(%s): %s' % (category, locale.nl_langinfo(_nl_langinfo_categories[category]))) 191 else: 192 try: 193 _log.debug(u'locale.nl_langinfo(%s): %s', category, unicode(locale.nl_langinfo(_nl_langinfo_categories[category]))) 194 except UnicodeDecodeError: 195 _log.debug(u'locale.nl_langinfo(%s): %s', category, unicode(locale.nl_langinfo(_nl_langinfo_categories[category]), loc_enc)) 196 except: 197 _log.exception('this OS does not support nl_langinfo')
198 #---------------------------------------------------------------------------
199 -def _translate_protected(term):
200 """This wraps _(). 201 202 It protects against translation errors such as a different number of "%s". 203 """ 204 translation = _translate_original(term) 205 206 if translation.count(u'%s') == term.count(u'%s'): 207 return translation 208 209 _log.error('count(%s) mismatch, returning untranslated string') 210 _log.error('original : %s', term) 211 _log.error('translation: %s', translation) 212 return term
213 #--------------------------------------------------------------------------- 214 # external API 215 #---------------------------------------------------------------------------
216 -def activate_locale():
217 """Get system locale from environment.""" 218 global system_locale 219 220 # logging state of affairs 221 __log_locale_settings('unmodified startup locale settings (should be [C])') 222 223 # activate user-preferred locale 224 loc, enc = None, None 225 try: 226 # check whether already set 227 loc, loc_enc = locale.getlocale() 228 if loc is None: 229 loc = locale.setlocale(locale.LC_ALL, '') 230 _log.debug("activating user-default locale with <locale.setlocale(locale.LC_ALL, '')> returns: [%s]" % loc) 231 else: 232 _log.info('user-default locale already activated') 233 loc, loc_enc = locale.getlocale() 234 except AttributeError: 235 _log.exception('Windows does not support locale.LC_ALL') 236 except: 237 _log.exception('error activating user-default locale') 238 239 # logging state of affairs 240 __log_locale_settings('locale settings after activating user-default locale') 241 242 # did we find any locale setting ? assume en_EN if not 243 if loc in [None, 'C']: 244 _log.error('the current system locale is still [None] or [C], assuming [en_EN]') 245 system_locale = "en_EN" 246 else: 247 system_locale = loc 248 249 # generate system locale levels 250 __split_locale_into_levels() 251 252 return True
253 #---------------------------------------------------------------------------
254 -def install_domain(domain=None, language=None, prefer_local_catalog=False):
255 """Install a text domain suitable for the main script.""" 256 257 # text domain directly specified ? 258 if domain is None: 259 _log.info('domain not specified, deriving from script name') 260 # get text domain from name of script 261 domain = os.path.splitext(os.path.basename(sys.argv[0]))[0] 262 _log.info('text domain is [%s]' % domain) 263 264 # http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html 265 _log.debug('searching message catalog file for system locale [%s]' % system_locale) 266 267 for env_var in ['LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG']: 268 tmp = os.getenv(env_var) 269 if env_var is None: 270 _log.debug('${%s} not set' % env_var) 271 else: 272 _log.debug('${%s} = [%s]' % (env_var, tmp)) 273 274 if language is not None: 275 _log.info('explicit setting of ${LANG} requested: [%s]' % language) 276 _log.info('this will override the system locale language setting') 277 os.environ['LANG'] = language 278 279 # search for message catalog 280 candidates = [] 281 282 # - locally 283 if prefer_local_catalog: 284 _log.debug('preferring local message catalog') 285 # - one level above path to binary 286 # last resort for inferior operating systems such as DOS/Windows 287 # strip one directory level 288 # this is a rather neat trick :-) 289 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..', 'po')) 290 _log.debug('looking above binary install directory [%s]' % loc_dir) 291 candidates.append(loc_dir) 292 # - in path to binary 293 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'po')) 294 _log.debug('looking in binary install directory [%s]' % loc_dir) 295 candidates.append(loc_dir) 296 297 # - standard places 298 if os.name == 'posix': 299 _log.debug('system is POSIX, looking in standard locations (see Python Manual)') 300 # if this is reported to segfault/fail/except on some 301 # systems we may have to assume "sys.prefix/share/locale/" 302 candidates.append(gettext.bindtextdomain(domain)) 303 else: 304 _log.debug('No use looking in standard POSIX locations - not a POSIX system.') 305 306 # - $(<script-name>_DIR)/ 307 env_key = "%s_DIR" % os.path.splitext(os.path.basename(sys.argv[0]))[0].upper() 308 _log.debug('looking at ${%s}' % env_key) 309 if os.environ.has_key(env_key): 310 loc_dir = os.path.abspath(os.path.join(os.environ[env_key], 'po')) 311 _log.debug('${%s} = "%s" -> [%s]' % (env_key, os.environ[env_key], loc_dir)) 312 candidates.append(loc_dir) 313 else: 314 _log.info("${%s} not set" % env_key) 315 316 # - locally 317 if not prefer_local_catalog: 318 # - one level above path to binary 319 # last resort for inferior operating systems such as DOS/Windows 320 # strip one directory level 321 # this is a rather neat trick :-) 322 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..', 'po')) 323 _log.debug('looking above binary install directory [%s]' % loc_dir) 324 candidates.append(loc_dir) 325 # - in path to binary 326 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'po' )) 327 _log.debug('looking in binary install directory [%s]' % loc_dir) 328 candidates.append(loc_dir) 329 330 # now try to actually install it 331 for candidate in candidates: 332 _log.debug('trying [%s](/%s/LC_MESSAGES/%s.mo)', candidate, system_locale, domain) 333 if not os.path.exists(candidate): 334 continue 335 try: 336 gettext.install(domain, candidate, unicode=1) 337 except: 338 _log.exception('installing text domain [%s] failed from [%s]', domain, candidate) 339 continue 340 global _ 341 # does it translate ? 342 if _(__orig_tag__) == __orig_tag__: 343 _log.debug('does not translate: [%s] => [%s]', __orig_tag__, _(__orig_tag__)) 344 continue 345 else: 346 _log.debug('found msg catalog: [%s] => [%s]', __orig_tag__, _(__orig_tag__)) 347 import __builtin__ 348 global _translate_original 349 _translate_original = __builtin__._ 350 __builtin__._ = _translate_protected 351 return True 352 353 # 5) install a dummy translation class 354 _log.warning("falling back to NullTranslations() class") 355 # this shouldn't fail 356 dummy = gettext.NullTranslations() 357 dummy.install() 358 return True
359 #=========================================================================== 360 _encoding_mismatch_already_logged = False 361
362 -def get_encoding():
363 """Try to get a sane encoding. 364 365 On MaxOSX locale.setlocale(locale.LC_ALL, '') does not 366 have the desired effect, so that locale.getlocale()[1] 367 still returns None. So in that case try to fallback to 368 locale.getpreferredencoding(). 369 370 <sys.getdefaultencoding()> 371 - what Python itself uses to convert string <-> unicode 372 when no other encoding was specified 373 - ascii by default 374 - can be set in site.py and sitecustomize.py 375 <locale.getpreferredencoding()> 376 - what the current locale would *recommend* using 377 as the encoding for text conversion 378 <locale.getlocale()[1]> 379 - what the current locale is *actually* using 380 as the encoding for text conversion 381 """ 382 enc = sys.getdefaultencoding() 383 if enc != 'ascii': 384 return enc 385 enc = locale.getlocale()[1] 386 if enc is not None: 387 return enc 388 global _encoding_mismatch_already_logged 389 if not _encoding_mismatch_already_logged: 390 _log.debug('*actual* encoding of locale is None, using encoding *recommended* by locale') 391 _encoding_mismatch_already_logged = True 392 return locale.getpreferredencoding(do_setlocale=False)
393 #=========================================================================== 394 # Main 395 #--------------------------------------------------------------------------- 396 if __name__ == "__main__": 397 398 if len(sys.argv) == 1: 399 sys.exit() 400 401 if sys.argv[1] != u'test': 402 sys.exit() 403 404 logging.basicConfig(level = logging.DEBUG) 405 406 print "======================================================================" 407 print "GNUmed i18n" 408 print "" 409 print "authors:", __author__ 410 print "license:", __license__, "; version:", __version__ 411 print "======================================================================" 412 413 activate_locale() 414 print "system locale: ", system_locale, "; levels:", system_locale_level 415 print "likely encoding:", get_encoding() 416 417 if len(sys.argv) > 1: 418 install_domain(domain = sys.argv[2]) 419 else: 420 install_domain() 421 # ******************************************************** 422 # == do not remove this line ============================= 423 # it is needed to check for successful installation of 424 # the desired message catalog 425 # ******************************************************** 426 tmp = _('Translate this or i18n will not work properly !') 427 # ******************************************************** 428 # ******************************************************** 429 430 #===================================================================== 431