make it so interpolation text can be a hash

Co-Authored-By: Samantaz Fox <coding@samantaz.fr>
This commit is contained in:
ChunkyProgrammer 2024-02-15 21:44:40 -05:00
parent ef6b766b29
commit 26429bee3f
2 changed files with 12 additions and 10 deletions

View File

@ -117,7 +117,7 @@ module Invidious::Frontend::Comments
image_array.each_index do |i| image_array.each_index do |i|
html << <<-END_HTML 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="" /> <img loading="lazy" src="/ggpht#{URI.parse(image_array[i][1]["url"].as_s).request_target}" alt="" />
</div> </div>
END_HTML END_HTML

View File

@ -78,7 +78,7 @@ def load_all_locales
return locales return locales
end 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 # 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. # that key as the text, so this is more or less transparent to the user.
if !LOCALES["en-US"].has_key?(key) if !LOCALES["en-US"].has_key?(key)
@ -101,6 +101,7 @@ def translate(locale : String?, key : String, text : String | Nil = nil, texts :
match_length = 0 match_length = 0
raw_data.as_h.each do |hash_key, value| raw_data.as_h.each do |hash_key, value|
if text.is_a?(String)
if md = text.try &.match(/#{hash_key}/) if md = text.try &.match(/#{hash_key}/)
if md[0].size >= match_length if md[0].size >= match_length
translation = value.as_s translation = value.as_s
@ -108,18 +109,19 @@ def translate(locale : String?, key : String, text : String | Nil = nil, texts :
end end
end end
end end
end
when .as_s? when .as_s?
translation = raw_data.as_s translation = raw_data.as_s
else else
raise "Invalid translation \"#{raw_data}\"" raise "Invalid translation \"#{raw_data}\""
end end
if text if text.is_a?(String)
translation = translation.gsub("`x`", text) 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 # adds support for multi string interpolation. Based on i18next https://www.i18next.com/translation-function/interpolation#basic
texts.each_key do |hash_key| text.each_key do |hash_key|
translation = translation.gsub("{{#{hash_key}}}", texts[hash_key]) translation = translation.gsub("{{#{hash_key}}}", text[hash_key])
end end
end end