Skip to content

Commit

Permalink
improve the pluralize macro
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAwiteb committed Feb 12, 2024
1 parent b6aa219 commit 62fc7de
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions templates/macros/pluralize.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
{% macro pluralize(language, number) %}
{%- set key_prefix = "" -%}

{# Arabic-specific pluralization rules #}
{%- if language == "ar" -%}
{# Arabic-specific pluralization rules #}
{%- set modulo = number % 100 -%}
{%- if number > 100 -%}
{%- if modulo == 0 or modulo == 1 or modulo == 2 -%}
{%- set key_prefix = "special_" -%} {# Sixth form for numbers above 100 ending with 0, 1, or 2 #}
{%- endif -%}
{%- elif number == 0 -%}
{%- set key_prefix = "zero_" -%} {# First form for 0 #}
{%- elif number == 1 -%}
{%- set key_prefix = "one_" -%} {# Second form for 1 #}
{%- elif number == 2 -%}
{%- set key_prefix = "two_" -%} {# Third form for 2 #}
{%- elif modulo >= 3 and modulo <= 10 -%}
{%- set key_prefix = "few_" -%} {# Fourth form for numbers ending 3-10 #}
{%- elif modulo >= 11 and modulo <= 99 -%}
{%- set key_prefix = "many_" -%} {# Fifth form for numbers ending 11-99 #}

{%- if number == 0 -%}
{%- set key_prefix = "zero_" -%}
{%- elif number == 1 %}
{%- set key_prefix = "one_" -%}
{%- elif number == 2 %}
{%- set key_prefix = "two_" -%}
{%- elif number >= 3 and number <= 10 or modulo >= 3 and modulo <= 10 %}
{%- set key_prefix = "few_" -%}
{%- else -%}
{%- set key_prefix = "other_" -%} {# Catch-all for any other cases #}
{%- set key_prefix = "many_" -%}
{%- endif -%}

{# Other languages #}
{%- else -%}
{# Default pluralization logic for languages other than Arabic #}
{%- if number == 1 -%}
{%- set key_prefix = "one_" -%}
{%- else -%}
{%- set key_prefix = "many_" -%}
{%- endif -%}
{%- endif -%}
{{- key_prefix -}}

{{-key_prefix-}}
{% endmacro %}

0 comments on commit 62fc7de

Please sign in to comment.