Check if an input value is a localizable type and return it formatted with the appropriate formatting string of the current locale.
(value, default=None)
| 216 | |
| 217 | |
| 218 | def localize_input(value, default=None): |
| 219 | """ |
| 220 | Check if an input value is a localizable type and return it |
| 221 | formatted with the appropriate formatting string of the current locale. |
| 222 | """ |
| 223 | if isinstance(value, str): # Handle strings first for performance reasons. |
| 224 | return value |
| 225 | elif isinstance(value, bool): # Don't treat booleans as numbers. |
| 226 | return str(value) |
| 227 | elif isinstance(value, (decimal.Decimal, float, int)): |
| 228 | return number_format(value) |
| 229 | elif isinstance(value, datetime.datetime): |
| 230 | format = default or get_format("DATETIME_INPUT_FORMATS")[0] |
| 231 | format = sanitize_strftime_format(format) |
| 232 | return value.strftime(format) |
| 233 | elif isinstance(value, datetime.date): |
| 234 | format = default or get_format("DATE_INPUT_FORMATS")[0] |
| 235 | format = sanitize_strftime_format(format) |
| 236 | return value.strftime(format) |
| 237 | elif isinstance(value, datetime.time): |
| 238 | format = default or get_format("TIME_INPUT_FORMATS")[0] |
| 239 | return value.strftime(format) |
| 240 | return value |
| 241 | |
| 242 | |
| 243 | @functools.lru_cache |