Commits

Anonymous committed 04fc88c

[soc2009/i18n] draft for a possible solution on how to localize database fields on templates

  • Participants
  • Parent commits 79fc734
  • Branches soc2009/i18n-improvements

Comments (0)

Files changed (3)

django/db/models/base.py

             # is *not* consumed. We rely on this, so don't change the order
             # without changing the logic.
             for val, field in izip(args, fields_iter):
-                setattr(self, field.attname, val)
+                setattr(self, field.attname, field.localize(val))
         else:
             # Slower, kwargs-ready version.
             for val, field in izip(args, fields_iter):
-                setattr(self, field.attname, val)
+                setattr(self, field.attname, field.localize(val))
                 kwargs.pop(field.name, None)
                 # Maintain compatibility with existing calls.
                 if isinstance(field.rel, ManyToOneRel):

django/db/models/fields/__init__.py

         "Returns the value of this field in the given model instance."
         return getattr(obj, self.attname)
 
+    def localize(self, value):
+        """
+        Returns the value with a localize method that can be used when
+        representing the object as a string, to get the value formatted
+        using the current locale
+        """
+        # FIXME: is there a way to to cast an instance to a class that works for
+        # datetime.datetime objects?
+        import datetime
+        localized_class = type('localized_class', (type(value),), dict(localize=None))
+        if isinstance(value, datetime.date):
+            localized_value = localized_class(value.year, value.month, value.day)
+        elif isinstance(value, datetime.datetime):
+            localized_value = localized_class(value.year, value.month, value.day,
+                value.hour, value.minute, value.second)
+        else:
+            localized_value = localized_class(value)
+
+        localized_value.localize = lambda : self._localize_funct(value)
+        return localized_value
+
+    def _localize_funct(self, value):
+        """
+        Localizable field types should overwrite this method to specify
+        how to localize field representation
+        """
+        return unicode(value)
+
 class AutoField(Field):
     empty_strings_allowed = False
     def __init__(self, *args, **kwargs):
         defaults.update(kwargs)
         return super(DecimalField, self).formfield(**defaults)
 
+    def _localize_funct(self, value):
+        from django.utils.formats import number_format
+        return number_format(value)
+
 class EmailField(CharField):
     def __init__(self, *args, **kwargs):
         kwargs['max_length'] = kwargs.get('max_length', 75)

django/template/__init__.py

                     else:
                         raise
 
-        return current
+        if hasattr(current, 'localize'):
+            return current.localize()
+        else:
+            return current
 
 class Node(object):
     # Set this to True for nodes that must be first in the template (although