summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-10-29 16:48:58 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-10-29 16:48:58 +0000
commitccc49029b8d84cf3eaaa3593df6370329f7b14e1 (patch)
tree3e88f1a774a557cae46a7095073ccf6e05e7fff8 /django/utils
parent269e921756371bee6d35a967bc2ffe84d1ae39eb (diff)
Fixed #14181 -- Added a template tag and filters to allow localization to be disabled in a template. Thanks to Benjamin Wohlwend for the work on the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14395 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/formats.py49
1 files changed, 32 insertions, 17 deletions
diff --git a/django/utils/formats.py b/django/utils/formats.py
index e64cc4e95a..18fcbb600f 100644
--- a/django/utils/formats.py
+++ b/django/utils/formats.py
@@ -41,14 +41,17 @@ def get_format_modules(reverse=False):
modules.reverse()
return modules
-def get_format(format_type, lang=None):
+def get_format(format_type, lang=None, use_l10n=None):
"""
For a specific format type, returns the format for the current
language (locale), defaults to the format in the settings.
format_type is the name of the format, e.g. 'DATE_FORMAT'
+
+ If use_l10n is provided and is not None, that will force the value to
+ be localized (or not), overriding the value of settings.USE_L10N.
"""
format_type = smart_str(format_type)
- if settings.USE_L10N:
+ if use_l10n or (use_l10n is None and settings.USE_L10N):
if lang is None:
lang = get_language()
cache_key = (format_type, lang)
@@ -65,48 +68,60 @@ def get_format(format_type, lang=None):
_format_cache[cache_key] = None
return getattr(settings, format_type)
-def date_format(value, format=None):
+def date_format(value, format=None, use_l10n=None):
"""
Formats a datetime.date or datetime.datetime object using a
localizable format
+
+ If use_l10n is provided and is not None, that will force the value to
+ be localized (or not), overriding the value of settings.USE_L10N.
"""
- return dateformat.format(value, get_format(format or 'DATE_FORMAT'))
+ return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))
-def time_format(value, format=None):
+def time_format(value, format=None, use_l10n=None):
"""
Formats a datetime.time object using a localizable format
+
+ If use_l10n is provided and is not None, that will force the value to
+ be localized (or not), overriding the value of settings.USE_L10N.
"""
- return dateformat.time_format(value, get_format(format or 'TIME_FORMAT'))
+ return dateformat.time_format(value, get_format(format or 'TIME_FORMAT', use_l10n=use_l10n))
-def number_format(value, decimal_pos=None):
+def number_format(value, decimal_pos=None, use_l10n=None):
"""
Formats a numeric value using localization settings
+
+ If use_l10n is provided and is not None, that will force the value to
+ be localized (or not), overriding the value of settings.USE_L10N.
"""
- if settings.USE_L10N:
+ if use_l10n or (use_l10n is None and settings.USE_L10N):
lang = get_language()
else:
lang = None
return numberformat.format(
value,
- get_format('DECIMAL_SEPARATOR', lang),
+ get_format('DECIMAL_SEPARATOR', lang, use_l10n=use_l10n),
decimal_pos,
- get_format('NUMBER_GROUPING', lang),
- get_format('THOUSAND_SEPARATOR', lang),
+ get_format('NUMBER_GROUPING', lang, use_l10n=use_l10n),
+ get_format('THOUSAND_SEPARATOR', lang, use_l10n=use_l10n),
)
-def localize(value):
+def localize(value, use_l10n=None):
"""
Checks if value is a localizable type (date, number...) and returns it
- formatted as a string using current locale format
+ formatted as a string using current locale format.
+
+ If use_l10n is provided and is not None, that will force the value to
+ be localized (or not), overriding the value of settings.USE_L10N.
"""
if isinstance(value, (decimal.Decimal, float, int, long)):
- return number_format(value)
+ return number_format(value, use_l10n=use_l10n)
elif isinstance(value, datetime.datetime):
- return date_format(value, 'DATETIME_FORMAT')
+ return date_format(value, 'DATETIME_FORMAT', use_l10n=use_l10n)
elif isinstance(value, datetime.date):
- return date_format(value)
+ return date_format(value, use_l10n=use_l10n)
elif isinstance(value, datetime.time):
- return time_format(value, 'TIME_FORMAT')
+ return time_format(value, 'TIME_FORMAT', use_l10n=use_l10n)
else:
return value