For a specific format type, return the format for the current language (locale). Default to the format in the settings. format_type is the name of the format, e.g. 'DATE_FORMAT'. If use_l10n is provided and is not None, it forces the value to be localized (or not), otherwise it
(format_type, lang=None, use_l10n=None)
| 98 | |
| 99 | |
| 100 | def get_format(format_type, lang=None, use_l10n=None): |
| 101 | """ |
| 102 | For a specific format type, return the format for the current |
| 103 | language (locale). Default to the format in the settings. |
| 104 | format_type is the name of the format, e.g. 'DATE_FORMAT'. |
| 105 | |
| 106 | If use_l10n is provided and is not None, it forces the value to |
| 107 | be localized (or not), otherwise it's always localized. |
| 108 | """ |
| 109 | if use_l10n is None: |
| 110 | use_l10n = True |
| 111 | if use_l10n and lang is None: |
| 112 | lang = get_language() |
| 113 | format_type = str(format_type) # format_type may be lazy. |
| 114 | cache_key = (format_type, lang) |
| 115 | try: |
| 116 | return _format_cache[cache_key] |
| 117 | except KeyError: |
| 118 | pass |
| 119 | |
| 120 | # The requested format_type has not been cached yet. Try to find it in any |
| 121 | # of the format_modules for the given lang if l10n is enabled. If it's not |
| 122 | # there or if l10n is disabled, fall back to the project settings. |
| 123 | val = None |
| 124 | if use_l10n: |
| 125 | for module in get_format_modules(lang): |
| 126 | val = getattr(module, format_type, None) |
| 127 | if val is not None: |
| 128 | break |
| 129 | if val is None: |
| 130 | if format_type not in FORMAT_SETTINGS: |
| 131 | return format_type |
| 132 | val = getattr(settings, format_type) |
| 133 | elif format_type in ISO_INPUT_FORMATS: |
| 134 | # If a list of input formats from one of the format_modules was |
| 135 | # retrieved, make sure the ISO_INPUT_FORMATS are in this list. |
| 136 | val = list(val) |
| 137 | for iso_input in ISO_INPUT_FORMATS.get(format_type, ()): |
| 138 | if iso_input not in val: |
| 139 | val.append(iso_input) |
| 140 | _format_cache[cache_key] = val |
| 141 | return val |
| 142 | |
| 143 | |
| 144 | get_format_lazy = lazy(get_format, str, list, tuple) |