Commits

garc...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 907860b

[soc2009/i18n-improvements] Created a specific function to get locale formats (to avoid using gettext), and updated the code that used gettext.

  • Participants
  • Parent commits 5335e89
  • Branches soc2009/i18n-improvements

Comments (0)

Files changed (14)

File django/conf/global_settings.py

 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
 # 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

File django/conf/locale/__init__.py

Empty file added.

File django/conf/locale/ca/__init__.py

Empty file added.

File django/conf/locale/ca/formats.py

+# 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 = '.'
+
+

File django/conf/locale/en/__init__.py

Empty file added.

File django/conf/locale/en/formats.py

+# 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 = ','
+
+

File django/contrib/admin/templates/admin/object_history.html

         <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>

File django/contrib/admin/templatetags/admin_list.py

 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
             # 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):
         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]))
 

File django/contrib/databrowse/datastructures.py

 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
             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):

File django/utils/formats.py

+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
+

File django/utils/translation/trans_null.py

 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,
 
 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)

File django/utils/translation/trans_real.py

 
     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):
     """
         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
+

File docs/ref/settings.txt

 site.
 
 
+.. setting:: DECIMAL_SEPARATOR
+
+DECIMAL_SEPARATOR
+-----------------
+
+Default: ``'.'`` (Dot)
+
+The default decimal separator used when formatting decimal numbers.
+
 .. setting:: DEFAULT_CHARSET
 
 DEFAULT_CHARSET

File tests/regressiontests/i18n/tests.py

 '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__ = {