MCPcopy
hub / github.com/django/django / localize

Function localize

django/utils/formats.py:193–215  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

191
192
193def 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
218def localize_input(value, default=None):

Callers 4

render_value_in_contextFunction · 0.90
test_l10n_enabledMethod · 0.90

Calls 3

number_formatFunction · 0.85
date_formatFunction · 0.85
time_formatFunction · 0.70

Tested by 3

test_l10n_enabledMethod · 0.72