Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Made django/utils/translation.py into a package django/utils/translat…

…ion, 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
  • Loading branch information...
commit ec4a143a407169f6a7b6d79695db6a641b242f9b 1 parent a25fe3b
@adrianholovaty adrianholovaty authored
View
15 django/conf/__init__.py
@@ -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 @@ def get_all_members(self):
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
60 django/conf/global_settings.py
@@ -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 @@
# 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
8 django/templatetags/i18n.py
@@ -12,7 +12,7 @@ def __init__(self, variable):
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 @@ def __init__(self, variable):
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 top(self):
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 top(self):
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
8 django/utils/translation/__init__.py
@@ -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
18 django/utils/translation/trans_null.py
@@ -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
5 django/utils/translation.py → django/utils/translation/trans_real.py
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.