Package entropy :: Module i18n

Source Code for Module entropy.i18n

 1  # -*- coding: utf-8 -*- 
 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      # support for ENV TEXTDOMAINDIR 
34      envdir = os.getenv('TEXTDOMAINDIR') 
35      if envdir is not None: 
36          localedir = envdir 
37      gettext.install('entropy', localedir = envdir, unicode = True) 
38      # do not use gettext.gettext because it returns str instead of unicode 
39      _ = _ 
40   
41  except (ImportError, OSError,): 
42 - def _(raw_string):
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
55 -def change_language(lang):
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 # change in environ 65 for var in ("LANGUAGE", "LC_ALL", "LANG",): 66 os.environ[var] = lang 67 # reinstall gettext 68 # remove _ from global scope so that gettext will readd it 69 old_ = _ 70 del _ 71 gettext.install('entropy', localedir = envdir, unicode = True) 72 _ = _ 73 # redeclare "_" in all loaded modules 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