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 gettext.install('entropy', localedir = envdir, unicode = True)
38
39 _ = _
40
41 except (ImportError, OSError,):
43 """
44 Fallback in case gettext is not available, same syntax
45 for the gettext provided function.
46
47 @param raw_string: raw untranslated string
48 @type raw_string: string
49 @return: translated string using environment locale
50 setting (LC_ALL, LANG or LANGUAGE)
51 @rtype: string
52 """
53 return raw_string
54
56 """
57 Change gettext language on the fly.
58
59 @param lang: new language string (see `locale -a` for a list of
60 supported ones)
61 @type lang: string
62 """
63 global _
64
65 for var in ("LANGUAGE", "LC_ALL", "LANG",):
66 os.environ[var] = lang
67
68
69 old_ = _
70 del _
71 gettext.install('entropy', localedir = envdir, unicode = True)
72 _ = _
73
74 for module in sys.modules.values():
75 if not hasattr(module, "__dict__"):
76 continue
77 t_func = module.__dict__.get("_")
78 if t_func is not old_:
79 continue
80 module._ = _
81