Skip to content

Commit

Permalink
🚧 feat: proof of concept pluralization
Browse files Browse the repository at this point in the history
  • Loading branch information
welpo committed Feb 12, 2024
1 parent 011d24a commit 68e9bf3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ author = "welpo"
generate_feed = true
compile_sass = true
minify_html = true
build_search_index = true
# build_search_index = true

# To translate the entire theme, there must be a file with the same ISO 639-1
# (or IETF BCP 47) Code in the `i18n` folder of your site or the tabi theme
# For example, "i18n/fr.toml" for French or "i18n/zh-Hans.toml" for Simplified Chinese.
# Otherwise the theme will be in English.
# See https://welpo.github.io/tabi/blog/faq-languages/ for more details.
default_language = "en"
default_language = "ar"

taxonomies = [{name = "tags", feed = true}]

Expand Down
6 changes: 6 additions & 0 deletions i18n/ar.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 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
Expand Down
5 changes: 5 additions & 0 deletions i18n/en.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 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.
Expand Down
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% 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 %}
Expand Down
32 changes: 32 additions & 0 deletions templates/macros/pluralize.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% macro pluralize(language="en", number) %}
{%- set key_prefix = "" -%}
{%- 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 #}
{%- else -%}
{%- set key_prefix = "other_" -%} {# Catch-all for any other cases #}
{%- endif -%}
{%- 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 -}}
{% endmacro %}
37 changes: 37 additions & 0 deletions templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,43 @@
{%- endif -%}

{# Debugging #}
{%- set count = 11 -%} {# Replace this to test #}
{%- set base_key = "read_minute" -%}
{%- set prefix = macros_pluralize::pluralize(lang=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) -%}
<table>
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>count</code></td>
<td>{{ count }}</td>
<td>The number of minutes used for testing.</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>
</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>
</tr>
</tbody>
</table>
{# {% set last_ancestor = page.ancestors | slice(start=-1) %}
{% set current_section = get_section(path=last_ancestor.0) %}

Expand Down

0 comments on commit 68e9bf3

Please sign in to comment.