2009-12-23 01:58:49 +08:00
|
|
|
import decimal
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.utils.translation import get_language, to_locale, check_for_language
|
|
|
|
from django.utils.importlib import import_module
|
2009-12-31 06:11:48 +08:00
|
|
|
from django.utils.encoding import smart_str
|
|
|
|
from django.utils import dateformat, numberformat, datetime_safe
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2010-09-27 23:25:08 +08:00
|
|
|
# format_cache is a mapping from (format_type, lang) to the format string.
|
|
|
|
# By using the cache, it is possible to avoid running get_format_modules
|
|
|
|
# repeatedly.
|
|
|
|
_format_cache = {}
|
|
|
|
_format_modules_cache = {}
|
|
|
|
|
|
|
|
def iter_format_modules(lang):
|
|
|
|
"""
|
|
|
|
Does the heavy lifting of finding format modules.
|
|
|
|
"""
|
|
|
|
if check_for_language(lang) or settings.USE_L10N:
|
|
|
|
format_locations = ['django.conf.locale.%s']
|
|
|
|
if settings.FORMAT_MODULE_PATH:
|
|
|
|
format_locations.append(settings.FORMAT_MODULE_PATH + '.%s')
|
|
|
|
format_locations.reverse()
|
|
|
|
locale = to_locale(lang)
|
|
|
|
locales = set((locale, locale.split('_')[0]))
|
|
|
|
for location in format_locations:
|
|
|
|
for loc in locales:
|
|
|
|
try:
|
|
|
|
yield import_module('.formats', location % loc)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2009-12-31 06:12:16 +08:00
|
|
|
def get_format_modules(reverse=False):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-09-27 23:25:08 +08:00
|
|
|
Returns an iterator over the format modules found
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-09-27 23:25:08 +08:00
|
|
|
lang = get_language()
|
|
|
|
modules = _format_modules_cache.setdefault(lang, list(iter_format_modules(lang)))
|
2009-12-31 06:12:16 +08:00
|
|
|
if reverse:
|
|
|
|
modules.reverse()
|
2009-12-23 01:58:49 +08:00
|
|
|
return modules
|
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def get_format(format_type, lang=None, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
|
|
|
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'
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
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.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-01-02 05:38:58 +08:00
|
|
|
format_type = smart_str(format_type)
|
2010-10-30 00:48:58 +08:00
|
|
|
if use_l10n or (use_l10n is None and settings.USE_L10N):
|
2010-09-27 23:25:08 +08:00
|
|
|
if lang is None:
|
|
|
|
lang = get_language()
|
|
|
|
cache_key = (format_type, lang)
|
|
|
|
try:
|
|
|
|
return _format_cache[cache_key] or getattr(settings, format_type)
|
|
|
|
except KeyError:
|
|
|
|
for module in get_format_modules():
|
|
|
|
try:
|
|
|
|
val = getattr(module, format_type)
|
|
|
|
_format_cache[cache_key] = val
|
|
|
|
return val
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
_format_cache[cache_key] = None
|
2009-12-23 01:58:49 +08:00
|
|
|
return getattr(settings, format_type)
|
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def date_format(value, format=None, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
|
|
|
Formats a datetime.date or datetime.datetime object using a
|
|
|
|
localizable format
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
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.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-10-30 00:48:58 +08:00
|
|
|
return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def time_format(value, format=None, use_l10n=None):
|
2010-01-02 05:36:36 +08:00
|
|
|
"""
|
|
|
|
Formats a datetime.time object using a localizable format
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
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.
|
2010-01-02 05:36:36 +08:00
|
|
|
"""
|
2010-10-30 00:48:58 +08:00
|
|
|
return dateformat.time_format(value, get_format(format or 'TIME_FORMAT', use_l10n=use_l10n))
|
2010-01-02 05:36:36 +08:00
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def number_format(value, decimal_pos=None, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
|
|
|
Formats a numeric value using localization settings
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
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.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-10-30 00:48:58 +08:00
|
|
|
if use_l10n or (use_l10n is None and settings.USE_L10N):
|
2010-09-27 23:25:08 +08:00
|
|
|
lang = get_language()
|
|
|
|
else:
|
|
|
|
lang = None
|
2009-12-23 01:58:49 +08:00
|
|
|
return numberformat.format(
|
|
|
|
value,
|
2010-10-30 00:48:58 +08:00
|
|
|
get_format('DECIMAL_SEPARATOR', lang, use_l10n=use_l10n),
|
2009-12-23 01:58:49 +08:00
|
|
|
decimal_pos,
|
2010-10-30 00:48:58 +08:00
|
|
|
get_format('NUMBER_GROUPING', lang, use_l10n=use_l10n),
|
|
|
|
get_format('THOUSAND_SEPARATOR', lang, use_l10n=use_l10n),
|
2009-12-23 01:58:49 +08:00
|
|
|
)
|
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def localize(value, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2009-12-31 06:11:48 +08:00
|
|
|
Checks if value is a localizable type (date, number...) and returns it
|
2010-10-30 00:48:58 +08:00
|
|
|
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.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-09-28 00:21:16 +08:00
|
|
|
if isinstance(value, (decimal.Decimal, float, int, long)):
|
2010-10-30 00:48:58 +08:00
|
|
|
return number_format(value, use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
elif isinstance(value, datetime.datetime):
|
2010-10-30 00:48:58 +08:00
|
|
|
return date_format(value, 'DATETIME_FORMAT', use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
elif isinstance(value, datetime.date):
|
2010-10-30 00:48:58 +08:00
|
|
|
return date_format(value, use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
elif isinstance(value, datetime.time):
|
2010-10-30 00:48:58 +08:00
|
|
|
return time_format(value, 'TIME_FORMAT', use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
else:
|
|
|
|
return value
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2009-12-31 06:11:48 +08:00
|
|
|
def localize_input(value, default=None):
|
|
|
|
"""
|
|
|
|
Checks if an input value is a localizable type and returns it
|
|
|
|
formatted with the appropriate formatting string of the current locale.
|
|
|
|
"""
|
2010-09-28 00:21:16 +08:00
|
|
|
if isinstance(value, (decimal.Decimal, float, int, long)):
|
2010-02-05 08:44:35 +08:00
|
|
|
return number_format(value)
|
2010-09-27 23:25:08 +08:00
|
|
|
elif isinstance(value, datetime.datetime):
|
2009-12-31 06:11:48 +08:00
|
|
|
value = datetime_safe.new_datetime(value)
|
|
|
|
format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
|
|
|
|
return value.strftime(format)
|
|
|
|
elif isinstance(value, datetime.date):
|
|
|
|
value = datetime_safe.new_date(value)
|
|
|
|
format = smart_str(default or get_format('DATE_INPUT_FORMATS')[0])
|
|
|
|
return value.strftime(format)
|
|
|
|
elif isinstance(value, datetime.time):
|
|
|
|
format = smart_str(default or get_format('TIME_INPUT_FORMATS')[0])
|
|
|
|
return value.strftime(format)
|
|
|
|
return value
|
2010-03-01 18:19:24 +08:00
|
|
|
|
|
|
|
def sanitize_separators(value):
|
|
|
|
"""
|
|
|
|
Sanitizes a value according to the current decimal and
|
|
|
|
thousand separator setting. Used with form field input.
|
|
|
|
"""
|
|
|
|
if settings.USE_L10N:
|
|
|
|
decimal_separator = get_format('DECIMAL_SEPARATOR')
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
parts = []
|
|
|
|
if decimal_separator in value:
|
|
|
|
value, decimals = value.split(decimal_separator, 1)
|
|
|
|
parts.append(decimals)
|
|
|
|
if settings.USE_THOUSAND_SEPARATOR:
|
|
|
|
parts.append(value.replace(get_format('THOUSAND_SEPARATOR'), ''))
|
|
|
|
else:
|
|
|
|
parts.append(value)
|
|
|
|
value = '.'.join(reversed(parts))
|
|
|
|
return value
|