summaryrefslogtreecommitdiff
path: root/templates/macros/translate.html
blob: 1b138ddb1cf0809cfba5a71d00d75f20fb17ad7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{#- Dynamically selects the appropriate translation key based on the provided `number` and `lang` context.
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 %}