diff options
author | Jannis Leidel <jannis@leidel.info> | 2009-12-22 17:58:49 +0000 |
---|---|---|
committer | Jannis Leidel <jannis@leidel.info> | 2009-12-22 17:58:49 +0000 |
commit | 9233d0426537615e06b78d28010d17d5a66adf44 (patch) | |
tree | 5c731977b3ef9bd1e660b63c4edf6caa7b6491ad /django/utils/formats.py | |
parent | 6632739e94c6c38b4c5a86cf5c80c48ae50ac49f (diff) | |
download | django-9233d0426537615e06b78d28010d17d5a66adf44.tar.gz |
Fixed #7980 - Improved i18n framework to support locale aware formatting (dates and numbers) and form processing.
Thanks to Marc Garcia for working on this during his Google Summer of Code 2009!
Additionally fixes #1061, #2203, #3940, #5526, #6449, #6231, #6693, #6783, #9366 and #10891.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11964 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/formats.py')
-rw-r--r-- | django/utils/formats.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/django/utils/formats.py b/django/utils/formats.py new file mode 100644 index 0000000000..e18e120b36 --- /dev/null +++ b/django/utils/formats.py @@ -0,0 +1,97 @@ +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 +from django.utils import dateformat +from django.utils import numberformat + +def get_format_modules(): + """ + Returns an iterator over the format modules found in the project and Django + """ + modules = [] + if not check_for_language(get_language()): + return modules + locale = to_locale(get_language()) + if settings.FORMAT_MODULE_PATH: + format_locations = [settings.FORMAT_MODULE_PATH + '.%s'] + else: + format_locations = [] + format_locations.append('django.conf.locale.%s') + for location in format_locations: + for l in (locale, locale.split('_')[0]): + try: + mod = import_module('.formats', location % l) + except ImportError: + pass + else: + # Don't return duplicates + if mod not in modules: + modules.append(mod) + return modules + +def get_format(format_type): + """ + 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 settings.USE_L10N: + for module in get_format_modules(): + try: + return getattr(module, format_type) + except AttributeError: + pass + return getattr(settings, format_type) + +def date_format(value, format=None): + """ + Formats a datetime.date or datetime.datetime object using a + localizable format + """ + return dateformat.format(value, get_format(format or 'DATE_FORMAT')) + +def number_format(value, decimal_pos=None): + """ + Formats a numeric value using localization settings + """ + return numberformat.format( + value, + get_format('DECIMAL_SEPARATOR'), + decimal_pos, + get_format('NUMBER_GROUPING'), + get_format('THOUSAND_SEPARATOR'), + ) + +def localize(value, is_input=False): + """ + Checks value, and if it has a localizable type (date, + number...) it returns the value as a string using + current locale format + """ + if settings.USE_L10N: + if isinstance(value, decimal.Decimal): + return number_format(value) + elif isinstance(value, float): + return number_format(value) + elif isinstance(value, int): + return number_format(value) + elif isinstance(value, datetime.datetime): + if not is_input: + return date_format(value, 'DATETIME_FORMAT') + else: + return value.strftime(get_format('DATETIME_INPUT_FORMATS')[0]) + elif isinstance(value, datetime.date): + if not is_input: + return date_format(value) + else: + return value.strftime(get_format('DATE_INPUT_FORMATS')[0]) + elif isinstance(value, datetime.time): + if not is_input: + return date_format(value, 'TIME_FORMAT') + else: + return value.strftime(get_format('TIME_INPUT_FORMATS')[0]) + return value + |