merge pluralize into translate; replace $NUMBER

This commit is contained in:
welpo 2024-02-13 14:31:00 +01:00
parent 62fc7de8a7
commit 7d41f3ca06
No known key found for this signature in database
GPG key ID: A2F978CF4EC1F5A6
7 changed files with 62 additions and 66 deletions

View file

@ -1,9 +1,3 @@
# Proof of concept for pluralisation:
one_read_minute = "دقيقة واحدة"
two_read_minute = "دقيقتان"
few_read_minute = "$MINUTES دقائق"
many_read_minute = "$MINUTES دقيقة"
# Hello, the Arabic language has many pronouns and words, and each word indicates a different meaning,
# unlike the English language, in which, on the other hand, the word can refer to a person and a group.
# This translation is for individual use, if you are a company or organization, I have put a comment in
@ -54,7 +48,11 @@ go_to_comments = "انتقل إلى التعليقات"
# Post metadata.
draft = "مسودة"
min_read = "دقيقة متوقعة للقراءة"
one_min_read = "دقيقة واحدة"
two_min_read = "دقيقتان"
few_min_read = "$NUMBER دقائق"
many_min_read = "$NUMBER دقيقة"
# TODO: words needs to use $NUMBER and pluralize the string.
words = "كلمة"
last_updated_on = "آخر تحديث كان في"
see_changes = "الإطلاع على التغييرات"

View file

@ -1,8 +1,3 @@
# Proof of concept for pluralisation:
one_read_minute = "$MINUTES min read"
many_read_minute = "$MINUTES min read (plural; it's the same)"
language_name = "English" # Shown in language picker for multi-language sites.
date_locale = "en_GB"
full_stop = "." # Used at the end of a sentence.
@ -48,8 +43,10 @@ go_to_comments = "Go to the comments section"
# Post metadata.
draft = "DRAFT"
min_read = "min read"
words = "words"
one_min_read = "$NUMBER min read"
many_min_read = "$NUMBER min read"
one_word = "$NUMBER word"
many_word = "$NUMBER words"
last_updated_on = "Last updated on"
see_changes = "See changes"

View file

@ -43,8 +43,10 @@ go_to_comments = "Ir a la sección de comentarios"
# Post metadata.
draft = "BORRADOR"
min_read = "min de lectura"
words = "palabras"
one_min_read = "$NUMBER min de lectura"
many_min_read = "$NUMBER min de lectura"
one_word = "$NUMBER palabra"
many_word = "$NUMBER palabras"
last_updated_on = "Última actualización el"
see_changes = "Ver cambios"

View file

@ -1,7 +1,6 @@
{% import "macros/format_date.html" as macros_format_date %}
{% import "macros/list_posts.html" as macros_list_posts %}
{% import "macros/page_header.html" as macros_page_header %}
{% import "macros/pluralize.html" as macros_pluralize %}
{% import "macros/rel_attributes.html" as macros_rel_attributes %}
{% import "macros/settings.html" as macros_settings %}
{% import "macros/table_of_contents.html" as macros_toc %}

View file

@ -1,30 +0,0 @@
{% macro pluralize(language, number) %}
{%- set key_prefix = "" -%}
{# Arabic-specific pluralization rules #}
{%- if language == "ar" -%}
{%- set modulo = number % 100 -%}
{%- 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 = "many_" -%}
{%- endif -%}
{# Other languages #}
{%- else -%}
{%- if number == 1 -%}
{%- set key_prefix = "one_" -%}
{%- else -%}
{%- set key_prefix = "many_" -%}
{%- endif -%}
{%- endif -%}
{{-key_prefix-}}
{% endmacro %}

View file

@ -1,3 +1,40 @@
{% macro translate(key, language_strings="", default="") %}
{{- language_strings[key] | default(value=default) | safe -}}
{% macro translate(key, number="", language_strings="", default="") %}
{%- set key_prefix = "" -%}
{# Pluralization #}
{%- if number == 0 or number -%}
{%- if lang == "ar" -%}
{%- set modulo = number % 100 -%}
{%- if number == 0 -%}
{%- set key_prefix = "zero_" -%}
{%- elif number == 1 -%}
{%- set key_prefix = "one_" -%}
{%- elif number == 2 -%}
{%- set key_prefix = "two_" -%}
{%- elif modulo >= 3 and modulo <= 10 -%}
{%- set key_prefix = "few_" -%}
{%- else -%}
{%- set key_prefix = "many_" -%}
{%- endif -%}
{%- else -%}
{# Default pluralization #}
{%- if number == 1 -%}
{%- set key_prefix = "one_" -%}
{%- else -%}
{%- set key_prefix = "many_" -%}
{%- endif -%}
{%- endif -%}
{%- set final_key = key_prefix ~ key -%}
{%- else -%}
{%- set final_key = key -%}
{%- endif -%}
{# Translated string #}
{%- set translated_text = language_strings[final_key] | default(value=default) | safe -%}
{# Replace $NUMBER with the number #}
{%- if number == 0 or number -%}
{%- set translated_text = translated_text | replace(from="$NUMBER", to=number | as_str) -%}
{%- endif -%}
{{- translated_text -}}
{% endmacro %}

View file

@ -12,12 +12,10 @@
{%- endif -%}
{# Debugging #}
{%- set count = 11 -%} {# Replace this to test #}
{%- set base_key = "read_minute" -%}
{%- set prefix = macros_pluralize::pluralize(language=lang, number=count) -%}
{%- set full_key = prefix ~ base_key -%}
{%- set translated_key = macros_translate::translate(key=full_key, language_strings=language_strings, default="couldn't find the string") -%}
{%- set processed_key = translated_key | replace(from="$MINUTES", to=count | as_str) -%}
{%- set count = 0 -%} {# Replace this to test #}
{%- set base_key = "min_read" -%}
{%- set processed_key = macros_translate::translate(key=base_key, number=count, language_strings=language_strings, default="couldn't find the string") -%}
<table>
<thead>
<tr>
@ -30,22 +28,17 @@
<tr>
<td><code>count</code></td>
<td>{{ count }}</td>
<td>The number of minutes used for testing.</td>
<td>The number of items.</td>
</tr>
<tr>
<td><code>full_key</code></td>
<td>{{ full_key }}</td>
<td>Constructed key for fetching the translation.</td>
</tr>
<tr>
<td><code>translated_key</code></td>
<td>{{ translated_key }}</td>
<td>The raw translation fetched using <code>full_key</code>.</td>
<td><code>key</code></td>
<td>{{ base_key }}</td>
<td>The base translation key used for fetching the translation.</td>
</tr>
<tr>
<td><code>processed_key</code></td>
<td>{{ processed_key }}</td>
<td>The final translation after replacing <code>$MINUTES</code> with the actual count.</td>
<td>The final translation with appropriate pluralization and dynamic content replaced (if applicable).</td>
</tr>
</tbody>
</table>
@ -106,7 +99,7 @@
{# page settings override config settings #}
{% if macros_settings::evaluate_setting_priority(setting="show_reading_time", page=page, default_global_value=true) == "true" %}
{{ separator }} <li title="{{ page.word_count }} {{ macros_translate::translate(key="words", default="words", language_strings=language_strings) }}">{{ page.reading_time }}&nbsp;{{ macros_translate::translate(key="min_read", default="min read", language_strings=language_strings) }}</li>
{{ separator }} <li title="{{ macros_translate::translate(key="word", number=page.word_count, default=page.word_count ~ " words", language_strings=language_strings) }}">{{ macros_translate::translate(key="min_read", number=page.reading_time, default=page.reading_time ~ " min read", language_strings=language_strings) }}</li>
{% endif %}
{%- if page.taxonomies and page.taxonomies.tags -%}