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}/locale/"
34 - below "<directory of binary of your app>/../locale/"
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
52
53 __version__ = "$Revision: 1.48 $"
54 __author__ = "H. Herb <hherb@gnumed.net>, I. Haywood <i.haywood@ugrad.unimelb.edu.au>, K. Hilbert <Karsten.Hilbert@gmx.net>"
55 __license__ = "GPL (details at http://www.gnu.org)"
56
57
58
59 import sys, os.path, os, re as regex, locale, gettext, logging, codecs
60
61
62 _log = logging.getLogger('gm.i18n')
63 _log.info(__version__)
64
65 system_locale = ''
66 system_locale_level = {}
67
68
69 _translate_original = lambda x:x
70
71
72
73
74
75
76 __orig_tag__ = u'Translate this or i18n will not work properly !'
77
78
79
80
81
82
83
84
85
86
87
88
108
110 _setlocale_categories = {}
111 for category in 'LC_ALL LC_CTYPE LC_COLLATE LC_TIME LC_MONETARY LC_MESSAGES LC_NUMERIC'.split():
112 try:
113 _setlocale_categories[category] = getattr(locale, category)
114 except:
115 _log.warning('this OS does not have locale.%s', category)
116
117 _getlocale_categories = {}
118 for category in 'LC_CTYPE LC_COLLATE LC_TIME LC_MONETARY LC_MESSAGES LC_NUMERIC'.split():
119 try:
120 _getlocale_categories[category] = getattr(locale, category)
121 except:
122 pass
123
124 if message is not None:
125 _log.debug(message)
126
127 _log.debug('current locale settings:')
128 _log.debug('locale.get_locale(): %s' % str(locale.getlocale()))
129 for category in _getlocale_categories.keys():
130 _log.debug('locale.get_locale(%s): %s' % (category, locale.getlocale(_getlocale_categories[category])))
131
132 for category in _setlocale_categories.keys():
133 _log.debug('(locale.set_locale(%s): %s)' % (category, locale.setlocale(_setlocale_categories[category])))
134
135 try:
136 _log.debug('locale.getdefaultlocale() - default (user) locale: %s' % str(locale.getdefaultlocale()))
137 except ValueError:
138 _log.exception('the OS locale setup seems faulty')
139
140 _log.debug('encoding sanity check (also check "locale.nl_langinfo(CODESET)" below):')
141 pref_loc_enc = locale.getpreferredencoding(do_setlocale=False)
142 loc_enc = locale.getlocale()[1]
143 py_str_enc = sys.getdefaultencoding()
144 sys_fs_enc = sys.getfilesystemencoding()
145 _log.debug('sys.getdefaultencoding(): [%s]' % py_str_enc)
146 _log.debug('locale.getpreferredencoding(): [%s]' % pref_loc_enc)
147 _log.debug('locale.getlocale()[1]: [%s]' % loc_enc)
148 _log.debug('sys.getfilesystemencoding(): [%s]' % sys_fs_enc)
149 if loc_enc is not None:
150 loc_enc = loc_enc.upper()
151 loc_enc_compare = loc_enc.replace(u'-', u'')
152 else:
153 loc_enc_compare = loc_enc
154 if pref_loc_enc.upper().replace(u'-', u'') != loc_enc_compare:
155 _log.warning('encoding suggested by locale (%s) does not match encoding currently set in locale (%s)' % (pref_loc_enc, loc_enc))
156 _log.warning('this might lead to encoding errors')
157 for enc in [pref_loc_enc, loc_enc, py_str_enc, sys_fs_enc]:
158 if enc is not None:
159 try:
160 codecs.lookup(enc)
161 _log.debug('<codecs> module CAN handle encoding [%s]' % enc)
162 except LookupError:
163 _log.warning('<codecs> module can NOT handle encoding [%s]' % enc)
164 _log.debug('on Linux you can determine a likely candidate for the encoding by running "locale charmap"')
165
166 _log.debug('locale related environment variables (${LANG} is typically used):')
167 for var in 'LANGUAGE LC_ALL LC_CTYPE LANG'.split():
168 try:
169 _log.debug('${%s}=%s' % (var, os.environ[var]))
170 except KeyError:
171 _log.debug('${%s} not set' % (var))
172
173 _log.debug('database of locale conventions:')
174 data = locale.localeconv()
175 for key in data.keys():
176 if loc_enc is None:
177 _log.debug(u'locale.localeconv(%s): %s', key, data[key])
178 else:
179 try:
180 _log.debug(u'locale.localeconv(%s): %s', key, unicode(data[key]))
181 except UnicodeDecodeError:
182 _log.debug(u'locale.localeconv(%s): %s', key, unicode(data[key], loc_enc))
183 _nl_langinfo_categories = {}
184 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():
185 try:
186 _nl_langinfo_categories[category] = getattr(locale, category)
187 except:
188 _log.warning('this OS does not support nl_langinfo category locale.%s' % category)
189 try:
190 for category in _nl_langinfo_categories.keys():
191 if loc_enc is None:
192 _log.debug('locale.nl_langinfo(%s): %s' % (category, locale.nl_langinfo(_nl_langinfo_categories[category])))
193 else:
194 try:
195 _log.debug(u'locale.nl_langinfo(%s): %s', category, unicode(locale.nl_langinfo(_nl_langinfo_categories[category])))
196 except UnicodeDecodeError:
197 _log.debug(u'locale.nl_langinfo(%s): %s', category, unicode(locale.nl_langinfo(_nl_langinfo_categories[category]), loc_enc))
198 except:
199 _log.exception('this OS does not support nl_langinfo')
200
202 """This wraps _().
203
204 It protects against translation errors such as different number of %s.
205 """
206 translation = _translate_original(term)
207
208 if translation.count(u'%s') == term.count(u'%s'):
209 return translation
210
211 _log.error('mismatch in translation of [%s]' % term)
212 return term
213
214
215
217 """Get system locale from environment."""
218 global system_locale
219
220
221 __log_locale_settings('unmodified startup locale settings (should be [C])')
222
223
224 loc, enc = None, None
225 try:
226
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
240 __log_locale_settings('locale settings after activating user-default locale')
241
242
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
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
258 if domain is None:
259
260 domain = os.path.splitext(os.path.basename(sys.argv[0]))[0]
261 _log.info('text domain is [%s]' % domain)
262
263
264 _log.debug('searching message catalog file for system locale [%s]' % system_locale)
265 for env_var in ['LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG']:
266 tmp = os.getenv(env_var)
267 if env_var is None:
268 _log.debug('${%s} not set' % env_var)
269 else:
270 _log.debug('${%s} = [%s]' % (env_var, tmp))
271
272 if language is not None:
273 _log.info('explicit setting of ${LANG} requested: [%s]' % language)
274 _log.info('this will override the system locale language setting')
275 os.environ['LANG'] = language
276
277
278 candidates = []
279
280 if prefer_local_catalog:
281 _log.debug('preferring local message catalog')
282
283
284
285
286 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..', 'locale'))
287 _log.debug('looking above binary install directory [%s]' % loc_dir)
288 candidates.append(loc_dir)
289
290 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'locale' ))
291 _log.debug('looking in binary install directory [%s]' % loc_dir)
292 candidates.append(loc_dir)
293
294 if os.name == 'posix':
295 _log.debug('system is POSIX, looking in standard locations (see Python Manual)')
296
297
298 candidates.append(gettext.bindtextdomain(domain))
299 else:
300 _log.debug('No use looking in standard POSIX locations - not a POSIX system.')
301
302 env_key = "%s_DIR" % os.path.splitext(os.path.basename(sys.argv[0]))[0].upper()
303 _log.debug('looking at ${%s}' % env_key)
304 if os.environ.has_key(env_key):
305 loc_dir = os.path.abspath(os.path.join(os.environ[env_key], 'locale'))
306 _log.debug('${%s} = "%s" -> [%s]' % (env_key, os.environ[env_key], loc_dir))
307 candidates.append(loc_dir)
308 else:
309 _log.info("${%s} not set" % env_key)
310
311 if not prefer_local_catalog:
312
313
314
315
316 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..', 'locale'))
317 _log.debug('looking above binary install directory [%s]' % loc_dir)
318 candidates.append(loc_dir)
319
320 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'locale' ))
321 _log.debug('looking in binary install directory [%s]' % loc_dir)
322 candidates.append(loc_dir)
323
324
325 for candidate in candidates:
326 _log.debug('trying [%s](/%s/LC_MESSAGES/%s.mo)' % (candidate, system_locale, domain))
327 if not os.path.exists(candidate):
328 continue
329 try:
330 gettext.install(domain, candidate, unicode=1)
331 except:
332 _log.exception('installing text domain [%s] failed from [%s]' % (domain, candidate))
333 continue
334 global _
335
336 if _(__orig_tag__) == __orig_tag__:
337 _log.debug('does not translate: [%s] => [%s]', __orig_tag__, _(__orig_tag__))
338 continue
339 else:
340 _log.debug('found msg catalog: [%s] => [%s]', __orig_tag__, _(__orig_tag__))
341 import __builtin__
342 global _translate_original
343 _translate_original = __builtin__._
344 __builtin__._ = _translate_protected
345 return True
346
347
348 _log.warning("falling back to NullTranslations() class")
349
350 dummy = gettext.NullTranslations()
351 dummy.install()
352 return True
353
354 _encoding_mismatch_already_logged = False
355
357 """Try to get a sane encoding.
358
359 On MaxOSX locale.setlocale(locale.LC_ALL, '') does not
360 have the desired effect, so that locale.getlocale()[1]
361 still returns None. So in that case try to fallback to
362 locale.getpreferredencoding().
363
364 <sys.getdefaultencoding()>
365 - what Python itself uses to convert string <-> unicode
366 when no other encoding was specified
367 - ascii by default
368 - can be set in site.py and sitecustomize.py
369 <locale.getpreferredencoding()>
370 - what the current locale would *recommend* using
371 as the encoding for text conversion
372 <locale.getlocale()[1]>
373 - what the current locale is *actually* using
374 as the encoding for text conversion
375 """
376 enc = sys.getdefaultencoding()
377 if enc != 'ascii':
378 return enc
379 enc = locale.getlocale()[1]
380 if enc is not None:
381 return enc
382 global _encoding_mismatch_already_logged
383 if not _encoding_mismatch_already_logged:
384 _log.debug('*actual* encoding of locale is None, using encoding *recommended* by locale')
385 _encoding_mismatch_already_logged = True
386 return locale.getpreferredencoding(do_setlocale=False)
387
388
389
390 if __name__ == "__main__":
391
392 if len(sys.argv) > 1 and sys.argv[1] == u'test':
393
394 logging.basicConfig(level = logging.DEBUG)
395
396 print "======================================================================"
397 print "GNUmed i18n"
398 print ""
399 print "authors:", __author__
400 print "license:", __license__, "; version:", __version__
401 print "======================================================================"
402 activate_locale()
403 print "system locale: ", system_locale, "; levels:", system_locale_level
404 print "likely encoding:", get_encoding()
405 install_domain()
406
407
408
409
410
411 tmp = _('Translate this or i18n will not work properly !')
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633