1
0
mirror of https://github.com/django/django.git synced 2024-12-27 03:25:58 +00:00

[py3] Ported django.utils.translation.

This commit is contained in:
Aymeric Augustin 2012-07-21 22:24:13 +02:00
parent 17da0aa893
commit 9e8df02d68

View File

@ -9,7 +9,9 @@ import gettext as gettext_module
from threading import local from threading import local
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.encoding import smart_str, smart_text
from django.utils.safestring import mark_safe, SafeData from django.utils.safestring import mark_safe, SafeData
from django.utils import six
from django.utils.six import StringIO from django.utils.six import StringIO
@ -259,12 +261,14 @@ def do_translate(message, translation_function):
def gettext(message): def gettext(message):
return do_translate(message, 'gettext') return do_translate(message, 'gettext')
def ugettext(message): if six.PY3:
return do_translate(message, 'ugettext') ugettext = gettext
else:
def ugettext(message):
return do_translate(message, 'ugettext')
def pgettext(context, message): def pgettext(context, message):
result = do_translate( result = ugettext("%s%s%s" % (context, CONTEXT_SEPARATOR, message))
"%s%s%s" % (context, CONTEXT_SEPARATOR, message), 'ugettext')
if CONTEXT_SEPARATOR in result: if CONTEXT_SEPARATOR in result:
# Translation not found # Translation not found
result = message result = message
@ -297,20 +301,23 @@ def ngettext(singular, plural, number):
""" """
return do_ntranslate(singular, plural, number, 'ngettext') return do_ntranslate(singular, plural, number, 'ngettext')
def ungettext(singular, plural, number): if six.PY3:
""" ungettext = ngettext
Returns a unicode strings of the translation of either the singular or else:
plural, based on the number. def ungettext(singular, plural, number):
""" """
return do_ntranslate(singular, plural, number, 'ungettext') Returns a unicode strings of the translation of either the singular or
plural, based on the number.
"""
return do_ntranslate(singular, plural, number, 'ungettext')
def npgettext(context, singular, plural, number): def npgettext(context, singular, plural, number):
result = do_ntranslate("%s%s%s" % (context, CONTEXT_SEPARATOR, singular), result = ungettext("%s%s%s" % (context, CONTEXT_SEPARATOR, singular),
"%s%s%s" % (context, CONTEXT_SEPARATOR, plural), "%s%s%s" % (context, CONTEXT_SEPARATOR, plural),
number, 'ungettext') number)
if CONTEXT_SEPARATOR in result: if CONTEXT_SEPARATOR in result:
# Translation not found # Translation not found
result = do_ntranslate(singular, plural, number, 'ungettext') result = ungettext(singular, plural, number)
return result return result
def all_locale_paths(): def all_locale_paths():
@ -440,7 +447,7 @@ def templatize(src, origin=None):
from django.conf import settings from django.conf import settings
from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK, from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK,
TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK) TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK)
src = src.decode(settings.FILE_CHARSET) src = smart_text(src, settings.FILE_CHARSET)
out = StringIO() out = StringIO()
message_context = None message_context = None
intrans = False intrans = False
@ -455,7 +462,7 @@ def templatize(src, origin=None):
content = ''.join(comment) content = ''.join(comment)
translators_comment_start = None translators_comment_start = None
for lineno, line in enumerate(content.splitlines(True)): for lineno, line in enumerate(content.splitlines(True)):
if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK): if line.lstrip().startswith(smart_text(TRANSLATOR_COMMENT_MARK)):
translators_comment_start = lineno translators_comment_start = lineno
for lineno, line in enumerate(content.splitlines(True)): for lineno, line in enumerate(content.splitlines(True)):
if translators_comment_start is not None and lineno >= translators_comment_start: if translators_comment_start is not None and lineno >= translators_comment_start:
@ -570,7 +577,7 @@ def templatize(src, origin=None):
out.write(' # %s' % t.contents) out.write(' # %s' % t.contents)
else: else:
out.write(blankout(t.contents, 'X')) out.write(blankout(t.contents, 'X'))
return out.getvalue().encode('utf-8') return smart_str(out.getvalue())
def parse_accept_lang_header(lang_string): def parse_accept_lang_header(lang_string):
""" """