Made django/utils/translation.py into a package django/utils/translation, which is loaded lazily depending on the value of settings.USE_I18N.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3271 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-07-04 03:58:45 +00:00
parent a25fe3b65e
commit ec4a143a40
6 changed files with 74 additions and 40 deletions

View File

@ -116,7 +116,7 @@ class UserSettingsHolder(object):
"""
Holder for user configured settings.
"""
# SETTINGS_MODULE does not really make sense in the manually configured
# SETTINGS_MODULE doesn't make much sense in the manually configured
# (standalone) case.
SETTINGS_MODULE = None
@ -135,6 +135,13 @@ class UserSettingsHolder(object):
settings = LazySettings()
# install the translation machinery so that it is available
from django.utils import translation
translation.install()
# This function replaces itself with django.utils.translation.gettext() the
# first time it's run. This is necessary because the import of
# django.utils.translation requires a working settings module, and loading it
# from within this file would cause a circular import.
def first_time_gettext(*args):
from django.utils.translation import gettext
__builtins__['_'] = gettext
return gettext(*args)
__builtins__['_'] = first_time_gettext

View File

@ -1,7 +1,9 @@
# Default Django settings. Override these with settings in the module
# pointed-to by the DJANGO_SETTINGS_MODULE environment variable.
from django.utils.translation import gettext_lazy as _
# This is defined here as a do-nothing function because we can't import
# django.utils.translation -- that module depends on the settings.
gettext_noop = lambda s: s
####################
# CORE #
@ -34,34 +36,34 @@ LANGUAGE_CODE = 'en-us'
# Languages we provide translations for, out of the box. The language name
# should be the utf-8 encoded local name for the language.
LANGUAGES = (
('bn', _('Bengali')),
('cs', _('Czech')),
('cy', _('Welsh')),
('da', _('Danish')),
('de', _('German')),
('el', _('Greek')),
('en', _('English')),
('es', _('Spanish')),
('es_AR', _('Argentinean Spanish')),
('fr', _('French')),
('gl', _('Galician')),
('hu', _('Hungarian')),
('he', _('Hebrew')),
('is', _('Icelandic')),
('it', _('Italian')),
('ja', _('Japanese')),
('nl', _('Dutch')),
('no', _('Norwegian')),
('pt-br', _('Brazilian')),
('ro', _('Romanian')),
('ru', _('Russian')),
('sk', _('Slovak')),
('sl', _('Slovenian')),
('sr', _('Serbian')),
('sv', _('Swedish')),
('uk', _('Ukrainian')),
('zh-cn', _('Simplified Chinese')),
('zh-tw', _('Traditional Chinese')),
('bn', gettext_noop('Bengali')),
('cs', gettext_noop('Czech')),
('cy', gettext_noop('Welsh')),
('da', gettext_noop('Danish')),
('de', gettext_noop('German')),
('el', gettext_noop('Greek')),
('en', gettext_noop('English')),
('es', gettext_noop('Spanish')),
('es_AR', gettext_noop('Argentinean Spanish')),
('fr', gettext_noop('French')),
('gl', gettext_noop('Galician')),
('hu', gettext_noop('Hungarian')),
('he', gettext_noop('Hebrew')),
('is', gettext_noop('Icelandic')),
('it', gettext_noop('Italian')),
('ja', gettext_noop('Japanese')),
('nl', gettext_noop('Dutch')),
('no', gettext_noop('Norwegian')),
('pt-br', gettext_noop('Brazilian')),
('ro', gettext_noop('Romanian')),
('ru', gettext_noop('Russian')),
('sk', gettext_noop('Slovak')),
('sl', gettext_noop('Slovenian')),
('sr', gettext_noop('Serbian')),
('sv', gettext_noop('Swedish')),
('uk', gettext_noop('Ukrainian')),
('zh-cn', gettext_noop('Simplified Chinese')),
('zh-tw', gettext_noop('Traditional Chinese')),
)
# Languages using BiDi (right-to-left) layout

View File

@ -12,7 +12,7 @@ class GetAvailableLanguagesNode(Node):
def render(self, context):
from django.conf import settings
context[self.variable] = settings.LANGUAGES
context[self.variable] = [(k, translation.gettext(v)) for k, v in settings.LANGUAGES]
return ''
class GetCurrentLanguageNode(Node):
@ -30,7 +30,7 @@ class GetCurrentLanguageBidiNode(Node):
def render(self, context):
context[self.variable] = translation.get_language_bidi()
return ''
class TranslateNode(Node):
def __init__(self, value, noop):
self.value = value
@ -171,7 +171,7 @@ def do_translate(parser, token):
else:
noop = False
return (value, noop)
(value, noop) = TranslateParser(token.contents).top()
value, noop = TranslateParser(token.contents).top()
return TranslateNode(value, noop)
def do_block_translate(parser, token):
@ -216,7 +216,7 @@ def do_block_translate(parser, token):
raise TemplateSyntaxError, "unknown subtag %s for 'blocktrans' found" % tag
return (countervar, counter, extra_context)
(countervar, counter, extra_context) = BlockTranslateParser(token.contents).top()
countervar, counter, extra_context = BlockTranslateParser(token.contents).top()
singular = []
plural = []

View File

@ -0,0 +1,8 @@
from django.conf import settings
if settings.USE_I18N:
from trans_real import *
else:
from trans_null import *
del settings

View File

@ -0,0 +1,18 @@
# These are versions of the functions in django.utils.translation.trans_real
# that don't actually do anything. This is purely for performance, so that
# settings.USE_I18N = False can use this module rather than trans_real.py.
from django.conf import settings
def ngettext(singular, plural, number):
if number == 1: return singular
return plural
ngettext_lazy = ngettext
gettext = gettext_noop = gettext_lazy = _ = lambda x: x
string_concat = lambda *strings: ''.join([str(el) for el in strings])
activate = lambda x: None
deactivate = install = lambda: None
get_language = lambda: 'en'
get_date_formats = lambda: settings.DATE_FORMAT, settings.DATETIME_FORMAT, settings.TIME_FORMAT
get_partial_date_formats = lambda: settings.YEAR_MONTH_FORMAT, settings.MONTH_DAY_FORMAT

View File

@ -1,4 +1,4 @@
"translation helper functions"
"Translation helper functions"
import os, re, sys
import gettext as gettext_module
@ -221,7 +221,6 @@ def get_language_bidi():
False = left-to-right layout
True = right-to-left layout
"""
from django.conf import settings
return get_language() in settings.LANGUAGES_BIDI
@ -389,7 +388,7 @@ def get_partial_date_formats():
def install():
"""
Installs the gettext function as the default translation function under
the name _.
the name '_'.
"""
__builtins__['_'] = gettext