mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-06 05:01:48 +02:00
Slightly rework localize_list
- use gettext only when needed - wrap items globally, to avoid calling item_wrapper everywhere - add comment for translators
This commit is contained in:
parent
2b5c2bbda6
commit
bf7208ad33
1 changed files with 24 additions and 20 deletions
|
@ -311,7 +311,7 @@ def em_surround(string, regex_escape=False):
|
||||||
return f'<em class="font-italic">{string}</em>'
|
return f'<em class="font-italic">{string}</em>'
|
||||||
|
|
||||||
|
|
||||||
def localize_list(list, surround_with_em=True):
|
def localize_list(items, surround_with_em=True):
|
||||||
"""
|
"""
|
||||||
Localize a list, optionally surrounding each item in <em> tags.
|
Localize a list, optionally surrounding each item in <em> tags.
|
||||||
|
|
||||||
|
@ -332,36 +332,40 @@ def localize_list(list, surround_with_em=True):
|
||||||
:param surround_with_em: Optionally surround each object with <em> tags
|
:param surround_with_em: Optionally surround each object with <em> tags
|
||||||
:return: A locally formatted list of objects
|
:return: A locally formatted list of objects
|
||||||
"""
|
"""
|
||||||
list = list[:]
|
|
||||||
two = _("{dual_object_0} and {dual_object_1}")
|
if len(items) == 0:
|
||||||
start = _("{start_object}, {next_object}")
|
return ""
|
||||||
middle = _("{previous_object}, {next_object}")
|
|
||||||
end = _("{previous_object}, and {end_object}")
|
|
||||||
|
|
||||||
item_wrapper = em_surround if surround_with_em else lambda x: x
|
item_wrapper = em_surround if surround_with_em else lambda x: x
|
||||||
|
wrapped_items = list(map(item_wrapper, items))
|
||||||
|
|
||||||
if len(list) == 0:
|
if len(wrapped_items) == 1:
|
||||||
return ""
|
return str(wrapped_items[0])
|
||||||
if len(list) == 1:
|
elif len(wrapped_items) == 2:
|
||||||
return str(item_wrapper(list[0]))
|
# I18N: List with two items only
|
||||||
elif len(list) == 2:
|
return _("{dual_object_0} and {dual_object_1}").format(
|
||||||
return two.format(
|
dual_object_0=wrapped_items[0], dual_object_1=wrapped_items[1]
|
||||||
dual_object_0=item_wrapper(list[0]), dual_object_1=item_wrapper(list[1])
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
output_str = end.format(
|
# I18N: Last two items of a list with more than 3 items
|
||||||
previous_object="{previous_object}", end_object=item_wrapper(list.pop())
|
output_str = _("{previous_object}, and {end_object}").format(
|
||||||
|
previous_object="{previous_object}", end_object=wrapped_items.pop()
|
||||||
)
|
)
|
||||||
while len(list) > 2:
|
# I18N: Two items in a middle of a list with more than 5 objects
|
||||||
|
middle = _("{previous_object}, {next_object}")
|
||||||
|
while len(wrapped_items) > 2:
|
||||||
temp = middle.format(
|
temp = middle.format(
|
||||||
previous_object="{previous_object}",
|
previous_object="{previous_object}",
|
||||||
next_object=item_wrapper(list.pop()),
|
next_object=wrapped_items.pop(),
|
||||||
)
|
)
|
||||||
output_str = output_str.format(previous_object=temp)
|
output_str = output_str.format(previous_object=temp)
|
||||||
|
|
||||||
output_str = output_str.format(previous_object=item_wrapper(list.pop()))
|
output_str = output_str.format(previous_object=wrapped_items.pop())
|
||||||
output_str = start.format(start_object="{start_object}", next_object=output_str)
|
# I18N: First two itmes of a list with more than 3 items
|
||||||
return output_str.format(start_object=item_wrapper(list.pop()))
|
output_str = _("{start_object}, {next_object}").format(
|
||||||
|
start_object="{start_object}", next_object=output_str
|
||||||
|
)
|
||||||
|
return output_str.format(start_object=wrapped_items.pop())
|
||||||
|
|
||||||
|
|
||||||
def render_localized_currency(code, detailed=True):
|
def render_localized_currency(code, detailed=True):
|
||||||
|
|
Loading…
Reference in a new issue