diff options
| author | ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> | 2024-02-15 21:44:40 -0500 |
|---|---|---|
| committer | ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> | 2024-02-15 21:45:22 -0500 |
| commit | 26429bee3f2bede1d4270f6e71a52482be1d5d49 (patch) | |
| tree | df32cc35709a6d1e55444484c2303b3a0cdc86dc /src | |
| parent | ef6b766b29160e06bd9abfb864851f993e75703c (diff) | |
| download | invidious-26429bee3f2bede1d4270f6e71a52482be1d5d49.tar.gz invidious-26429bee3f2bede1d4270f6e71a52482be1d5d49.tar.bz2 invidious-26429bee3f2bede1d4270f6e71a52482be1d5d49.zip | |
make it so interpolation text can be a hash
Co-Authored-By: Samantaz Fox <coding@samantaz.fr>
Diffstat (limited to 'src')
| -rw-r--r-- | src/invidious/frontend/comments_youtube.cr | 2 | ||||
| -rw-r--r-- | src/invidious/helpers/i18n.cr | 20 |
2 files changed, 12 insertions, 10 deletions
diff --git a/src/invidious/frontend/comments_youtube.cr b/src/invidious/frontend/comments_youtube.cr index 6551d411..aecac87f 100644 --- a/src/invidious/frontend/comments_youtube.cr +++ b/src/invidious/frontend/comments_youtube.cr @@ -117,7 +117,7 @@ module Invidious::Frontend::Comments image_array.each_index do |i| html << <<-END_HTML - <div class="slides-item slide-#{i + 1}" id="#{child["commentId"]}-slide-#{i + 1}" aria-label="#{translate(locale, "carousel_slide", nil, {"current" => (i + 1).to_s, "total" => image_array.size.to_s})}" tabindex="0"> + <div class="slides-item slide-#{i + 1}" id="#{child["commentId"]}-slide-#{i + 1}" aria-label="#{translate(locale, "carousel_slide", {"current" => (i + 1).to_s, "total" => image_array.size.to_s})}" tabindex="0"> <img loading="lazy" src="/ggpht#{URI.parse(image_array[i][1]["url"].as_s).request_target}" alt="" /> </div> END_HTML diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.cr index 8e2f7f44..23a1aafc 100644 --- a/src/invidious/helpers/i18n.cr +++ b/src/invidious/helpers/i18n.cr @@ -78,7 +78,7 @@ def load_all_locales return locales end -def translate(locale : String?, key : String, text : String | Nil = nil, texts : Hash(String, String) | Nil = nil) : String +def translate(locale : String?, key : String, text : String | Hash(String, String) | Nil = nil) : String # Log a warning if "key" doesn't exist in en-US locale and return # that key as the text, so this is more or less transparent to the user. if !LOCALES["en-US"].has_key?(key) @@ -101,10 +101,12 @@ def translate(locale : String?, key : String, text : String | Nil = nil, texts : match_length = 0 raw_data.as_h.each do |hash_key, value| - if md = text.try &.match(/#{hash_key}/) - if md[0].size >= match_length - translation = value.as_s - match_length = md[0].size + if text.is_a?(String) + if md = text.try &.match(/#{hash_key}/) + if md[0].size >= match_length + translation = value.as_s + match_length = md[0].size + end end end end @@ -114,12 +116,12 @@ def translate(locale : String?, key : String, text : String | Nil = nil, texts : raise "Invalid translation \"#{raw_data}\"" end - if text + if text.is_a?(String) translation = translation.gsub("`x`", text) - elsif texts + elsif text.is_a?(Hash(String, String)) # adds support for multi string interpolation. Based on i18next https://www.i18next.com/translation-function/interpolation#basic - texts.each_key do |hash_key| - translation = translation.gsub("{{#{hash_key}}}", texts[hash_key]) + text.each_key do |hash_key| + translation = translation.gsub("{{#{hash_key}}}", text[hash_key]) end end |
