blob: 825c2e01df6e9e39325c257282d56de153d890a7 [file] [log] [blame]
Matthew Treinish0db53772013-07-26 10:39:35 -04001# Copyright 2012 Red Hat, Inc.
Matthew Treinish0db53772013-07-26 10:39:35 -04002# Copyright 2013 IBM Corp.
Matthew Treinishffa94d62013-09-11 18:09:17 +00003# All Rights Reserved.
Matthew Treinish0db53772013-07-26 10:39:35 -04004#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17"""
18gettext for openstack-common modules.
19
20Usual usage in an openstack.common module:
21
22 from tempest.openstack.common.gettextutils import _
23"""
24
25import copy
26import gettext
Matthew Treinishffa94d62013-09-11 18:09:17 +000027import logging
Matthew Treinish0db53772013-07-26 10:39:35 -040028import os
29import re
Matthew Treinishffa94d62013-09-11 18:09:17 +000030try:
31 import UserString as _userString
32except ImportError:
33 import collections as _userString
Matthew Treinish0db53772013-07-26 10:39:35 -040034
Matthew Treinishffa94d62013-09-11 18:09:17 +000035from babel import localedata
Matthew Treinish0db53772013-07-26 10:39:35 -040036import six
37
38_localedir = os.environ.get('tempest'.upper() + '_LOCALEDIR')
39_t = gettext.translation('tempest', localedir=_localedir, fallback=True)
40
Matthew Treinishffa94d62013-09-11 18:09:17 +000041_AVAILABLE_LANGUAGES = {}
42USE_LAZY = False
43
44
45def enable_lazy():
46 """Convenience function for configuring _() to use lazy gettext
47
48 Call this at the start of execution to enable the gettextutils._
49 function to use lazy gettext functionality. This is useful if
50 your project is importing _ directly instead of using the
51 gettextutils.install() way of importing the _ function.
52 """
53 global USE_LAZY
54 USE_LAZY = True
55
Matthew Treinish0db53772013-07-26 10:39:35 -040056
57def _(msg):
Matthew Treinishffa94d62013-09-11 18:09:17 +000058 if USE_LAZY:
59 return Message(msg, 'tempest')
60 else:
Matthew Treinishf45528a2013-10-24 20:12:28 +000061 if six.PY3:
62 return _t.gettext(msg)
Matthew Treinishffa94d62013-09-11 18:09:17 +000063 return _t.ugettext(msg)
Matthew Treinish0db53772013-07-26 10:39:35 -040064
65
Matthew Treinishffa94d62013-09-11 18:09:17 +000066def install(domain, lazy=False):
Matthew Treinish0db53772013-07-26 10:39:35 -040067 """Install a _() function using the given translation domain.
68
69 Given a translation domain, install a _() function using gettext's
70 install() function.
71
72 The main difference from gettext.install() is that we allow
73 overriding the default localedir (e.g. /usr/share/locale) using
74 a translation-domain-specific environment variable (e.g.
75 NOVA_LOCALEDIR).
Matthew Treinishffa94d62013-09-11 18:09:17 +000076
77 :param domain: the translation domain
78 :param lazy: indicates whether or not to install the lazy _() function.
79 The lazy _() introduces a way to do deferred translation
80 of messages by installing a _ that builds Message objects,
81 instead of strings, which can then be lazily translated into
82 any available locale.
Matthew Treinish0db53772013-07-26 10:39:35 -040083 """
Matthew Treinishffa94d62013-09-11 18:09:17 +000084 if lazy:
85 # NOTE(mrodden): Lazy gettext functionality.
86 #
87 # The following introduces a deferred way to do translations on
88 # messages in OpenStack. We override the standard _() function
89 # and % (format string) operation to build Message objects that can
90 # later be translated when we have more information.
91 #
92 # Also included below is an example LocaleHandler that translates
93 # Messages to an associated locale, effectively allowing many logs,
94 # each with their own locale.
95
96 def _lazy_gettext(msg):
97 """Create and return a Message object.
98
99 Lazy gettext function for a given domain, it is a factory method
100 for a project/module to get a lazy gettext function for its own
101 translation domain (i.e. nova, glance, cinder, etc.)
102
103 Message encapsulates a string so that we can translate
104 it later when needed.
105 """
106 return Message(msg, domain)
107
Matthew Treinishf45528a2013-10-24 20:12:28 +0000108 from six import moves
109 moves.builtins.__dict__['_'] = _lazy_gettext
Matthew Treinishffa94d62013-09-11 18:09:17 +0000110 else:
111 localedir = '%s_LOCALEDIR' % domain.upper()
Matthew Treinishf45528a2013-10-24 20:12:28 +0000112 if six.PY3:
113 gettext.install(domain,
114 localedir=os.environ.get(localedir))
115 else:
116 gettext.install(domain,
117 localedir=os.environ.get(localedir),
118 unicode=True)
Matthew Treinish0db53772013-07-26 10:39:35 -0400119
120
Matthew Treinishffa94d62013-09-11 18:09:17 +0000121class Message(_userString.UserString, object):
Matthew Treinish0db53772013-07-26 10:39:35 -0400122 """Class used to encapsulate translatable messages."""
123 def __init__(self, msg, domain):
124 # _msg is the gettext msgid and should never change
125 self._msg = msg
126 self._left_extra_msg = ''
127 self._right_extra_msg = ''
Matthew Treinishf45528a2013-10-24 20:12:28 +0000128 self._locale = None
Matthew Treinish0db53772013-07-26 10:39:35 -0400129 self.params = None
Matthew Treinish0db53772013-07-26 10:39:35 -0400130 self.domain = domain
131
132 @property
133 def data(self):
134 # NOTE(mrodden): this should always resolve to a unicode string
135 # that best represents the state of the message currently
136
137 localedir = os.environ.get(self.domain.upper() + '_LOCALEDIR')
138 if self.locale:
139 lang = gettext.translation(self.domain,
140 localedir=localedir,
141 languages=[self.locale],
142 fallback=True)
143 else:
144 # use system locale for translations
145 lang = gettext.translation(self.domain,
146 localedir=localedir,
147 fallback=True)
148
Matthew Treinishf45528a2013-10-24 20:12:28 +0000149 if six.PY3:
150 ugettext = lang.gettext
151 else:
152 ugettext = lang.ugettext
153
Matthew Treinish0db53772013-07-26 10:39:35 -0400154 full_msg = (self._left_extra_msg +
Matthew Treinishf45528a2013-10-24 20:12:28 +0000155 ugettext(self._msg) +
Matthew Treinish0db53772013-07-26 10:39:35 -0400156 self._right_extra_msg)
157
158 if self.params is not None:
159 full_msg = full_msg % self.params
160
161 return six.text_type(full_msg)
162
Matthew Treinishf45528a2013-10-24 20:12:28 +0000163 @property
164 def locale(self):
165 return self._locale
166
167 @locale.setter
168 def locale(self, value):
169 self._locale = value
170 if not self.params:
171 return
172
173 # This Message object may have been constructed with one or more
174 # Message objects as substitution parameters, given as a single
175 # Message, or a tuple or Map containing some, so when setting the
176 # locale for this Message we need to set it for those Messages too.
177 if isinstance(self.params, Message):
178 self.params.locale = value
179 return
180 if isinstance(self.params, tuple):
181 for param in self.params:
182 if isinstance(param, Message):
183 param.locale = value
184 return
185 if isinstance(self.params, dict):
186 for param in self.params.values():
187 if isinstance(param, Message):
188 param.locale = value
189
Matthew Treinish0db53772013-07-26 10:39:35 -0400190 def _save_dictionary_parameter(self, dict_param):
191 full_msg = self.data
192 # look for %(blah) fields in string;
193 # ignore %% and deal with the
194 # case where % is first character on the line
Matthew Treinishffa94d62013-09-11 18:09:17 +0000195 keys = re.findall('(?:[^%]|^)?%\((\w*)\)[a-z]', full_msg)
Matthew Treinish0db53772013-07-26 10:39:35 -0400196
197 # if we don't find any %(blah) blocks but have a %s
198 if not keys and re.findall('(?:[^%]|^)%[a-z]', full_msg):
199 # apparently the full dictionary is the parameter
200 params = copy.deepcopy(dict_param)
201 else:
202 params = {}
203 for key in keys:
204 try:
205 params[key] = copy.deepcopy(dict_param[key])
206 except TypeError:
207 # cast uncopyable thing to unicode string
Matthew Treinishf45528a2013-10-24 20:12:28 +0000208 params[key] = six.text_type(dict_param[key])
Matthew Treinish0db53772013-07-26 10:39:35 -0400209
210 return params
211
212 def _save_parameters(self, other):
213 # we check for None later to see if
214 # we actually have parameters to inject,
215 # so encapsulate if our parameter is actually None
216 if other is None:
217 self.params = (other, )
218 elif isinstance(other, dict):
219 self.params = self._save_dictionary_parameter(other)
220 else:
221 # fallback to casting to unicode,
222 # this will handle the problematic python code-like
223 # objects that cannot be deep-copied
224 try:
225 self.params = copy.deepcopy(other)
226 except TypeError:
Matthew Treinishf45528a2013-10-24 20:12:28 +0000227 self.params = six.text_type(other)
Matthew Treinish0db53772013-07-26 10:39:35 -0400228
229 return self
230
231 # overrides to be more string-like
232 def __unicode__(self):
233 return self.data
234
235 def __str__(self):
Matthew Treinishf45528a2013-10-24 20:12:28 +0000236 if six.PY3:
237 return self.__unicode__()
Matthew Treinish0db53772013-07-26 10:39:35 -0400238 return self.data.encode('utf-8')
239
240 def __getstate__(self):
241 to_copy = ['_msg', '_right_extra_msg', '_left_extra_msg',
Matthew Treinishf45528a2013-10-24 20:12:28 +0000242 'domain', 'params', '_locale']
Matthew Treinish0db53772013-07-26 10:39:35 -0400243 new_dict = self.__dict__.fromkeys(to_copy)
244 for attr in to_copy:
245 new_dict[attr] = copy.deepcopy(self.__dict__[attr])
246
247 return new_dict
248
249 def __setstate__(self, state):
250 for (k, v) in state.items():
251 setattr(self, k, v)
252
253 # operator overloads
254 def __add__(self, other):
255 copied = copy.deepcopy(self)
256 copied._right_extra_msg += other.__str__()
257 return copied
258
259 def __radd__(self, other):
260 copied = copy.deepcopy(self)
261 copied._left_extra_msg += other.__str__()
262 return copied
263
264 def __mod__(self, other):
265 # do a format string to catch and raise
266 # any possible KeyErrors from missing parameters
267 self.data % other
268 copied = copy.deepcopy(self)
269 return copied._save_parameters(other)
270
271 def __mul__(self, other):
272 return self.data * other
273
274 def __rmul__(self, other):
275 return other * self.data
276
277 def __getitem__(self, key):
278 return self.data[key]
279
280 def __getslice__(self, start, end):
281 return self.data.__getslice__(start, end)
282
283 def __getattribute__(self, name):
284 # NOTE(mrodden): handle lossy operations that we can't deal with yet
285 # These override the UserString implementation, since UserString
286 # uses our __class__ attribute to try and build a new message
287 # after running the inner data string through the operation.
288 # At that point, we have lost the gettext message id and can just
289 # safely resolve to a string instead.
290 ops = ['capitalize', 'center', 'decode', 'encode',
291 'expandtabs', 'ljust', 'lstrip', 'replace', 'rjust', 'rstrip',
292 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
293 if name in ops:
294 return getattr(self.data, name)
295 else:
Matthew Treinishffa94d62013-09-11 18:09:17 +0000296 return _userString.UserString.__getattribute__(self, name)
297
298
299def get_available_languages(domain):
300 """Lists the available languages for the given translation domain.
301
302 :param domain: the domain to get languages for
303 """
304 if domain in _AVAILABLE_LANGUAGES:
305 return copy.copy(_AVAILABLE_LANGUAGES[domain])
306
307 localedir = '%s_LOCALEDIR' % domain.upper()
308 find = lambda x: gettext.find(domain,
309 localedir=os.environ.get(localedir),
310 languages=[x])
311
312 # NOTE(mrodden): en_US should always be available (and first in case
313 # order matters) since our in-line message strings are en_US
314 language_list = ['en_US']
315 # NOTE(luisg): Babel <1.0 used a function called list(), which was
316 # renamed to locale_identifiers() in >=1.0, the requirements master list
317 # requires >=0.9.6, uncapped, so defensively work with both. We can remove
Sean Daguefc691e32014-01-03 08:51:54 -0500318 # this check when the master list updates to >=1.0, and update all projects
Matthew Treinishffa94d62013-09-11 18:09:17 +0000319 list_identifiers = (getattr(localedata, 'list', None) or
320 getattr(localedata, 'locale_identifiers'))
321 locale_identifiers = list_identifiers()
322 for i in locale_identifiers:
323 if find(i) is not None:
324 language_list.append(i)
325 _AVAILABLE_LANGUAGES[domain] = language_list
326 return copy.copy(language_list)
327
328
329def get_localized_message(message, user_locale):
Matthew Treinishf45528a2013-10-24 20:12:28 +0000330 """Gets a localized version of the given message in the given locale.
331
332 If the message is not a Message object the message is returned as-is.
333 If the locale is None the message is translated to the default locale.
334
335 :returns: the translated message in unicode, or the original message if
336 it could not be translated
337 """
338 translated = message
Matthew Treinishffa94d62013-09-11 18:09:17 +0000339 if isinstance(message, Message):
Matthew Treinishf45528a2013-10-24 20:12:28 +0000340 original_locale = message.locale
341 message.locale = user_locale
342 translated = six.text_type(message)
343 message.locale = original_locale
344 return translated
Matthew Treinish0db53772013-07-26 10:39:35 -0400345
346
347class LocaleHandler(logging.Handler):
348 """Handler that can have a locale associated to translate Messages.
349
350 A quick example of how to utilize the Message class above.
351 LocaleHandler takes a locale and a target logging.Handler object
352 to forward LogRecord objects to after translating the internal Message.
353 """
354
355 def __init__(self, locale, target):
356 """Initialize a LocaleHandler
357
358 :param locale: locale to use for translating messages
359 :param target: logging.Handler object to forward
360 LogRecord objects to after translation
361 """
362 logging.Handler.__init__(self)
363 self.locale = locale
364 self.target = target
365
366 def emit(self, record):
367 if isinstance(record.msg, Message):
368 # set the locale and resolve to a string
369 record.msg.locale = self.locale
370
371 self.target.emit(record)