✨ feat(i18n): implement pluralization logic (#277)
Co-authored-by: welpo <welpo@users.noreply.github.com>main
parent
daef40338c
commit
c4893d4fdd
File diff suppressed because one or more lines are too long
@ -1,3 +1,72 @@
|
|||||||
{% macro translate(key, language_strings="", default="") %}
|
{#- Dynamically selects the appropriate translation key based on the provided `number` and `lang` context.
|
||||||
{{- language_strings[key] | default(value=default) | safe -}}
|
If a `number` is provided, the macro will attempt to pluralize the translation key based on the language's rules.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `key`: The base key for the translation string, matching the i18n files. Example: `results` to get `zero_results`, `one_results`, `many_results`, etc.
|
||||||
|
- `number`: Optional. The numerical value associated with the key.
|
||||||
|
- `language_strings`: A dictionary containing the translation strings.
|
||||||
|
- `default`: A default string to use if no translation is found for the key.
|
||||||
|
- `replace`: Optional. If `true`, the macro will replace the `$NUMBER` placeholder in the translation string with the provided `number`.
|
||||||
|
|
||||||
|
The macro supports special pluralization rules for:
|
||||||
|
- Arabic (`ar`): Has unique forms for zero, one, two, few, and many.
|
||||||
|
- Slavic languages: Pluralization depends on the last digit of the number, with exceptions for numbers ending in 11-14.
|
||||||
|
|
||||||
|
NOTE: If the logic for pluralization is modified, it needs to be replicated on the JavaScript search.
|
||||||
|
Files: static/js/searchElasticlunr.js and its minified version at static/js/searchElasticlunr.min.js
|
||||||
|
Function name: getPluralizationKey -#}
|
||||||
|
{% macro translate(key, number=-1, language_strings="", default="", replace=true) %}
|
||||||
|
{%- set slavic_plural_languages = ["uk", "be", "bs", "hr", "ru", "sr"] -%}
|
||||||
|
|
||||||
|
{%- set key_prefix = "" -%}
|
||||||
|
{#- `zero_` and `one_` are common cases. We handle "many" (plural) later, after language-specific pluralization -#}
|
||||||
|
{%- if number == 0 -%}
|
||||||
|
{%- set key_prefix = "zero_" -%}
|
||||||
|
{%- elif number == 1 -%}
|
||||||
|
{%- set key_prefix = "one_" -%}
|
||||||
|
{%- endif -%}
|
||||||
|
|
||||||
|
{#- Pluralization -#}
|
||||||
|
{%- if number != -1 and key_prefix == "" -%}
|
||||||
|
{#- Arabic -#}
|
||||||
|
{%- if lang == "ar" -%}
|
||||||
|
{%- set modulo = number % 100 -%}
|
||||||
|
{%- if number == 2 -%}
|
||||||
|
{%- set key_prefix = "two_" -%}
|
||||||
|
{%- elif modulo >= 3 and modulo <= 10 -%}
|
||||||
|
{%- set key_prefix = "few_" -%}
|
||||||
|
{%- else -%}
|
||||||
|
{%- set key_prefix = "many_" -%}
|
||||||
|
{%- endif -%}
|
||||||
|
{#- Slavic languages like Russian or Ukrainian -#}
|
||||||
|
{%- elif lang in slavic_plural_languages -%}
|
||||||
|
{%- set modulo10 = number % 10 -%}
|
||||||
|
{%- set modulo100 = number % 100 -%}
|
||||||
|
{%- if modulo10 == 1 and modulo100 != 11 -%}
|
||||||
|
{%- set key_prefix = "one_" -%}
|
||||||
|
{%- elif modulo10 >= 2 and modulo10 <= 4 -%}
|
||||||
|
{%- if modulo100 >= 12 and modulo100 <= 14 -%}
|
||||||
|
{%- set key_prefix = "many_" -%}
|
||||||
|
{%- else -%}
|
||||||
|
{%- set key_prefix = "few_" -%}
|
||||||
|
{%- endif -%}
|
||||||
|
{%- else -%}
|
||||||
|
{%- set key_prefix = "many_" -%}
|
||||||
|
{%- endif -%}
|
||||||
|
{%- else -%}
|
||||||
|
{#- Default pluralization -#}
|
||||||
|
{#- Zero and one are already handled -#}
|
||||||
|
{%- set key_prefix = "many_" -%}
|
||||||
|
{%- endif -%}
|
||||||
|
{%- endif -%}
|
||||||
|
|
||||||
|
{#- Translated string -#}
|
||||||
|
{%- set final_key = key_prefix ~ key -%}
|
||||||
|
{%- set translated_text = language_strings[final_key] | default(value=default) | safe -%}
|
||||||
|
|
||||||
|
{#- Replace $NUMBER with the number -#}
|
||||||
|
{%- if replace -%}
|
||||||
|
{%- set translated_text = translated_text | replace(from="$NUMBER", to=number | as_str) -%}
|
||||||
|
{%- endif -%}
|
||||||
|
{{- translated_text -}}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
Loading…
Reference in New Issue