Skip to content
Browse files

Allow to use custom i18n backends, just like custom test runners.

  • Loading branch information...
1 parent 0eeae15 commit 35dfea49c83f990780e181c59c10f9702e72f2e1 @rtnpro committed Nov 30, 2012
Showing with 54 additions and 4 deletions.
  1. +4 −0 django/conf/global_settings.py
  2. +50 −4 django/utils/translation/__init__.py
View
4 django/conf/global_settings.py
@@ -562,6 +562,10 @@
# The name of the class to use to run the test suite
TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'
+# The name of the class to use as i18n backend
+I18N_BACKEND_REAL = 'django.utils.translation.I18nRealBackend'
+I18N_BACKEND_NULL = 'django.utils.translation.I18nNullBackend'
+
############
# FIXTURES #
############
View
54 django/utils/translation/__init__.py
@@ -19,6 +19,7 @@
'ungettext', 'ungettext_lazy',
'pgettext', 'pgettext_lazy',
'npgettext', 'npgettext_lazy',
+ 'I18nRealBackend', 'I18nNullBackend'
]
# Here be dragons, so a short explanation of the logic won't hurt:
@@ -29,6 +30,48 @@
# replace the functions with their real counterparts (once we do access the
# settings).
+class I18nRealBackend(object):
+ """
+ i18n backend to be used when settings.USE_I18N == True.
+ This class can also be subclassed to build a custom
+ i18n real backend.
+ """
+ def __getattr__(self, real_name):
+ from django.utils.translation import trans_real
+ return getattr(trans_real, real_name)
+
+class I18nNullBackend(object):
+ """
+ i18n backend to be used when settings.USE_I18N != True.
+ This class can also be subclassed to build a custom
+ i18n null backend.
+ """
+ def __getattr__(self, real_name):
+ from django.utils.translation import trans_null
+ return getattr(trans_null, real_name)
+
+def get_i18n_backend(i18n_backend_name, settings):
+ """
+ Get i18n backend object from settings variables:
+ - I18N_BACKEND_REAL
+ - I18N_BACKEND_NULL
+
+ Args:
+ i18n_backend_name: A string, either I18N_BACKEND_REAL or
+ I18N_BACKEND_NULL
+ settings: django.conf.settings
+ Returns:
+ An i18n backend object
+ """
+ i18n_class = getattr(settings, i18n_backend_name)
+ i18n_path = i18n_class.split('.')
+ if len(i18n_path) > 1:
+ i18n_module_name = '.'.join(i18n_path[:-1])
+ else:
+ i18n_module_name = '.'
+ i18n_module = __import__(i18n_module_name, {}, {}, i18n_path[-1])
+ return getattr(i18n_module, i18n_path[-1])()
+
class Trans(object):
"""
The purpose of this class is to store the actual translation function upon
@@ -41,13 +84,16 @@ class Trans(object):
performance effect, as access to the function goes the normal path,
instead of using __getattr__.
"""
-
- def __getattr__(self, real_name):
+ def get_trans(self):
from django.conf import settings
if settings.USE_I18N:
- from django.utils.translation import trans_real as trans
+ i18n_backend_name = 'I18N_BACKEND_REAL'
else:
- from django.utils.translation import trans_null as trans
+ i18n_backend_name = 'I18N_BACKEND_NULL'
+ return get_i18n_backend(i18n_backend_name, settings)
+
+ def __getattr__(self, real_name):
+ trans = self.get_trans()
setattr(self, real_name, getattr(trans, real_name))
return getattr(trans, real_name)

0 comments on commit 35dfea4

Please sign in to comment.
Something went wrong with that request. Please try again.