comparison babel/core.py @ 26:6041782ea677 trunk

* Reduce size of locale data pickles by only storing the data provided by each locale itself, and merging inherited data at runtime. * Move locale data loading from `babel.core` into a separate `babel.localedata` module. * Add curency names and symbols to locale data.
author cmlenz
date Sun, 03 Jun 2007 15:27:27 +0000
parents d1e6944f2ff0
children b00b06e5ace8
comparison
equal deleted inserted replaced
25:1b9956f20649 26:6041782ea677
11 # individuals. For the exact contribution history, see the revision 11 # individuals. For the exact contribution history, see the revision
12 # history and logs, available at http://babel.edgewall.org/log/. 12 # history and logs, available at http://babel.edgewall.org/log/.
13 13
14 """Core locale representation and locale data access gateway.""" 14 """Core locale representation and locale data access gateway."""
15 15
16 import os 16 from babel import localedata
17 import pickle
18 try:
19 import threading
20 except ImportError:
21 import dummy_threading as threading
22 17
23 __all__ = ['Locale', 'negotiate', 'parse'] 18 __all__ = ['Locale', 'negotiate', 'parse']
24 __docformat__ = 'restructuredtext en' 19 __docformat__ = 'restructuredtext en'
25 20
26 21
45 >>> locale.number_symbols['decimal'] 40 >>> locale.number_symbols['decimal']
46 u'.' 41 u'.'
47 42
48 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_ 43 :see: `IETF RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
49 """ 44 """
50 _cache = {}
51 _cache_lock = threading.Lock()
52
53 def __new__(cls, language, territory=None, variant=None):
54 """Create new locale object, or load it from the cache if it had already
55 been instantiated.
56
57 >>> l1 = Locale('en')
58 >>> l2 = Locale('en')
59 >>> l1 is l2
60 True
61
62 :param language: the language code
63 :param territory: the territory (country or region) code
64 :param variant: the variant code
65 :return: new or existing `Locale` instance
66 :rtype: `Locale`
67 """
68 key = (language, territory, variant)
69 cls._cache_lock.acquire()
70 try:
71 self = cls._cache.get(key)
72 if self is None:
73 self = super(Locale, cls).__new__(cls, language, territory,
74 variant)
75 cls._cache[key] = self
76 return self
77 finally:
78 self._cache_lock.release()
79 45
80 def __init__(self, language, territory=None, variant=None): 46 def __init__(self, language, territory=None, variant=None):
81 """Initialize the locale object from the given identifier components. 47 """Initialize the locale object from the given identifier components.
82 48
83 >>> locale = Locale('en', 'US') 49 >>> locale = Locale('en', 'US')
91 :param variant: the variant code 57 :param variant: the variant code
92 """ 58 """
93 self.language = language 59 self.language = language
94 self.territory = territory 60 self.territory = territory
95 self.variant = variant 61 self.variant = variant
96 self.__data = None 62 self._data = localedata.load(str(self))
97 63
98 def parse(cls, identifier, sep='_'): 64 def parse(cls, identifier, sep='_'):
99 """Create a `Locale` instance for the given locale identifier. 65 """Create a `Locale` instance for the given locale identifier.
100 66
101 >>> l = Locale.parse('de-DE', sep='-') 67 >>> l = Locale.parse('de-DE', sep='-')
125 91
126 def __str__(self): 92 def __str__(self):
127 return '_'.join(filter(None, [self.language, self.territory, 93 return '_'.join(filter(None, [self.language, self.territory,
128 self.variant])) 94 self.variant]))
129 95
130 def _data(self):
131 if self.__data is None:
132 filename = os.path.join(os.path.dirname(__file__),
133 'localedata/%s.dat' % self)
134 fileobj = open(filename, 'rb')
135 try:
136 self.__data = pickle.load(fileobj)
137 finally:
138 fileobj.close()
139 return self.__data
140 _data = property(_data)
141
142 def display_name(self): 96 def display_name(self):
143 retval = self.languages.get(self.language) 97 retval = self.languages.get(self.language)
144 if self.territory: 98 if self.territory:
145 variant = '' 99 variant = ''
146 if self.variant: 100 if self.variant:
206 160
207 :type: `dict` 161 :type: `dict`
208 """) 162 """)
209 163
210 #{ Number Formatting 164 #{ Number Formatting
165
166 def currencies(self):
167 return self._data['currency_names']
168 currencies = property(currencies, doc="""\
169 Mapping of currency codes to translated currency names.
170
171 >>> Locale('en').currencies['COP']
172 u'Colombian Peso'
173 >>> Locale('de', 'DE').currencies['COP']
174 u'Kolumbianischer Peso'
175
176 :type: `dict`
177 """)
178
179 def currency_symbols(self):
180 return self._data['currency_symbols']
181 currency_symbols = property(currency_symbols, doc="""\
182 Mapping of currency codes to symbols.
183
184 >>> Locale('en').currency_symbols['USD']
185 u'US$'
186 >>> Locale('en', 'US').currency_symbols['USD']
187 u'$'
188
189 :type: `dict`
190 """)
211 191
212 def number_symbols(self): 192 def number_symbols(self):
213 return self._data['number_symbols'] 193 return self._data['number_symbols']
214 number_symbols = property(number_symbols, doc="""\ 194 number_symbols = property(number_symbols, doc="""\
215 Symbols used in number formatting. 195 Symbols used in number formatting.
Copyright (C) 2012-2017 Edgewall Software