Package entropy :: Module i18n

Source Code for Module entropy.i18n

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3   
  4      @author: Fabio Erculiani <[email protected]> 
  5      @contact: [email protected] 
  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  # make possible to override the default gettext domain 
 31  # from api users 
 32  _GETTEXT_DOMAIN = os.getenv("ETP_GETTEXT_DOMAIN", "entropy") 
 33   
 34  try: 
 35      try: 
 36          import __builtin__ 
 37      except ImportError: 
 38          # python3 
 39          import builtins as __builtin__ 
 40      import gettext 
 41      localedir = "/usr/share/locale" 
 42      # support for ENV TEXTDOMAINDIR 
 43      envdir = os.getenv('TEXTDOMAINDIR') 
 44      if envdir is not None: 
 45          localedir = envdir 
 46      kwargs = {"localedir": localedir} 
 47      kwargs['names'] = ["ngettext"] 
 48      if sys.hexversion < 0x3000000: 
 49          kwargs['unicode'] = True 
 50      gettext.install(_GETTEXT_DOMAIN, **kwargs) 
 51      # do not use gettext.gettext because it returns str instead of unicode 
 52      _ = __builtin__.__dict__['_'] 
 53      ngettext = __builtin__.__dict__['ngettext'] 
 54   
 55  except (ImportError, OSError,): 
56 - def _(raw_string):
57 """ 58 Fallback in case gettext is not available, same syntax 59 for the gettext provided function. 60 61 @param raw_string: raw untranslated string 62 @type raw_string: string 63 @return: translated string using environment locale 64 setting (LC_ALL, LANG or LANGUAGE) 65 @rtype: string 66 """ 67 return raw_string
68
69 - def ngettext(singular, plural, n):
70 """ 71 Plural aware version of _(). Fallback function in case 72 gettext is not available, same syntax as gettext.ngettext. 73 74 @param singular: the singular version of the string 75 @type singular: string 76 @param plural: the plural version of the string 77 @type plural: string 78 @param n: the number of elements 79 @type n: int 80 @return: translated string, either singular or plural 81 basin on n 82 """ 83 if n < 2: 84 return singular 85 return plural
86
87 -def change_language(lang):
88 """ 89 Change gettext language on the fly. 90 91 @param lang: new language string (see `locale -a` for a list of 92 supported ones) 93 @type lang: string 94 """ 95 try: 96 import __builtin__ 97 except ImportError: 98 # python3 99 import builtins as __builtin__ 100 # change in environ 101 for var in ("LANGUAGE", "LC_ALL", "LANG"): 102 os.environ[var] = lang 103 # reinstall gettext 104 # remove _ from global scope so that gettext will readd it 105 old_ = __builtin__.__dict__.get('_') 106 __builtin__.__dict__.pop("_", None) 107 localedir = "/usr/share/locale" 108 # support for ENV TEXTDOMAINDIR 109 envdir = os.getenv('TEXTDOMAINDIR') 110 if envdir is not None: 111 localedir = envdir 112 kw_args = {"localedir": localedir} 113 if sys.hexversion < 0x3000000: 114 kw_args['unicode'] = True 115 gettext.install(_GETTEXT_DOMAIN, **kw_args) 116 _ = __builtin__.__dict__['_'] 117 # redeclare "_" in all loaded modules 118 for module in list(sys.modules.values()): 119 if not hasattr(module, "__dict__"): 120 continue 121 t_func = module.__dict__.get("_") 122 if t_func is not old_: 123 continue 124 module.__dict__['_'] = _
125 126 # Define some constants that can be used externally. 127 ENCODING = "UTF-8" 128 RAW_ENCODING = "raw_unicode_escape" 129 130 # determine whether we have a valid locale configured and 131 # glibc is happy. 132 _FALLBACK_LOCALE = "en_US.UTF-8" 133 _DETECTED_ENC = sys.getfilesystemencoding() 134 135 _VALID_LOCALE = ENCODING.lower() == _DETECTED_ENC.lower() 136 137 # if locale is invalid, we call change_language and switch 138 # to a reliable one that we assume it's always present: _FALLBACK_LOCALE. 139 # Values different from UTF-8 are not supported and can cause 140 # massive system destruction. Unfortunately, there is no good way 141 # to ensure that a valid UTF-8 encoding is selected, but we try to do 142 # our best to automatically recover from this. 143 if not _VALID_LOCALE: 144 default_locale = "en_US." + ENCODING 145 sys.stderr.write("""\ 146 invalid filesystem encoding %s, must be %s. 147 Make sure to set LC_ALL, LANG, LANGUAGE to valid %s values. 148 Please execute: 149 LC_ALL=en_US.%s %s 150 Trying to force %s. 151 """ % (_DETECTED_ENC, ENCODING, ENCODING, ENCODING, 152 " ".join(sys.argv), _FALLBACK_LOCALE)) 153 # switch to the FALLBACK_LOCALE 154 change_language(_FALLBACK_LOCALE) 155