Package backend :: Package common :: Module rhnTranslate
[hide private]
[frames] | no frames]

Source Code for Module backend.common.rhnTranslate

 1  # 
 2  # Copyright (c) 2008--2016 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU General Public License, 
 5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 8  # along with this software; if not, see 
 9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
10  # 
11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
12  # granted to use or replicate Red Hat trademarks that are incorporated 
13  # in this software or its documentation. 
14  # 
15   
16  import os 
17  import gettext 
18   
19  from spacewalk.common.usix import StringType 
20   
21 -class RHN_Translations(gettext.GNUTranslations):
22 # Defining our own class, since we'd like to save the language we use 23 # Determining the language is not very pretty - we parse the file name 24 # which is supposed to be something like 25 # .../<lang>/LC_MESSAGES/<domain>.po 26
27 - def __init__(self, fp=None):
28 self.lang = None 29 gettext.GNUTranslations.__init__(self, fp)
30
31 - def _parse(self, fp):
32 gettext.GNUTranslations._parse(self, fp) 33 filename = fp.name 34 filename = os.path.normpath(filename) 35 # Extract the language 36 self.lang = filename.split('/')[-3]
37
38 - def getlangs(self):
39 # Return all languages 40 # pkilambi:bug#158561,170819,170821: the gettext object in python 2.2.3 has no attribute 41 #_fallback so add a check if __dict__ has key 42 # if not self._fallback or not hasattr(self._fallback, 'getlangs'): 43 if "_fallback" not in self.__dict__ or not self._fallback or not hasattr(self._fallback, 'getlangs'): 44 return [self.lang, 'C'] 45 # Recursive call 46 return [self.lang] + self._fallback.getlangs()
47 48
49 -class i18n:
50 _default_langs = ['en', 'en_US', 'C'] 51 # Wrapper class that allows us to change languages 52
53 - def __init__(self, domain=None, localedir="/usr/share/locale"):
54 self.domain = domain 55 self.localedir = localedir 56 self.langs = self._default_langs[:] 57 self.cat = None 58 self._set_catalog()
59
60 - def _set_catalog(self):
61 # Set the catalog object 62 self.cat = gettext.Catalog(self.domain, localedir=self.localedir, 63 languages=self.langs, fallback=1, class_=RHN_Translations)
64
65 - def getlangs(self):
66 # List of languages we support 67 # pylint: disable=E1103 68 if not hasattr(self.cat, "getlangs"): 69 return ["C"] 70 return self.cat.getlangs()
71
72 - def setlangs(self, langs):
73 if isinstance(langs, StringType): 74 langs = [langs] 75 # Filter "C" - we will add it ourselves later anyway 76 langs = [l for l in langs if l != 'C'] 77 langs.extend(self._default_langs) 78 self.langs = langs 79 self._set_catalog()
80
81 - def gettext(self, string):
82 return self.cat.gettext(string)
83 84 # reinitialize this catalog
85 - def set(self, domain=None, localedir=None):
86 if domain: 87 self.domain = domain 88 if localedir: 89 self.localedir = localedir 90 self._set_catalog()
91 92 93 cat = i18n() 94 _ = cat.gettext 95