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.50 $"
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 _log.info('domain not specified, deriving from script name')
260
261 domain = os.path.splitext(os.path.basename(sys.argv[0]))[0]
262 _log.info('text domain is [%s]' % domain)
263
264
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
280 candidates = []
281
282
283 if prefer_local_catalog:
284 _log.debug('preferring local message catalog')
285
286
287
288
289 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..', 'locale'))
290 _log.debug('looking above binary install directory [%s]' % loc_dir)
291 candidates.append(loc_dir)
292
293 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'locale' ))
294 _log.debug('looking in binary install directory [%s]' % loc_dir)
295 candidates.append(loc_dir)
296
297
298 if os.name == 'posix':
299 _log.debug('system is POSIX, looking in standard locations (see Python Manual)')
300
301
302 candidates.append(gettext.bindtextdomain(domain))
303 else:
304 _log.debug('No use looking in standard POSIX locations - not a POSIX system.')
305
306
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], 'locale'))
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
317 if not prefer_local_catalog:
318
319
320
321
322 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..', 'locale'))
323 _log.debug('looking above binary install directory [%s]' % loc_dir)
324 candidates.append(loc_dir)
325
326 loc_dir = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'locale' ))
327 _log.debug('looking in binary install directory [%s]' % loc_dir)
328 candidates.append(loc_dir)
329
330
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
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
354 _log.warning("falling back to NullTranslations() class")
355
356 dummy = gettext.NullTranslations()
357 dummy.install()
358 return True
359
360 _encoding_mismatch_already_logged = False
361
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
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
423
424
425
426 tmp = _('Translate this or i18n will not work properly !')
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654