1
2 """
3
4 @author: Fabio Erculiani <lxnay@sabayonlinux.org>
5 @contact: lxnay@sabayonlinux.org
6 @copyright: Fabio Erculiani
7 @license: GPL-2
8
9 B{Entropy Framework internationalization module}
10
11 This module contains Entropy Framework i18n functions and
12 variables.
13
14 """
15
16 import sys
17 import os
18 _LOCALE = None
19 _LOCALE_FULL = os.getenv('LC_ALL')
20 if _LOCALE_FULL == None:
21 _LOCALE_FULL = os.getenv('LANG')
22 if _LOCALE_FULL == None:
23 _LOCALE_FULL = os.getenv('LANGUAGE')
24
25 if _LOCALE_FULL:
26 _LOCALE = _LOCALE_FULL.split('.')[0]
27 _LOCALE = _LOCALE.split('_')[0]
28 _LOCALE = _LOCALE.lower()
29
30 try:
31 import gettext
32 localedir = "/usr/share/locale"
33
34 envdir = os.getenv('TEXTDOMAINDIR')
35 if envdir is not None:
36 localedir = envdir
37 kwargs = {"localedir": localedir,}
38 if sys.hexversion < 0x3000000:
39 kwargs['unicode'] = True
40 gettext.install('entropy', **kwargs)
41
42 _ = _
43
44 except (ImportError, OSError,):
46 """
47 Fallback in case gettext is not available, same syntax
48 for the gettext provided function.
49
50 @param raw_string: raw untranslated string
51 @type raw_string: string
52 @return: translated string using environment locale
53 setting (LC_ALL, LANG or LANGUAGE)
54 @rtype: string
55 """
56 return raw_string
57
59 """
60 Change gettext language on the fly.
61
62 @param lang: new language string (see `locale -a` for a list of
63 supported ones)
64 @type lang: string
65 """
66 global _
67
68 for var in ("LANGUAGE", "LC_ALL", "LANG",):
69 os.environ[var] = lang
70
71
72 old_ = _
73 del _
74 kwargs = {"localedir": localedir,}
75 if sys.hexversion < 0x3000000:
76 kwargs['unicode'] = True
77 gettext.install('entropy', **kwargs)
78 _ = _
79
80 for module in list(sys.modules.values()):
81 if not hasattr(module, "__dict__"):
82 continue
83 t_func = module.__dict__.get("_")
84 if t_func is not old_:
85 continue
86 module._ = _
87