summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2006-07-04 03:58:45 +0000
committerAdrian Holovaty <adrian@holovaty.com>2006-07-04 03:58:45 +0000
commitec4a143a407169f6a7b6d79695db6a641b242f9b (patch)
treeaf8afc919c4b121fe2acc37cc7b5bab395d62fc6 /django
parenta25fe3b65e39ff6f59ac545d7934461be6c13612 (diff)
downloaddjango-ec4a143a407169f6a7b6d79695db6a641b242f9b.tar.gz
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
Diffstat (limited to 'django')
-rw-r--r--django/conf/__init__.py15
-rw-r--r--django/conf/global_settings.py60
-rw-r--r--django/templatetags/i18n.py8
-rw-r--r--django/utils/translation/__init__.py8
-rw-r--r--django/utils/translation/trans_null.py18
-rw-r--r--django/utils/translation/trans_real.py (renamed from django/utils/translation.py)5
6 files changed, 74 insertions, 40 deletions
diff --git a/django/conf/__init__.py b/django/conf/__init__.py
index d5477201d7..a6a09d772f 100644
--- a/django/conf/__init__.py
+++ b/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 @@ 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
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
index ab09f364fb..a40dc6a6c5 100644
--- a/django/conf/global_settings.py
+++ b/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 @@ 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
diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
index 37b8d93908..6718a0fbac 100644
--- a/django/templatetags/i18n.py
+++ b/django/templatetags/i18n.py
@@ -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 = []
diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
new file mode 100644
index 0000000000..276e59f4bd
--- /dev/null
+++ b/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
diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py
new file mode 100644
index 0000000000..7a3163cfa5
--- /dev/null
+++ b/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
diff --git a/django/utils/translation.py b/django/utils/translation/trans_real.py
index a73c43c257..94df23a8e9 100644
--- a/django/utils/translation.py
+++ b/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