Changeset 5055
- Timestamp:
- 04/21/07 09:16:21 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/unicode/django/utils/translation/__init__.py
r4907 r5055 8 8 'get_language', 'get_language_bidi', 'get_date_formats', 9 9 'get_partial_date_formats', 'check_for_language', 'to_locale', 10 'get_language_from_request', 'install', 'templatize'] 10 'get_language_from_request', 'install', 'templatize', 'ugettext', 11 'ungettext'] 11 12 12 13 # Here be dragons, so a short explanation of the logic won't hurt: … … 49 50 return real_gettext_noop(message) 50 51 52 ugettext_noop = gettext_noop 53 51 54 def gettext(message): 52 55 return real_gettext(message) 53 54 56 55 57 def ngettext(singular, plural, number): 56 58 return real_ngettext(singular, plural, number) 57 59 60 def ugettext(message): 61 return real_ugettext(message) 62 63 def ungettext(singular, plural, number): 64 return real_ungettext(singular, plural, number) 65 58 66 def string_concat(*strings): 59 67 return real_string_concat(*strings) 60 68 61 ngettext_lazy = lazy(ngettext, str, unicode) 62 gettext_lazy = lazy(gettext, str, unicode) 69 ngettext_lazy = lazy(ngettext, str) 70 gettext_lazy = lazy(gettext, str) 71 ungettext_lazy = lazy(ungettext, unicode) 72 ugettext_lazy = lazy(ugettext, unicode) 63 73 string_concat = lazy(string_concat, str, unicode) 64 74 django/branches/unicode/django/utils/translation/trans_null.py
r4932 r5055 4 4 5 5 from django.conf import settings 6 from django.utils.encoding import smart_unicode 6 7 7 8 def ngettext(singular, plural, number): … … 9 10 return plural 10 11 ngettext_lazy = ngettext 12 13 def ungettext(singular, plural, number): 14 return smart_unicode(ngettext(singular, plural, number)) 11 15 12 16 string_concat = lambda *strings: ''.join([str(el) for el in strings]) … … 31 35 return TECHNICAL_ID_MAP.get(message, message) 32 36 37 def ugettext(message): 38 return smart_unicode(gettext(message)) 39 33 40 gettext_noop = gettext_lazy = _ = gettext 34 41 django/branches/unicode/django/utils/translation/trans_real.py
r4905 r5055 4 4 import gettext as gettext_module 5 5 from cStringIO import StringIO 6 from django.utils.encoding import smart_str, smart_unicode 6 7 7 8 try: … … 58 59 # identical with the translation file charset. 59 60 try: 60 self.set_output_charset( settings.DEFAULT_CHARSET)61 self.set_output_charset('utf-8') 61 62 except AttributeError: 62 63 pass 63 self.django_output_charset = settings.DEFAULT_CHARSET64 self.django_output_charset = 'utf-8' 64 65 self.__language = '??' 65 66 … … 239 240 return _default 240 241 242 def do_translate(message, translation_function): 243 """ 244 Translate 'message' using the given 'translation_function' name -- which 245 will be either gettext or ugettext. 246 """ 247 global _default, _active 248 t = _active.get(currentThread(), None) 249 if t is not None: 250 return getattr(t, translation_function)(message) 251 if _default is None: 252 from django.conf import settings 253 _default = translation(settings.LANGUAGE_CODE) 254 return getattr(_default, translation_function)(message) 255 241 256 def gettext(message): 242 257 """ … … 246 261 message will be run through the default translation object. 247 262 """ 263 return do_translate(message, 'gettext') 264 265 def ugettext(message): 266 return do_translate(message, 'ugettext') 267 268 def gettext_noop(message): 269 """ 270 Marks strings for translation but doesn't translate them now. This can be 271 used to store strings in global variables that should stay in the base 272 language (because they might be used externally) and will be translated 273 later. 274 """ 275 return message 276 277 def do_ntranslate(singular, plural, number, translation_function): 248 278 global _default, _active 279 249 280 t = _active.get(currentThread(), None) 250 281 if t is not None: 251 return t.gettext(message)282 return getattr(t, translation_function)(singular, plural, number) 252 283 if _default is None: 253 284 from django.conf import settings 254 285 _default = translation(settings.LANGUAGE_CODE) 255 return _default.gettext(message) 256 257 def gettext_noop(message): 258 """ 259 Marks strings for translation but doesn't translate them now. This can be 260 used to store strings in global variables that should stay in the base 261 language (because they might be used externally) and will be translated later. 262 """ 263 return message 286 return getattr(_default, translation_function)(singular, plural, number) 264 287 265 288 def ngettext(singular, plural, number): 266 289 """ 267 Returns the translation of either the singular or plural, based on the number.268 """269 global _default, _active270 271 t = _active.get(currentThread(), None) 272 if t is not None:273 return t.ngettext(singular, plural, number)274 if _default is None:275 from django.conf import settings276 _default = translation(settings.LANGUAGE_CODE)277 return _default.ngettext(singular, plural, number)290 Returns a UTF-8 bytestring of the translation of either the singular or 291 plural, based on the number. 292 """ 293 return do_ntranslate(singular, plural, number, 'ngettext') 294 295 def ungettext(singular, plural, number): 296 """ 297 Returns a unicode strings of the translation of either the singular or 298 plural, based on the number. 299 """ 300 return do_ntranslate(singular, plural, number, 'ungettext') 278 301 279 302 def check_for_language(lang_code): 280 303 """ 281 Checks whether there is a global language file for the given language code. 282 This is used to decide whether a user-provided language is available. This is 283 only used for language codes from either the cookies or session. 304 Checks whether there is a global language file for the given language 305 code. This is used to decide whether a user-provided language is 306 available. This is only used for language codes from either the cookies or 307 session. 284 308 """ 285 309 from django.conf import settings … … 292 316 def get_language_from_request(request): 293 317 """ 294 Analyzes the request to find what language the user wants the system to show. 295 Only languages listed in settings.LANGUAGES are taken into account. If the user 296 requests a sublanguage where we have a main language, we send out the main language. 318 Analyzes the request to find what language the user wants the system to 319 show. Only languages listed in settings.LANGUAGES are taken into account. 320 If the user requests a sublanguage where we have a main language, we send 321 out the main language. 297 322 """ 298 323 global _accepted
