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. 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. # (standalone) case.
SETTINGS_MODULE = None SETTINGS_MODULE = None
@ -135,6 +135,13 @@ class UserSettingsHolder(object):
settings = LazySettings() settings = LazySettings()
# install the translation machinery so that it is available # This function replaces itself with django.utils.translation.gettext() the
from django.utils import translation # first time it's run. This is necessary because the import of
translation.install() # 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 # Default Django settings. Override these with settings in the module
# pointed-to by the DJANGO_SETTINGS_MODULE environment variable. # 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 # # CORE #
@ -34,34 +36,34 @@ LANGUAGE_CODE = 'en-us'
# Languages we provide translations for, out of the box. The language name # Languages we provide translations for, out of the box. The language name
# should be the utf-8 encoded local name for the language. # should be the utf-8 encoded local name for the language.
LANGUAGES = ( LANGUAGES = (
('bn', _('Bengali')), ('bn', gettext_noop('Bengali')),
('cs', _('Czech')), ('cs', gettext_noop('Czech')),
('cy', _('Welsh')), ('cy', gettext_noop('Welsh')),
('da', _('Danish')), ('da', gettext_noop('Danish')),
('de', _('German')), ('de', gettext_noop('German')),
('el', _('Greek')), ('el', gettext_noop('Greek')),
('en', _('English')), ('en', gettext_noop('English')),
('es', _('Spanish')), ('es', gettext_noop('Spanish')),
('es_AR', _('Argentinean Spanish')), ('es_AR', gettext_noop('Argentinean Spanish')),
('fr', _('French')), ('fr', gettext_noop('French')),
('gl', _('Galician')), ('gl', gettext_noop('Galician')),
('hu', _('Hungarian')), ('hu', gettext_noop('Hungarian')),
('he', _('Hebrew')), ('he', gettext_noop('Hebrew')),
('is', _('Icelandic')), ('is', gettext_noop('Icelandic')),
('it', _('Italian')), ('it', gettext_noop('Italian')),
('ja', _('Japanese')), ('ja', gettext_noop('Japanese')),
('nl', _('Dutch')), ('nl', gettext_noop('Dutch')),
('no', _('Norwegian')), ('no', gettext_noop('Norwegian')),
('pt-br', _('Brazilian')), ('pt-br', gettext_noop('Brazilian')),
('ro', _('Romanian')), ('ro', gettext_noop('Romanian')),
('ru', _('Russian')), ('ru', gettext_noop('Russian')),
('sk', _('Slovak')), ('sk', gettext_noop('Slovak')),
('sl', _('Slovenian')), ('sl', gettext_noop('Slovenian')),
('sr', _('Serbian')), ('sr', gettext_noop('Serbian')),
('sv', _('Swedish')), ('sv', gettext_noop('Swedish')),
('uk', _('Ukrainian')), ('uk', gettext_noop('Ukrainian')),
('zh-cn', _('Simplified Chinese')), ('zh-cn', gettext_noop('Simplified Chinese')),
('zh-tw', _('Traditional Chinese')), ('zh-tw', gettext_noop('Traditional Chinese')),
) )
# Languages using BiDi (right-to-left) layout # Languages using BiDi (right-to-left) layout

View File

@ -12,7 +12,7 @@ class GetAvailableLanguagesNode(Node):
def render(self, context): def render(self, context):
from django.conf import settings from django.conf import settings
context[self.variable] = settings.LANGUAGES context[self.variable] = [(k, translation.gettext(v)) for k, v in settings.LANGUAGES]
return '' return ''
class GetCurrentLanguageNode(Node): class GetCurrentLanguageNode(Node):
@ -171,7 +171,7 @@ def do_translate(parser, token):
else: else:
noop = False noop = False
return (value, noop) return (value, noop)
(value, noop) = TranslateParser(token.contents).top() value, noop = TranslateParser(token.contents).top()
return TranslateNode(value, noop) return TranslateNode(value, noop)
def do_block_translate(parser, token): 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 raise TemplateSyntaxError, "unknown subtag %s for 'blocktrans' found" % tag
return (countervar, counter, extra_context) return (countervar, counter, extra_context)
(countervar, counter, extra_context) = BlockTranslateParser(token.contents).top() countervar, counter, extra_context = BlockTranslateParser(token.contents).top()
singular = [] singular = []
plural = [] 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 os, re, sys
import gettext as gettext_module import gettext as gettext_module
@ -221,7 +221,6 @@ def get_language_bidi():
False = left-to-right layout False = left-to-right layout
True = right-to-left layout True = right-to-left layout
""" """
from django.conf import settings from django.conf import settings
return get_language() in settings.LANGUAGES_BIDI return get_language() in settings.LANGUAGES_BIDI
@ -389,7 +388,7 @@ def get_partial_date_formats():
def install(): def install():
""" """
Installs the gettext function as the default translation function under Installs the gettext function as the default translation function under
the name _. the name '_'.
""" """
__builtins__['_'] = gettext __builtins__['_'] = gettext