From ec4a143a407169f6a7b6d79695db6a641b242f9b Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 4 Jul 2006 03:58:45 +0000 Subject: [PATCH] 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 --- django/conf/__init__.py | 15 +++-- django/conf/global_settings.py | 60 ++++++++++--------- django/templatetags/i18n.py | 8 +-- django/utils/translation/__init__.py | 8 +++ django/utils/translation/trans_null.py | 18 ++++++ .../trans_real.py} | 5 +- 6 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 django/utils/translation/__init__.py create mode 100644 django/utils/translation/trans_null.py rename django/utils/{translation.py => translation/trans_real.py} (99%) 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 similarity index 99% rename from django/utils/translation.py rename to 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