diff options
| author | Marc Garcia <garcia.marc@gmail.com> | 2009-06-08 13:46:06 +0000 |
|---|---|---|
| committer | Marc Garcia <garcia.marc@gmail.com> | 2009-06-08 13:46:06 +0000 |
| commit | a25d53191c2ad869ba688da77d04c78a6c7b4640 (patch) | |
| tree | 6261b30bad094066296df97319770f7ae73d4c90 | |
| parent | f973c57aaac3e918ac0234acbbbc86834ccfca86 (diff) | |
[soc2009/i18n-improvements] Created a specific function to get locale formats (to avoid using gettext), and updated the code that used gettext.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/i18n-improvements@10956 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/conf/global_settings.py | 23 | ||||
| -rw-r--r-- | django/conf/locale/__init__.py | 0 | ||||
| -rw-r--r-- | django/conf/locale/ca/__init__.py | 0 | ||||
| -rw-r--r-- | django/conf/locale/ca/formats.py | 18 | ||||
| -rw-r--r-- | django/conf/locale/en/__init__.py | 0 | ||||
| -rw-r--r-- | django/conf/locale/en/formats.py | 18 | ||||
| -rw-r--r-- | django/contrib/admin/templates/admin/object_history.html | 2 | ||||
| -rw-r--r-- | django/contrib/admin/templatetags/admin_list.py | 10 | ||||
| -rw-r--r-- | django/contrib/databrowse/datastructures.py | 5 | ||||
| -rw-r--r-- | django/utils/formats.py | 40 | ||||
| -rw-r--r-- | django/utils/translation/trans_null.py | 9 | ||||
| -rw-r--r-- | django/utils/translation/trans_real.py | 72 | ||||
| -rw-r--r-- | docs/ref/settings.txt | 9 | ||||
| -rw-r--r-- | tests/regressiontests/i18n/tests.py | 9 |
14 files changed, 174 insertions, 41 deletions
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 99fc72e468..a8f180a426 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -103,6 +103,10 @@ USE_I18N = True LOCALE_PATHS = () LANGUAGE_COOKIE_NAME = 'django_language' +# If you set this to True, Django will format dates, numbers and calendars +# according to user current locale +USE_FORMAT_I18N = False + # Not-necessarily-technical managers of the site. They get broken link # notifications and other various e-mails. MANAGERS = ADMINS @@ -277,6 +281,25 @@ YEAR_MONTH_FORMAT = 'F Y' # http://docs.djangoproject.com/en/dev/ref/templates/builtins/#now MONTH_DAY_FORMAT = 'F j' +# Default shortformatting for date objects. See all available format strings here: +# http://docs.djangoproject.com/en/dev/ref/templates/builtins/#now +SHORT_DATE_FORMAT = 'N j, Y' + +# Default short formatting for datetime objects. +# See all available format strings here: +# http://docs.djangoproject.com/en/dev/ref/templates/builtins/#now +SHORT_DATETIME_FORMAT = 'N j, Y, P' + +# Default first day of week, to be used on calendars +# 0 means Sunday +FIRST_DAY_OF_WEEK = 0 + +# Default decimal separator symbol +DECIMAL_SEPARATOR = '.' + +# Default thousand separator symbol +THOUSAND_SEPARATOR = ',' + # Do you want to manage transactions manually? # Hint: you really don't! TRANSACTIONS_MANAGED = False diff --git a/django/conf/locale/__init__.py b/django/conf/locale/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/django/conf/locale/__init__.py diff --git a/django/conf/locale/ca/__init__.py b/django/conf/locale/ca/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/django/conf/locale/ca/__init__.py diff --git a/django/conf/locale/ca/formats.py b/django/conf/locale/ca/formats.py new file mode 100644 index 0000000000..414fe86e26 --- /dev/null +++ b/django/conf/locale/ca/formats.py @@ -0,0 +1,18 @@ +# This file is distributed under the same license as the Django package. +# + +DATE_FORMAT = 'j \de F \de Y' +DATETIME_FORMAT = 'j \de F \de Y \\a \le\s H:i' +TIME_FORMAT = 'P' +YEAR_MONTH_FORMAT = 'F \de Y' +MONTH_DAY_FORMAT = 'j \de F' + +SHORT_DATE_FORMAT = 'd/m/Y' +SHORT_DATETIME_FORMAT = 'd/m/Y H:i' + +FIRST_DAY_OF_WEEK = 1 # 0 means Sunday + +DECIMAL_SEPARATOR = ',' +THOUSAND_SEPARATOR = '.' + + diff --git a/django/conf/locale/en/__init__.py b/django/conf/locale/en/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/django/conf/locale/en/__init__.py diff --git a/django/conf/locale/en/formats.py b/django/conf/locale/en/formats.py new file mode 100644 index 0000000000..670245c3ce --- /dev/null +++ b/django/conf/locale/en/formats.py @@ -0,0 +1,18 @@ +# This file is distributed under the same license as the Django package. +# + +DATE_FORMAT = 'N j, Y' +DATETIME_FORMAT = 'N j, Y, P' +TIME_FORMAT = 'P' +YEAR_MONTH_FORMAT = 'F Y' +MONTH_DAY_FORMAT = 'F j' + +SHORT_DATE_FORMAT = 'm/d/Y' +SHORT_DATETIME_FORMAT = 'm/d/Y h:i A' + +FIRST_DAY_OF_WEEK = 0 # 0 means Sunday + +DECIMAL_SEPARATOR = '.' +THOUSAND_SEPARATOR = ',' + + diff --git a/django/contrib/admin/templates/admin/object_history.html b/django/contrib/admin/templates/admin/object_history.html index 38b1c34ac8..9e6223acb9 100644 --- a/django/contrib/admin/templates/admin/object_history.html +++ b/django/contrib/admin/templates/admin/object_history.html @@ -27,7 +27,7 @@ <tbody> {% for action in action_list %} <tr> - <th scope="row">{{ action.action_time|date:_("DATETIME_FORMAT") }}</th> + <th scope="row">{{ action.action_time|date }}</th> <td>{{ action.user.username }}{% if action.user.get_full_name %} ({{ action.user.get_full_name }}){% endif %}</td> <td>{{ action.change_message }}</td> </tr> diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index d97c36a207..4b56652b45 100644 --- a/django/contrib/admin/templatetags/admin_list.py +++ b/django/contrib/admin/templatetags/admin_list.py @@ -7,7 +7,8 @@ from django.utils import dateformat from django.utils.html import escape, conditional_escape from django.utils.text import capfirst from django.utils.safestring import mark_safe -from django.utils.translation import get_date_formats, get_partial_date_formats, ugettext as _ +from django.utils.translation import ugettext as _ +from django.utils.formats import getformat from django.utils.encoding import smart_unicode, smart_str, force_unicode from django.template import Library import datetime @@ -184,7 +185,9 @@ def items_for_result(cl, result, form): # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance(f, models.TimeField): if field_val: - (date_format, datetime_format, time_format) = get_date_formats() + date_format = getformat('DATE_FORMAT') + datetime_format = getformat('DATETIME_FORMAT') + time_format = getformat('TIME_FORMAT') if isinstance(f, models.DateTimeField): result_repr = capfirst(dateformat.format(field_val, datetime_format)) elif isinstance(f, models.TimeField): @@ -263,7 +266,8 @@ def date_hierarchy(cl): year_lookup = cl.params.get(year_field) month_lookup = cl.params.get(month_field) day_lookup = cl.params.get(day_field) - year_month_format, month_day_format = get_partial_date_formats() + year_month_format = getformat('YEAR_MONTH_FORMAT') + month_day_format = getformat('MONTH_DAY_FORMAT') link = lambda d: mark_safe(cl.get_query_string(d, [field_generic])) diff --git a/django/contrib/databrowse/datastructures.py b/django/contrib/databrowse/datastructures.py index 5fdbdbe134..65e2fa49dd 100644 --- a/django/contrib/databrowse/datastructures.py +++ b/django/contrib/databrowse/datastructures.py @@ -6,7 +6,6 @@ convenience functionality and permalink functions for the databrowse app. from django.db import models from django.utils import dateformat from django.utils.text import capfirst -from django.utils.translation import get_date_formats from django.utils.encoding import smart_unicode, smart_str, iri_to_uri from django.utils.safestring import mark_safe from django.db.models.query import QuerySet @@ -156,7 +155,9 @@ class EasyInstanceField(object): objs = dict(self.field.choices).get(self.raw_value, EMPTY_VALUE) elif isinstance(self.field, models.DateField) or isinstance(self.field, models.TimeField): if self.raw_value: - date_format, datetime_format, time_format = get_date_formats() + date_format = getformat('DATE_FORMAT') + datetime_format = getformat('DATETIME_FORMAT') + time_format = getformat('TIME_FORMAT') if isinstance(self.field, models.DateTimeField): objs = capfirst(dateformat.format(self.raw_value, datetime_format)) elif isinstance(self.field, models.TimeField): diff --git a/django/utils/formats.py b/django/utils/formats.py new file mode 100644 index 0000000000..5b9456aa96 --- /dev/null +++ b/django/utils/formats.py @@ -0,0 +1,40 @@ +from django.utils.importlib import import_module + +def getformat_null(format_type): + """ + For a specific format type, returns the default format as + set on the settings. + format_type is the name of the format, for example 'DATE_FORMAT' + """ + from django.conf import settings + return getattr(settings, format_type) + +def getformat_real(format_type): + """ + For a specific format type, returns the format for the + current language (locale) defaulting to the format on settings. + format_type is the name of the format, for example 'DATE_FORMAT' + """ + from django.utils.translation import get_language + import_formats = lambda s: import_module('.formats', 'django.conf.locale.%s' % s) + tmp = import_formats('ca') + format = None + try: + module = import_formats(get_language()) + except ImportError: + pass + else: + try: + format = getattr(module, format_type) + except AttributeError: + pass + return format or getformat_null(format_type) + +# getformat will just return the value on setings if +# we don't use i18n in our project +from django.conf import settings +if settings.USE_I18N and settings.USE_FORMAT_I18N: + getformat = getformat_real +else: + getformat = getformat_null + diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index 98c6de6197..7a5713985c 100644 --- a/django/utils/translation/trans_null.py +++ b/django/utils/translation/trans_null.py @@ -18,10 +18,10 @@ activate = lambda x: None deactivate = deactivate_all = lambda: None get_language = lambda: settings.LANGUAGE_CODE get_language_bidi = lambda: settings.LANGUAGE_CODE in settings.LANGUAGES_BIDI -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) check_for_language = lambda x: True +# date formats shouldn't be used using gettext anymore. This +# is kept for backward compatibility TECHNICAL_ID_MAP = { "DATE_WITH_TIME_FULL": settings.DATETIME_FORMAT, "DATE_FORMAT": settings.DATE_FORMAT, @@ -51,3 +51,8 @@ def to_locale(language): def get_language_from_request(request): return settings.LANGUAGE_CODE + +# get_date_formats and get_partial_date_formats aren't used anymore from django +# itself, and are kept for backward compatibility. +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/trans_real.py b/django/utils/translation/trans_real.py index 48ed7cc885..27edc081c8 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -389,39 +389,6 @@ def get_language_from_request(request): return settings.LANGUAGE_CODE -def get_date_formats(): - """ - Checks whether translation files provide a translation for some technical - message ID to store date and time formats. If it doesn't contain one, the - formats provided in the settings will be used. - """ - from django.conf import settings - date_format = ugettext('DATE_FORMAT') - datetime_format = ugettext('DATETIME_FORMAT') - time_format = ugettext('TIME_FORMAT') - if date_format == 'DATE_FORMAT': - date_format = settings.DATE_FORMAT - if datetime_format == 'DATETIME_FORMAT': - datetime_format = settings.DATETIME_FORMAT - if time_format == 'TIME_FORMAT': - time_format = settings.TIME_FORMAT - return date_format, datetime_format, time_format - -def get_partial_date_formats(): - """ - Checks whether translation files provide a translation for some technical - message ID to store partial date formats. If it doesn't contain one, the - formats provided in the settings will be used. - """ - from django.conf import settings - year_month_format = ugettext('YEAR_MONTH_FORMAT') - month_day_format = ugettext('MONTH_DAY_FORMAT') - if year_month_format == 'YEAR_MONTH_FORMAT': - year_month_format = settings.YEAR_MONTH_FORMAT - if month_day_format == 'MONTH_DAY_FORMAT': - month_day_format = settings.MONTH_DAY_FORMAT - return year_month_format, month_day_format - dot_re = re.compile(r'\S') def blankout(src, char): """ @@ -537,3 +504,42 @@ def parse_accept_lang_header(lang_string): result.append((lang, priority)) result.sort(lambda x, y: -cmp(x[1], y[1])) return result + +# get_date_formats and get_partial_date_formats aren't used anymore from django +# itself, and are kept for backward compatibility. +# Note that it's also important to keep format names maked for translation, so +# for compatibility we still want to have formats on translation catalogs. That +# makes template code like {{ my_date|date:_('DATE_FORMAT') }} go on working +def get_date_formats(): + """ + Checks whether translation files provide a translation for some technical + message ID to store date and time formats. If it doesn't contain one, the + formats provided in the settings will be used. + """ + from django.conf import settings + date_format = ugettext('DATE_FORMAT') + datetime_format = ugettext('DATETIME_FORMAT') + time_format = ugettext('TIME_FORMAT') + if date_format == 'DATE_FORMAT': + date_format = settings.DATE_FORMAT + if datetime_format == 'DATETIME_FORMAT': + datetime_format = settings.DATETIME_FORMAT + if time_format == 'TIME_FORMAT': + time_format = settings.TIME_FORMAT + return date_format, datetime_format, time_format + +def get_partial_date_formats(): + """ + Checks whether translation files provide a translation for some technical + message ID to store partial date formats. If it doesn't contain one, the + formats provided in the settings will be used. + """ + from django.conf import settings + year_month_format = ugettext('YEAR_MONTH_FORMAT') + month_day_format = ugettext('MONTH_DAY_FORMAT') + if year_month_format == 'YEAR_MONTH_FORMAT': + year_month_format = settings.YEAR_MONTH_FORMAT + if month_day_format == 'MONTH_DAY_FORMAT': + month_day_format = settings.MONTH_DAY_FORMAT + return year_month_format, month_day_format + diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 0cca6fe05d..b5ac651e25 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -303,6 +303,15 @@ be useful for some test setups, and should never be used on a live site. +.. setting:: DECIMAL_SEPARATOR + +DECIMAL_SEPARATOR +----------------- + +Default: ``'.'`` (Dot) + +The default decimal separator used when formatting decimal numbers. + .. setting:: DEFAULT_CHARSET DEFAULT_CHARSET diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 94e792cf54..dc875c6a31 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -64,6 +64,15 @@ status. 'as' >>> print s Password + +>>> from django.utils.formats import getformat +>>> activate('en') +>>> getformat('DATE_FORMAT') +'N j, Y' +>>> activate('ca') +>>> getformat('DATE_FORMAT') +'j \de N \de Y' + """ __test__ = { |
