diff options
| author | Claude Paroz <claude@2xlibre.net> | 2021-09-09 07:42:05 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-09-14 12:05:43 +0200 |
| commit | 676bd084f2509f4201561d5c77ed4ecbd157bfa0 (patch) | |
| tree | bc8dfe6748a6bfc5fe8c728a5f825dfff575cb56 /django | |
| parent | 04e023e38331d6717af1cbd8da4926af612f7831 (diff) | |
Fixed #32873 -- Deprecated settings.USE_L10N.
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'django')
| -rw-r--r-- | django/conf/__init__.py | 34 | ||||
| -rw-r--r-- | django/conf/global_settings.py | 2 | ||||
| -rw-r--r-- | django/conf/locale/en/formats.py | 32 | ||||
| -rw-r--r-- | django/conf/project_template/project_name/settings.py-tpl | 2 | ||||
| -rw-r--r-- | django/utils/formats.py | 12 |
5 files changed, 72 insertions, 10 deletions
diff --git a/django/conf/__init__.py b/django/conf/__init__.py index 628b1f7e4d..f8018f723b 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -9,9 +9,11 @@ for a list of all possible variables. import importlib import os import time +import traceback import warnings from pathlib import Path +import django from django.conf import global_settings from django.core.exceptions import ImproperlyConfigured from django.utils.deprecation import RemovedInDjango50Warning @@ -19,6 +21,12 @@ from django.utils.functional import LazyObject, empty ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE" +USE_L10N_DEPRECATED_MSG = ( + 'The USE_L10N setting is deprecated. Starting with Django 5.0, localized ' + 'formatting of data will always be enabled. For example Django will ' + 'display numbers and dates using the format of the current locale.' +) + class SettingsReference(str): """ @@ -129,6 +137,27 @@ class LazySettings(LazyObject): """Return True if the settings have already been configured.""" return self._wrapped is not empty + @property + def USE_L10N(self): + stack = traceback.extract_stack() + # Show a warning if the setting is used outside of Django. + # Stack index: -1 this line, -2 the caller. + filename, _, _, _ = stack[-2] + if not filename.startswith(os.path.dirname(django.__file__)): + warnings.warn( + USE_L10N_DEPRECATED_MSG, + RemovedInDjango50Warning, + stacklevel=2, + ) + return self.__getattr__('USE_L10N') + + # RemovedInDjango50Warning. + @property + def _USE_L10N_INTERNAL(self): + # Special hook to avoid checking a traceback in internal use on hot + # paths. + return self.__getattr__('USE_L10N') + class Settings: def __init__(self, settings_module): @@ -179,6 +208,9 @@ class Settings: os.environ['TZ'] = self.TIME_ZONE time.tzset() + if self.is_overridden('USE_L10N'): + warnings.warn(USE_L10N_DEPRECATED_MSG, RemovedInDjango50Warning) + def is_overridden(self, setting): return setting in self._explicit_settings @@ -210,6 +242,8 @@ class UserSettingsHolder: def __setattr__(self, name, value): self._deleted.discard(name) + if name == 'USE_L10N': + warnings.warn(USE_L10N_DEPRECATED_MSG, RemovedInDjango50Warning) super().__setattr__(name, value) def __delattr__(self, name): diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index b9541d81b5..ab69316314 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -167,7 +167,7 @@ LANGUAGE_COOKIE_SAMESITE = None # If you set this to True, Django will format dates, numbers and calendars # according to user current locale. -USE_L10N = False +USE_L10N = True # Not-necessarily-technical managers of the site. They get broken link # notifications and other various emails. diff --git a/django/conf/locale/en/formats.py b/django/conf/locale/en/formats.py index ccace3d2b3..f5af6509c6 100644 --- a/django/conf/locale/en/formats.py +++ b/django/conf/locale/en/formats.py @@ -2,24 +2,36 @@ # # The *_FORMAT strings use the Django date format syntax, # see https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date + +# Formatting for date objects. DATE_FORMAT = 'N j, Y' +# Formatting for time objects. TIME_FORMAT = 'P' +# Formatting for datetime objects. DATETIME_FORMAT = 'N j, Y, P' +# Formatting for date objects when only the year and month are relevant. YEAR_MONTH_FORMAT = 'F Y' +# Formatting for date objects when only the month and day are relevant. MONTH_DAY_FORMAT = 'F j' +# Short formatting for date objects. SHORT_DATE_FORMAT = 'm/d/Y' +# Short formatting for datetime objects. SHORT_DATETIME_FORMAT = 'm/d/Y P' -FIRST_DAY_OF_WEEK = 0 # Sunday +# First day of week, to be used on calendars. +# 0 means Sunday, 1 means Monday... +FIRST_DAY_OF_WEEK = 0 +# Formats to be used when parsing dates from input boxes, in order. # The *_INPUT_FORMATS strings use the Python strftime format syntax, # see https://docs.python.org/library/datetime.html#strftime-strptime-behavior +# Note that these format strings are different from the ones to display dates. # Kept ISO formats as they are in first position DATE_INPUT_FORMATS = [ '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06' - # '%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006' - # '%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006' - # '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006' - # '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006' + '%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006' + '%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006' + '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006' + '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006' ] DATETIME_INPUT_FORMATS = [ '%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' @@ -32,6 +44,16 @@ DATETIME_INPUT_FORMATS = [ '%m/%d/%y %H:%M:%S.%f', # '10/25/06 14:30:59.000200' '%m/%d/%y %H:%M', # '10/25/06 14:30' ] +TIME_INPUT_FORMATS = [ + '%H:%M:%S', # '14:30:59' + '%H:%M:%S.%f', # '14:30:59.000200' + '%H:%M', # '14:30' +] + +# Decimal separator symbol. DECIMAL_SEPARATOR = '.' +# Thousand separator symbol. THOUSAND_SEPARATOR = ',' +# Number of digits that will be together, when splitting them by +# THOUSAND_SEPARATOR. 0 means no grouping, 3 means splitting by thousands. NUMBER_GROUPING = 3 diff --git a/django/conf/project_template/project_name/settings.py-tpl b/django/conf/project_template/project_name/settings.py-tpl index e949752df4..3b6caab333 100644 --- a/django/conf/project_template/project_name/settings.py-tpl +++ b/django/conf/project_template/project_name/settings.py-tpl @@ -109,8 +109,6 @@ TIME_ZONE = 'UTC' USE_I18N = True -USE_L10N = True - USE_TZ = True diff --git a/django/utils/formats.py b/django/utils/formats.py index cffbb34398..781d334a8e 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -104,7 +104,11 @@ def get_format(format_type, lang=None, use_l10n=None): If use_l10n is provided and is not None, it forces the value to be localized (or not), overriding the value of settings.USE_L10N. """ - use_l10n = use_l10n or (use_l10n is None and settings.USE_L10N) + use_l10n = use_l10n or (use_l10n is None and ( + settings._USE_L10N_INTERNAL + if hasattr(settings, '_USE_L10N_INTERNAL') + else settings.USE_L10N + )) if use_l10n and lang is None: lang = get_language() cache_key = (format_type, lang) @@ -168,7 +172,11 @@ def number_format(value, decimal_pos=None, use_l10n=None, force_grouping=False): If use_l10n is provided and is not None, it forces the value to be localized (or not), overriding the value of settings.USE_L10N. """ - use_l10n = use_l10n or (use_l10n is None and settings.USE_L10N) + use_l10n = use_l10n or (use_l10n is None and ( + settings._USE_L10N_INTERNAL + if hasattr(settings, '_USE_L10N_INTERNAL') + else settings.USE_L10N + )) lang = get_language() if use_l10n else None return numberformat.format( value, |
