Check if value is a localizable type (date, number...) and return it formatted as a string using current locale format. If use_l10n is provided and is not None, it forces the value to be localized (or not), otherwise it's always localized.
(value, use_l10n=None)
| 191 | |
| 192 | |
| 193 | def localize(value, use_l10n=None): |
| 194 | """ |
| 195 | Check if value is a localizable type (date, number...) and return it |
| 196 | formatted as a string using current locale format. |
| 197 | |
| 198 | If use_l10n is provided and is not None, it forces the value to |
| 199 | be localized (or not), otherwise it's always localized. |
| 200 | """ |
| 201 | if isinstance(value, str): # Handle strings first for performance reasons. |
| 202 | return value |
| 203 | elif isinstance(value, bool): # Make sure booleans don't get treated as numbers |
| 204 | return str(value) |
| 205 | elif isinstance(value, (decimal.Decimal, float, int)): |
| 206 | if use_l10n is False: |
| 207 | return str(value) |
| 208 | return number_format(value, use_l10n=use_l10n) |
| 209 | elif isinstance(value, datetime.datetime): |
| 210 | return date_format(value, "DATETIME_FORMAT", use_l10n=use_l10n) |
| 211 | elif isinstance(value, datetime.date): |
| 212 | return date_format(value, use_l10n=use_l10n) |
| 213 | elif isinstance(value, datetime.time): |
| 214 | return time_format(value, use_l10n=use_l10n) |
| 215 | return value |
| 216 | |
| 217 | |
| 218 | def localize_input(value, default=None): |