Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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
  • Loading branch information...
commit a25d53191c2ad869ba688da77d04c78a6c7b4640 1 parent f973c57
Marc garcia-marc authored
23 django/conf/global_settings.py
View
@@ -103,6 +103,10 @@
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 @@
# 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
0  django/conf/locale/__init__.py
View
No changes.
0  django/conf/locale/ca/__init__.py
View
No changes.
18 django/conf/locale/ca/formats.py
View
@@ -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 = '.'
+
+
0  django/conf/locale/en/__init__.py
View
No changes.
18 django/conf/locale/en/formats.py
View
@@ -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 = ','
+
+
2  django/contrib/admin/templates/admin/object_history.html
View
@@ -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>
10 django/contrib/admin/templatetags/admin_list.py
View
@@ -7,7 +7,8 @@
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]))
5 django/contrib/databrowse/datastructures.py
View
@@ -6,7 +6,6 @@
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 @@ def values(self):
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):
40 django/utils/formats.py
View
@@ -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
+
9 django/utils/translation/trans_null.py
View
@@ -18,10 +18,10 @@ def ungettext(singular, plural, number):
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)
72 django/utils/translation/trans_real.py
View
@@ -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
+
9 docs/ref/settings.txt
View
@@ -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
9 tests/regressiontests/i18n/tests.py
View
@@ -64,6 +64,15 @@
'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__ = {
Please sign in to comment.
Something went wrong with that request. Please try again.