diff options
| author | Samantaz Fox <coding@samantaz.fr> | 2021-12-21 23:10:03 +0100 |
|---|---|---|
| committer | Samantaz Fox <coding@samantaz.fr> | 2022-01-10 22:49:07 +0100 |
| commit | 7bb1471207a6dd30bae9466497e940ccc6057196 (patch) | |
| tree | 109a3eb1ad2cdadc94c765d4dff862122bb98f25 | |
| parent | 4752e16ad2cc2ee717b628032e54e87ad50a4aa0 (diff) | |
| download | invidious-7bb1471207a6dd30bae9466497e940ccc6057196.tar.gz invidious-7bb1471207a6dd30bae9466497e940ccc6057196.tar.bz2 invidious-7bb1471207a6dd30bae9466497e940ccc6057196.zip | |
i18n: Add dedicated function for counts translation
| -rw-r--r-- | src/invidious/helpers/i18n.cr | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.cr index fd3ddbad..316e5cda 100644 --- a/src/invidious/helpers/i18n.cr +++ b/src/invidious/helpers/i18n.cr @@ -107,6 +107,36 @@ def translate(locale : String?, key : String, text : String | Nil = nil) : Strin return translation end +def translate_count(locale : String, key : String, count : Int) : String + # Fallback on english if locale doesn't exist + locale = "en-US" if !LOCALES.has_key?(locale) + + # Retrieve suffix + suffix = I18next::Plurals::RESOLVER.get_suffix(locale, count) + plural_key = key + suffix + + if LOCALES[locale].has_key?(plural_key) + translation = LOCALES[locale][plural_key].as_s + else + # Try #1: Fallback to singular in the same locale + singular_suffix = I18next::Plurals::RESOLVER.get_suffix(locale, 1) + + if LOCALES[locale].has_key?(key + singular_suffix) + translation = LOCALES[locale][key + singular_suffix].as_s + else + # Try #2: Fallback to english (or return key we're already in english) + if locale == "en-US" + LOGGER.warn("i18n: Missing translation key \"#{key}\"") + return key + end + + translation = translate_count("en-US", key, count) + end + end + + return translation.gsub("{{count}}", count.to_s) +end + def translate_bool(locale : String?, translation : Bool) case translation when true |
