From 9a48fd81a36634b41c55562eba410823448f9f68 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 18 Nov 2021 17:37:33 +0100 Subject: [PATCH 01/12] i18n: Add i18next plurals base sets --- src/invidious/helpers/i18next.cr | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/invidious/helpers/i18next.cr diff --git a/src/invidious/helpers/i18next.cr b/src/invidious/helpers/i18next.cr new file mode 100644 index 00000000..16ca594b --- /dev/null +++ b/src/invidious/helpers/i18next.cr @@ -0,0 +1,83 @@ +# I18next-compatible implementation of plural forms +# +module I18next::Plurals + # ----------------------------------- + # I18next plural forms definition + # ----------------------------------- + + private enum PluralForms + # One singular, one plural forms + Single_gt_one = 1 # E.g: French + Single_not_one = 2 # E.g: English + + # No plural forms (E.g: Azerbaijani) + None = 3 + + # One singular, two plural forms + Dual_Slavic = 4 # E.g: Russian + + # Special cases (rules used by only one or two language(s)) + Special_Arabic = 5 + Special_Czech_Slovak = 6 + Special_Polish_Kashubian = 7 + Special_Welsh = 8 + Special_Irish = 10 + Special_Scottish_Gaelic = 11 + Special_Icelandic = 12 + Special_Javanese = 13 + Special_Cornish = 14 + Special_Lithuanian = 15 + Special_Latvian = 16 + Special_Macedonian = 17 + Special_Mandinka = 18 + Special_Maltese = 19 + Special_Romanian = 20 + Special_Slovenian = 21 + Special_Hebrew = 22 + end + + private PLURAL_SETS = { + PluralForms::Single_gt_one => [ + "ach", "ak", "am", "arn", "br", "fil", "fr", "gun", "ln", "mfe", "mg", + "mi", "oc", "pt", "pt-BR", "tg", "tl", "ti", "tr", "uz", "wa", + ], + PluralForms::Single_not_one => [ + "af", "an", "ast", "az", "bg", "bn", "ca", "da", "de", "dev", "el", "en", + "eo", "es", "et", "eu", "fi", "fo", "fur", "fy", "gl", "gu", "ha", "hi", + "hu", "hy", "ia", "it", "kk", "kn", "ku", "lb", "mai", "ml", "mn", "mr", + "nah", "nap", "nb", "ne", "nl", "nn", "no", "nso", "pa", "pap", "pms", + "ps", "pt-PT", "rm", "sco", "se", "si", "so", "son", "sq", "sv", "sw", + "ta", "te", "tk", "ur", "yo", + ], + PluralForms::None => [ + "ay", "bo", "cgg", "fa", "ht", "id", "ja", "jbo", "ka", "km", "ko", "ky", + "lo", "ms", "sah", "su", "th", "tt", "ug", "vi", "wo", "zh", + ], + PluralForms::Dual_Slavic => [ + "be", "bs", "cnr", "dz", "hr", "ru", "sr", "uk", + ], + } + + private PLURAL_SINGLES = { + "ar" => PluralForms::Special_Arabic, + "cs" => PluralForms::Special_Czech_Slovak, + "csb" => PluralForms::Special_Polish_Kashubian, + "cy" => PluralForms::Special_Welsh, + "ga" => PluralForms::Special_Irish, + "gd" => PluralForms::Special_Scottish_Gaelic, + "he" => PluralForms::Special_Hebrew, + "is" => PluralForms::Special_Icelandic, + "iw" => PluralForms::Special_Hebrew, + "jv" => PluralForms::Special_Javanese, + "kw" => PluralForms::Special_Cornish, + "lt" => PluralForms::Special_Lithuanian, + "lv" => PluralForms::Special_Latvian, + "mk" => PluralForms::Special_Macedonian, + "mnk" => PluralForms::Special_Mandinka, + "mt" => PluralForms::Special_Maltese, + "pl" => PluralForms::Special_Polish_Kashubian, + "ro" => PluralForms::Special_Romanian, + "sk" => PluralForms::Special_Czech_Slovak, + "sl" => PluralForms::Special_Slovenian, + } +end From 71a1ad307c89a343880e09b8ca0b610f2106511a Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 18 Nov 2021 17:44:08 +0100 Subject: [PATCH 02/12] i18n: Add i18next plural resolver class --- src/invidious/helpers/i18next.cr | 129 +++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/src/invidious/helpers/i18next.cr b/src/invidious/helpers/i18next.cr index 16ca594b..d8b451a1 100644 --- a/src/invidious/helpers/i18next.cr +++ b/src/invidious/helpers/i18next.cr @@ -80,4 +80,133 @@ module I18next::Plurals "sk" => PluralForms::Special_Czech_Slovak, "sl" => PluralForms::Special_Slovenian, } + + # The array indices matches the PluralForms enum above + private NUMBERS = [ + [1, 2], # 1 + [1, 2], # 2 + [1], # 3 + [1, 2, 5], # 4 + [0, 1, 2, 3, 11, 100], # 5 + [1, 2, 5], # 6 + [1, 2, 5], # 7 + [1, 2, 3, 8], # 8 + [1, 2], # 9 (not used) + [1, 2, 3, 7, 11], # 10 + [1, 2, 3, 20], # 11 + [1, 2], # 12 + [0, 1], # 13 + [1, 2, 3, 4], # 14 + [1, 2, 10], # 15 + [1, 2, 0], # 16 + [1, 2], # 17 + [0, 1, 2], # 18 + [1, 2, 11, 20], # 19 + [1, 2, 20], # 20 + [5, 1, 2, 3], # 21 + [1, 2, 20, 21], # 22 + ] + + # "or" () + private NUMBERS_OR = [2, 1] + + # ----------------------------------- + # I18next plural resolver class + # ----------------------------------- + + class Resolver + @@forms : Hash(String, PluralForms) = init_rules() + @@version : UInt8 = 3 + + # Options + property simplify_plural_suffix : Bool = true + + # Suffixes + SUFFIXES_V1 = { + "", + "_plural_1", + "_plural_2", + "_plural_3", + "_plural_11", + "_plural_100", + } + SUFFIXES_V2 = {"_0", "_1", "_2", "_3", "_11", "_100"} + SUFFIXES_V3 = {"_0", "_1", "_2", "_3", "_4", "_5"} + + def initialize(version : UInt8 = 3) + # Sanity checks + # V4 isn't supported, as it requires a full CLDR database. + if version > 4 || version == 0 + raise "Invalid i18next version: v#{version}." + elsif version == 4 + # Logger.error("Unsupported i18next version: v4. Falling back to v3") + @@version = 3 + else + @@version = version + end + end + + def self.init_rules + # Look into sets + PLURAL_SETS.each do |form, langs| + langs.each { |lang| @@forms[lang] = form } + end + + # Add plurals from the "singles" set + @@forms.merge!(PLURAL_SINGLES) + end + + def get_plural_form(locale : String) : PluralForms + # Extract the ISO 639-1 or 639-2 code from an RFC 5646 + # language code, except for pt-BR which needs to be kept as-is. + if locale.starts_with?("pt-BR") + locale = "pt-BR" + else + locale = locale.split('-')[0] + end + + return @@forms[locale] if @@forms[locale]? + + # If nothing was found, then use the most common form, i.e + # one singular and one plural, as in english. Not perfect, + # but better than yielding an exception at the user. + return PluralForms::Single_not_one + end + + def get_suffix(locale : String, count : Int) : String + # Checked count must be absolute. In i18next, `rule.noAbs` is used to + # determine if comparison should be done on a signed or unsigned integer, + # but this variable is never set, resulting in the comparison always + # being done on absolute numbers. + return get_suffix_retrocompat(locale, count.abs) + end + + def get_suffix_retrocompat(locale : String, count : Int) : String + # Get plural form + plural_form = get_plural_form(locale) + rule_numbers = (locale == "or") ? NUMBERS_OR : NUMBERS[plural_form.to_i] + + # Languages with no plural have no suffix + return "" if plural_form.none? + + # Get the index and suffix for this number + # idx = Todo + suffix = rule_numbers[idx] + + # Simple plurals are handled differently in all versions (but v4) + if @simplify_plural_suffix && rule_numbers.size == 2 && rule_numbers[0] == 1 + return "_plural" if (suffix == 2) + return "" if (suffix == 1) + end + + # More complex plurals + # TODO: support `options.prepend` for v2 and v3 + # this.options.prepend && suffix.toString() ? this.options.prepend + suffix.toString() : suffix.toString() + case @version + when 1 then return SUFFIXES_V1[idx] + when 2 then return SUFFIXES_V2[idx] + else return SUFFIXES_V3[idx] + end + end + end end From 67d2635e41313f4bd1fb4259dfc1ce8b923a498f Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 18 Nov 2021 22:18:53 +0100 Subject: [PATCH 03/12] i18n: Add i18next plural rules and selector --- src/invidious/helpers/i18next.cr | 282 ++++++++++++++++++++++++++++++- 1 file changed, 281 insertions(+), 1 deletion(-) diff --git a/src/invidious/helpers/i18next.cr b/src/invidious/helpers/i18next.cr index d8b451a1..0ccb6a3e 100644 --- a/src/invidious/helpers/i18next.cr +++ b/src/invidious/helpers/i18next.cr @@ -190,7 +190,7 @@ module I18next::Plurals return "" if plural_form.none? # Get the index and suffix for this number - # idx = Todo + idx = SuffixIndex.get_index(plural_form, count) suffix = rule_numbers[idx] # Simple plurals are handled differently in all versions (but v4) @@ -209,4 +209,284 @@ module I18next::Plurals end end end + + # ----------------------------- + # Plural functions + # ----------------------------- + + module SuffixIndex + def self.get_index(plural_form : PluralForms, count : Int) : UInt8 + case plural_form + when .single_gt_one? then return (count > 1) ? 1_u8 : 0_u8 + when .single_not_one? then return (count != 1) ? 1_u8 : 0_u8 + when .none? then return 0_u8 + when .dual_slavic? then return dual_slavic(count) + when .special_arabic? then return special_arabic(count) + when .special_czech_slovak? then return special_czech_slovak(count) + when .special_polish_kashubian? then return special_polish_kashubian(count) + when .special_welsh? then return special_welsh(count) + when .special_irish? then return special_irish(count) + when .special_scottish_gaelic? then return special_scottish_gaelic(count) + when .special_icelandic? then return special_icelandic(count) + when .special_javanese? then return special_javanese(count) + when .special_cornish? then return special_cornish(count) + when .special_lithuanian? then return special_lithuanian(count) + when .special_latvian? then return special_latvian(count) + when .special_macedonian? then return special_macedonian(count) + when .special_mandinka? then return special_mandinka(count) + when .special_maltese? then return special_maltese(count) + when .special_romanian? then return special_romanian(count) + when .special_slovenian? then return special_slovenian(count) + when .special_hebrew? then return special_hebrew(count) + else + # default, if nothing matched above + return 0_u8 + end + end + + # Plural form of Slavic languages (E.g: Russian) + # + # Corresponds to i18next rule #4 + # Rule: (n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2) + # + def self.dual_slavic(count : Int) : UInt8 + n_mod_10 = count % 10 + n_mod_100 = count % 100 + + if n_mod_10 == 1 && n_mod_100 != 11 + return 0_u8 + elsif n_mod_10 >= 2 && n_mod_10 <= 4 && (n_mod_100 < 10 || n_mod_100 >= 20) + return 1_u8 + else + return 2_u8 + end + end + + # Plural form for Arabic language + # + # Corresponds to i18next rule #5 + # Rule: (n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5) + # + def self.special_arabic(count : Int) : UInt8 + return count.to_u8 if (count == 0 || count == 1 || count == 2) + + n_mod_100 = count % 100 + + return 3_u8 if (n_mod_100 >= 3 && n_mod_100 <= 10) + return 4_u8 if (n_mod_100 >= 11) + return 5_u8 + end + + # Plural form for Czech and Slovak languages + # + # Corresponds to i18next rule #6 + # Rule: ((n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2) + # + def self.special_czech_slovak(count : Int) : UInt8 + return 0_u8 if (count == 1) + return 1_u8 if (count >= 2 && count <= 4) + return 2_u8 + end + + # Plural form for Polish and Kashubian languages + # + # Corresponds to i18next rule #7 + # Rule: (n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2) + # + def self.special_polish_kashubian(count : Int) : UInt8 + return 0_u8 if (count == 1) + + n_mod_10 = count % 10 + n_mod_100 = count % 100 + + if n_mod_10 >= 2 && n_mod_10 <= 4 && (n_mod_100 < 10 || n_mod_100 >= 20) + return 1_u8 + else + return 2_u8 + end + end + + # Plural form for Welsh language + # + # Corresponds to i18next rule #8 + # Rule: ((n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3) + # + def self.special_welsh(count : Int) : UInt8 + return 0_u8 if (count == 1) + return 1_u8 if (count == 2) + return 2_u8 if (count != 8 && count != 11) + return 3_u8 + end + + # Plural form for Irish language + # + # Corresponds to i18next rule #10 + # Rule: (n==1 ? 0 : n==2 ? 1 : n<7 ? 2 : n<11 ? 3 : 4) + # + def self.special_irish(count : Int) : UInt8 + return count.to_u8 if (count == 1 || count == 2) + return 2_u8 if (count < 7) + return 3_u8 if (count < 11) + return 4_u8 + end + + # Plural form for Gaelic language + # + # Corresponds to i18next rule #11 + # Rule: ((n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3) + # + def self.special_scottish_gaelic(count : Int) : UInt8 + return 0_u8 if (count == 1 || count == 11) + return 1_u8 if (count == 2 || count == 12) + return 2_u8 if (count > 2 && count < 20) + return 3_u8 + end + + # Plural form for Icelandic language + # + # Corresponds to i18next rule #12 + # Rule: (n%10!=1 || n%100==11) + # + def self.special_icelandic(count : Int) : UInt8 + if (count % 10) != 1 || (count % 100) == 11 + return 1_u8 + else + return 0_u8 + end + end + + # Plural form for Javanese language + # + # Corresponds to i18next rule #13 + # Rule: (n !== 0) + # + def self.special_javanese(count : Int) : UInt8 + return (count != 0) ? 1_u8 : 0_u8 + end + + # Plural form for Cornish language + # + # Corresponds to i18next rule #14 + # Rule: ((n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3) + # + def self.special_cornish(count : Int) : UInt8 + return 0_u8 if count == 1 + return 1_u8 if count == 2 + return 2_u8 if count == 3 + return 3_u8 + end + + # Plural form for Lithuanian language + # + # Corresponds to i18next rule #15 + # Rule: (n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2) + # + def self.special_lithuanian(count : Int) : UInt8 + n_mod_10 = count % 10 + n_mod_100 = count % 100 + + if n_mod_10 == 1 && n_mod_100 != 11 + return 0_u8 + elsif n_mod_10 >= 2 && (n_mod_100 < 10 || n_mod_100 >= 20) + return 1_u8 + else + return 2_u8 + end + end + + # Plural form for Latvian language + # + # Corresponds to i18next rule #16 + # Rule: (n%10==1 && n%100!=11 ? 0 : n !== 0 ? 1 : 2) + # + def self.special_latvian(count : Int) : UInt8 + if (count % 10) == 1 && (count % 100) != 11 + return 0_u8 + elsif count != 0 + return 1_u8 + else + return 2_u8 + end + end + + # Plural form for Macedonian language + # + # Corresponds to i18next rule #17 + # Rule: (n==1 || n%10==1 && n%100!=11 ? 0 : 1) + # + def self.special_macedonian(count : Int) : UInt8 + if count == 1 || ((count % 10) == 1 && (count % 100) != 11) + return 0_u8 + else + return 1_u8 + end + end + + # Plural form for Mandinka language + # + # Corresponds to i18next rule #18 + # Rule: (n==0 ? 0 : n==1 ? 1 : 2) + # + def self.special_mandinka(count : Int) : UInt8 + return (count == 0 || count == 1) ? count.to_u8 : 2_u8 + end + + # Plural form for Maltese language + # + # Corresponds to i18next rule #19 + # Rule: (n==1 ? 0 : n==0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3) + # + def self.special_maltese(count : Int) : UInt8 + return 0_u8 if count == 1 + return 1_u8 if count == 0 + + n_mod_100 = count % 100 + return 1_u8 if (n_mod_100 > 1 && n_mod_100 < 11) + return 2_u8 if (n_mod_100 > 10 && n_mod_100 < 20) + return 3_u8 + end + + # Plural form for Romanian language + # + # Corresponds to i18next rule #20 + # Rule: (n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2) + # + def self.special_romanian(count : Int) : UInt8 + return 0_u8 if count == 1 + return 1_u8 if count == 0 + + n_mod_100 = count % 100 + return 1_u8 if (n_mod_100 > 0 && n_mod_100 < 20) + return 2_u8 + end + + # Plural form for Slovenian language + # + # Corresponds to i18next rule #21 + # Rule: (n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0) + # + def self.special_slovenian(count : Int) : UInt8 + n_mod_100 = count % 100 + return 1_u8 if (n_mod_100 == 1) + return 2_u8 if (n_mod_100 == 2) + return 3_u8 if (n_mod_100 == 3 || n_mod_100 == 4) + return 0_u8 + end + + # Plural form for Hebrew language + # + # Corresponds to i18next rule #22 + # Rule: (n==1 ? 0 : n==2 ? 1 : (n<0 || n>10) && n%10==0 ? 2 : 3) + # + def self.special_hebrew(count : Int) : UInt8 + return 0_u8 if (count == 1) + return 1_u8 if (count == 2) + + if (count < 0 || count > 10) && (count % 10) == 0 + return 2_u8 + else + return 3_u8 + end + end + end end From 2a156e7313611eca29abd275f7e19a383580a65f Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 19 Nov 2021 03:36:52 +0100 Subject: [PATCH 04/12] i18n: Add plural tests (spec) --- spec/i18next_plurals_spec.cr | 208 +++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 spec/i18next_plurals_spec.cr diff --git a/spec/i18next_plurals_spec.cr b/spec/i18next_plurals_spec.cr new file mode 100644 index 00000000..ec14f418 --- /dev/null +++ b/spec/i18next_plurals_spec.cr @@ -0,0 +1,208 @@ +require "spec" +require "../src/invidious/helpers/i18next.cr" + +resolver = I18next::Plurals::RESOLVER + +FORM_TESTS = { + "ach" => I18next::Plurals::PluralForms::Single_gt_one, + "ar" => I18next::Plurals::PluralForms::Special_Arabic, + "be" => I18next::Plurals::PluralForms::Dual_Slavic, + "cy" => I18next::Plurals::PluralForms::Special_Welsh, + "en" => I18next::Plurals::PluralForms::Single_not_one, + "fr" => I18next::Plurals::PluralForms::Single_gt_one, + "ga" => I18next::Plurals::PluralForms::Special_Irish, + "gd" => I18next::Plurals::PluralForms::Special_Scottish_Gaelic, + "he" => I18next::Plurals::PluralForms::Special_Hebrew, + "is" => I18next::Plurals::PluralForms::Special_Icelandic, + "jv" => I18next::Plurals::PluralForms::Special_Javanese, + "kw" => I18next::Plurals::PluralForms::Special_Cornish, + "lt" => I18next::Plurals::PluralForms::Special_Lithuanian, + "lv" => I18next::Plurals::PluralForms::Special_Latvian, + "mk" => I18next::Plurals::PluralForms::Special_Macedonian, + "mnk" => I18next::Plurals::PluralForms::Special_Mandinka, + "mt" => I18next::Plurals::PluralForms::Special_Maltese, + "or" => I18next::Plurals::PluralForms::Special_Odia, + "pl" => I18next::Plurals::PluralForms::Special_Polish_Kashubian, + "pt" => I18next::Plurals::PluralForms::Single_gt_one, + "pt-PT" => I18next::Plurals::PluralForms::Single_not_one, + "pt-BR" => I18next::Plurals::PluralForms::Single_gt_one, + "ro" => I18next::Plurals::PluralForms::Special_Romanian, + "su" => I18next::Plurals::PluralForms::None, + "sk" => I18next::Plurals::PluralForms::Special_Czech_Slovak, + "sl" => I18next::Plurals::PluralForms::Special_Slovenian, + +} + +SUFFIX_TESTS = { + "ach" => [ + {num: 0, suffix: ""}, + {num: 1, suffix: ""}, + {num: 10, suffix: "_plural"}, + ], + "ar" => [ + {num: 0, suffix: "_0"}, + {num: 1, suffix: "_1"}, + {num: 2, suffix: "_2"}, + {num: 3, suffix: "_3"}, + {num: 4, suffix: "_3"}, + {num: 104, suffix: "_3"}, + {num: 11, suffix: "_4"}, + {num: 99, suffix: "_4"}, + {num: 199, suffix: "_4"}, + {num: 100, suffix: "_5"}, + ], + "be" => [ + {num: 0, suffix: "_2"}, + {num: 1, suffix: "_0"}, + {num: 5, suffix: "_2"}, + ], + "cy" => [ + {num: 0, suffix: "_2"}, + {num: 1, suffix: "_0"}, + {num: 3, suffix: "_2"}, + {num: 8, suffix: "_3"}, + ], + "en" => [ + {num: 0, suffix: "_plural"}, + {num: 1, suffix: ""}, + {num: 10, suffix: "_plural"}, + ], + "fr" => [ + {num: 0, suffix: ""}, + {num: 1, suffix: ""}, + {num: 10, suffix: "_plural"}, + ], + "ga" => [ + {num: 1, suffix: "_0"}, + {num: 2, suffix: "_1"}, + {num: 3, suffix: "_2"}, + {num: 7, suffix: "_3"}, + {num: 11, suffix: "_4"}, + ], + "gd" => [ + {num: 1, suffix: "_0"}, + {num: 2, suffix: "_1"}, + {num: 3, suffix: "_2"}, + {num: 20, suffix: "_3"}, + ], + "he" => [ + {num: 0, suffix: "_3"}, + {num: 1, suffix: "_0"}, + {num: 2, suffix: "_1"}, + {num: 3, suffix: "_3"}, + {num: 20, suffix: "_2"}, + {num: 21, suffix: "_3"}, + {num: 30, suffix: "_2"}, + {num: 100, suffix: "_2"}, + {num: 101, suffix: "_3"}, + ], + "is" => [ + {num: 1, suffix: ""}, + {num: 2, suffix: "_plural"}, + ], + "jv" => [ + {num: 0, suffix: "_0"}, + {num: 1, suffix: "_1"}, + ], + "kw" => [ + {num: 1, suffix: "_0"}, + {num: 2, suffix: "_1"}, + {num: 3, suffix: "_2"}, + {num: 4, suffix: "_3"}, + ], + "lt" => [ + {num: 1, suffix: "_0"}, + {num: 2, suffix: "_1"}, + {num: 10, suffix: "_2"}, + ], + "lv" => [ + {num: 1, suffix: "_0"}, + {num: 2, suffix: "_1"}, + {num: 0, suffix: "_2"}, + ], + "mk" => [ + {num: 1, suffix: ""}, + {num: 2, suffix: "_plural"}, + {num: 0, suffix: "_plural"}, + {num: 11, suffix: "_plural"}, + {num: 21, suffix: ""}, + {num: 31, suffix: ""}, + {num: 311, suffix: "_plural"}, + ], + "mnk" => [ + {num: 0, suffix: "_0"}, + {num: 1, suffix: "_1"}, + {num: 2, suffix: "_2"}, + ], + "mt" => [ + {num: 1, suffix: "_0"}, + {num: 2, suffix: "_1"}, + {num: 11, suffix: "_2"}, + {num: 20, suffix: "_3"}, + ], + "or" => [ + {num: 2, suffix: "_1"}, + {num: 1, suffix: "_0"}, + ], + "pl" => [ + {num: 0, suffix: "_2"}, + {num: 1, suffix: "_0"}, + {num: 5, suffix: "_2"}, + ], + "pt" => [ + {num: 0, suffix: ""}, + {num: 1, suffix: ""}, + {num: 10, suffix: "_plural"}, + ], + "pt-PT" => [ + {num: 0, suffix: "_plural"}, + {num: 1, suffix: ""}, + {num: 10, suffix: "_plural"}, + ], + "pt-BR" => [ + {num: 0, suffix: ""}, + {num: 1, suffix: ""}, + {num: 10, suffix: "_plural"}, + ], + "ro" => [ + {num: 0, suffix: "_1"}, + {num: 1, suffix: "_0"}, + {num: 20, suffix: "_2"}, + ], + "su" => [ + {num: 0, suffix: "_0"}, + {num: 1, suffix: "_0"}, + {num: 10, suffix: "_0"}, + ], + "sk" => [ + {num: 0, suffix: "_2"}, + {num: 1, suffix: "_0"}, + {num: 5, suffix: "_2"}, + ], + "sl" => [ + {num: 5, suffix: "_0"}, + {num: 1, suffix: "_1"}, + {num: 2, suffix: "_2"}, + {num: 3, suffix: "_3"}, + ], +} + +describe "i18next_Plural_Resolver" do + describe "get_plural_form" do + FORM_TESTS.each do |locale, form| + it "returns the right plural form for locale '#{locale}'" do + resolver.get_plural_form(locale).should eq(form) + end + end + end + + describe "get_suffix" do + SUFFIX_TESTS.each do |locale, data| + it "returns the right suffix for locale '#{locale}'" do + data.each do |d| + resolver.get_suffix(locale, d[:num]).should eq(d[:suffix]) + end + end + end + end +end From 4752e16ad2cc2ee717b628032e54e87ad50a4aa0 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 19 Nov 2021 03:48:21 +0100 Subject: [PATCH 05/12] i18n: make multiple fixes to i18next plurals --- src/invidious/helpers/i18next.cr | 114 ++++++++++++++++++------------- 1 file changed, 68 insertions(+), 46 deletions(-) diff --git a/src/invidious/helpers/i18next.cr b/src/invidious/helpers/i18next.cr index 0ccb6a3e..b7506545 100644 --- a/src/invidious/helpers/i18next.cr +++ b/src/invidious/helpers/i18next.cr @@ -5,7 +5,7 @@ module I18next::Plurals # I18next plural forms definition # ----------------------------------- - private enum PluralForms + enum PluralForms # One singular, one plural forms Single_gt_one = 1 # E.g: French Single_not_one = 2 # E.g: English @@ -34,6 +34,7 @@ module I18next::Plurals Special_Romanian = 20 Special_Slovenian = 21 Special_Hebrew = 22 + Special_Odia = 23 end private PLURAL_SETS = { @@ -75,13 +76,15 @@ module I18next::Plurals "mk" => PluralForms::Special_Macedonian, "mnk" => PluralForms::Special_Mandinka, "mt" => PluralForms::Special_Maltese, + "or" => PluralForms::Special_Odia, "pl" => PluralForms::Special_Polish_Kashubian, "ro" => PluralForms::Special_Romanian, "sk" => PluralForms::Special_Czech_Slovak, "sl" => PluralForms::Special_Slovenian, } - # The array indices matches the PluralForms enum above + # These are the v1 and v2 compatible suffixes. + # The array indices matches the PluralForms enum above. private NUMBERS = [ [1, 2], # 1 [1, 2], # 2 @@ -105,67 +108,58 @@ module I18next::Plurals [1, 2, 20], # 20 [5, 1, 2, 3], # 21 [1, 2, 20, 21], # 22 + [2, 1], # 23 (Odia) ] - # "or" () - private NUMBERS_OR = [2, 1] - # ----------------------------------- # I18next plural resolver class # ----------------------------------- + RESOLVER = Resolver.new + class Resolver - @@forms : Hash(String, PluralForms) = init_rules() - @@version : UInt8 = 3 + private property forms = {} of String => PluralForms + property version : UInt8 = 3 # Options property simplify_plural_suffix : Bool = true - # Suffixes - SUFFIXES_V1 = { - "", - "_plural_1", - "_plural_2", - "_plural_3", - "_plural_11", - "_plural_100", - } - SUFFIXES_V2 = {"_0", "_1", "_2", "_3", "_11", "_100"} - SUFFIXES_V3 = {"_0", "_1", "_2", "_3", "_4", "_5"} - - def initialize(version : UInt8 = 3) + def initialize(version : Int = 3) # Sanity checks # V4 isn't supported, as it requires a full CLDR database. if version > 4 || version == 0 raise "Invalid i18next version: v#{version}." elsif version == 4 # Logger.error("Unsupported i18next version: v4. Falling back to v3") - @@version = 3 + @version = 3_u8 else - @@version = version + @version = version.to_u8 end + + self.init_rules end - def self.init_rules + def init_rules # : Hash(String, PluralForms) + # Init + # forms = {} of String => PluralForms + # Look into sets PLURAL_SETS.each do |form, langs| - langs.each { |lang| @@forms[lang] = form } + langs.each { |lang| self.forms[lang] = form } end # Add plurals from the "singles" set - @@forms.merge!(PLURAL_SINGLES) + self.forms.merge!(PLURAL_SINGLES) end def get_plural_form(locale : String) : PluralForms - # Extract the ISO 639-1 or 639-2 code from an RFC 5646 - # language code, except for pt-BR which needs to be kept as-is. - if locale.starts_with?("pt-BR") - locale = "pt-BR" - else + # Extract the ISO 639-1 or 639-2 code from an RFC 5646 language code, + # except for pt-BR and pt-PT which needs to be kept as-is. + if !locale.matches?(/^pt-(BR|PT)$/) locale = locale.split('-')[0] end - return @@forms[locale] if @@forms[locale]? + return self.forms[locale] if self.forms[locale]? # If nothing was found, then use the most common form, i.e # one singular and one plural, as in english. Not perfect, @@ -181,32 +175,48 @@ module I18next::Plurals return get_suffix_retrocompat(locale, count.abs) end - def get_suffix_retrocompat(locale : String, count : Int) : String + # Emulate the `rule.numbers.size == 2 && rule.numbers[0] == 1` check + # from original i18next code + private def is_simple_plural(form : PluralForms) : Bool + case form + when .single_gt_one? then return true + when .single_not_one? then return true + when .special_icelandic? then return true + when .special_macedonian? then return true + else + return false + end + end + + private def get_suffix_retrocompat(locale : String, count : Int) : String # Get plural form plural_form = get_plural_form(locale) - rule_numbers = (locale == "or") ? NUMBERS_OR : NUMBERS[plural_form.to_i] - # Languages with no plural have no suffix - return "" if plural_form.none? + # Languages with no plural have the "_0" suffix + return "_0" if plural_form.none? # Get the index and suffix for this number idx = SuffixIndex.get_index(plural_form, count) - suffix = rule_numbers[idx] # Simple plurals are handled differently in all versions (but v4) - if @simplify_plural_suffix && rule_numbers.size == 2 && rule_numbers[0] == 1 - return "_plural" if (suffix == 2) - return "" if (suffix == 1) + if @simplify_plural_suffix && is_simple_plural(plural_form) + return (idx == 1) ? "_plural" : "" end # More complex plurals - # TODO: support `options.prepend` for v2 and v3 + # TODO: support v1 and v2 + # TODO: support `options.prepend` (v2 and v3) # this.options.prepend && suffix.toString() ? this.options.prepend + suffix.toString() : suffix.toString() - case @version - when 1 then return SUFFIXES_V1[idx] - when 2 then return SUFFIXES_V2[idx] - else return SUFFIXES_V3[idx] - end + # + # case @version + # when 1 + # suffix = SUFFIXES_V1_V2[plural_form.to_i][idx] + # return (suffix == 1) ? "" : return "_plural_#{suffix}" + # when 2 + # return "_#{suffix}" + # else # v3 + return "_#{idx}" + # end end end @@ -238,6 +248,7 @@ module I18next::Plurals when .special_romanian? then return special_romanian(count) when .special_slovenian? then return special_slovenian(count) when .special_hebrew? then return special_hebrew(count) + when .special_odia? then return special_odia(count) else # default, if nothing matched above return 0_u8 @@ -324,7 +335,8 @@ module I18next::Plurals # Rule: (n==1 ? 0 : n==2 ? 1 : n<7 ? 2 : n<11 ? 3 : 4) # def self.special_irish(count : Int) : UInt8 - return count.to_u8 if (count == 1 || count == 2) + return 0_u8 if (count == 1) + return 1_u8 if (count == 2) return 2_u8 if (count < 7) return 3_u8 if (count < 11) return 4_u8 @@ -488,5 +500,15 @@ module I18next::Plurals return 3_u8 end end + + # Plural form for Odia ("or") language + # + # This one is a bit special. It should use rule #2 (like english) + # but the "numbers" (suffixes?) it has are inverted, so we'll make a + # special rule for it. + # + def self.special_odia(count : Int) : UInt8 + return (count == 1) ? 0_u8 : 1_u8 + end end end From 7bb1471207a6dd30bae9466497e940ccc6057196 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 21 Dec 2021 23:10:03 +0100 Subject: [PATCH 06/12] i18n: Add dedicated function for counts translation --- src/invidious/helpers/i18n.cr | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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 From 692f4e5be2c80556e84fa618eb8124083da24161 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Wed, 22 Dec 2021 00:07:20 +0100 Subject: [PATCH 07/12] i18n: Use plurals for year/month/day/etc... --- locales/en-US.json | 42 ++++++++++++---------------------- locales/fa.json | 35 ++++++---------------------- locales/fr.json | 42 ++++++++++++---------------------- locales/id.json | 35 ++++++---------------------- locales/it.json | 42 ++++++++++++---------------------- locales/ja.json | 35 ++++++---------------------- locales/ko.json | 35 ++++++---------------------- locales/pt-BR.json | 42 ++++++++++++---------------------- locales/pt-PT.json | 42 ++++++++++++---------------------- locales/pt.json | 42 ++++++++++++---------------------- locales/zh-CN.json | 35 ++++++---------------------- locales/zh-TW.json | 35 ++++++---------------------- src/invidious/helpers/utils.cr | 16 ++++++------- 13 files changed, 133 insertions(+), 345 deletions(-) diff --git a/locales/en-US.json b/locales/en-US.json index 94aac89e..166143ac 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -359,34 +359,20 @@ "Yiddish": "Yiddish", "Yoruba": "Yoruba", "Zulu": "Zulu", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` year", - "": "`x` years" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` month", - "": "`x` months" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` week", - "": "`x` weeks" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` day", - "": "`x` days" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` hour", - "": "`x` hours" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` minute", - "": "`x` minutes" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` second", - "": "`x` seconds" - }, + "generic_count_years": "{{count}} year", + "generic_count_years_plural": "{{count}} years", + "generic_count_months": "{{count}} month", + "generic_count_months_plural": "{{count}} months", + "generic_count_weeks": "{{count}} week", + "generic_count_weeks_plural": "{{count}} weeks", + "generic_count_days": "{{count}} day", + "generic_count_days_plural": "{{count}} days", + "generic_count_hours": "{{count}} hour", + "generic_count_hours_plural": "{{count}} hours", + "generic_count_minutes": "{{count}} minute", + "generic_count_minutes_plural": "{{count}} minutes", + "generic_count_seconds": "{{count}} second", + "generic_count_seconds_plural": "{{count}} seconds", "Fallback comments: ": "Fallback comments: ", "Popular": "Popular", "Search": "Search", diff --git a/locales/fa.json b/locales/fa.json index 1f723a63..d8df2b4f 100644 --- a/locales/fa.json +++ b/locales/fa.json @@ -340,34 +340,13 @@ "Yiddish": "ییدیش", "Yoruba": "یوروبایی", "Zulu": "زولو", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` سال", - "": "`x` سال" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ماه", - "": "`x` ماه" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` هفته", - "": "`x` هفته" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` روز", - "": "`x` روز" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ساعت", - "": "`x` ساعت" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` دقیقه", - "": "`x` دقیقه" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ثانیه", - "": "`x` ثانیه" - }, + "generic_count_years_0": "{{count}} سال", + "generic_count_months_0": "{{count}} ماه", + "generic_count_weeks_0": "{{count}} هفته", + "generic_count_days_0": "{{count}} روز", + "generic_count_hours_0": "{{count}} ساعت", + "generic_count_minutes_0": "{{count}} دقیقه", + "generic_count_seconds_0": "{{count}} ثانیه", "Fallback comments: ": "نظرات عقب گرد: ", "Popular": "محبوب", "Search": "جستجو", diff --git a/locales/fr.json b/locales/fr.json index 5ebd6f70..f9975a6b 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -340,34 +340,20 @@ "Yiddish": "Yiddish", "Yoruba": "Yoruba", "Zulu": "Zoulou", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` an", - "": "`x` ans" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` mois", - "": "`x` mois" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` semaine", - "": "`x` semaines" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` jour", - "": "`x` jours" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` heure", - "": "`x` heures" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` minute", - "": "`x` minutes" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` seconde", - "": "`x` secondes" - }, + "generic_count_years": "{{count}} an", + "generic_count_years_plural": "{{count}} ans", + "generic_count_months": "{{count}} mois", + "generic_count_months_plural": "{{count}} mois", + "generic_count_weeks": "{{count}} semaine", + "generic_count_weeks_plural": "{{count}} semaines", + "generic_count_days": "{{count}} jour", + "generic_count_days_plural": "{{count}} jours", + "generic_count_hours": "{{count}} heure", + "generic_count_hours_plural": "{{count}} heures", + "generic_count_minutes": "{{count}} minute", + "generic_count_minutes_plural": "{{count}} minutes", + "generic_count_seconds": "{{count}} seconde", + "generic_count_seconds_plural": "{{count}} secondes", "Fallback comments: ": "Commentaires alternatifs : ", "Popular": "Populaire", "Search": "Rechercher", diff --git a/locales/id.json b/locales/id.json index b3918955..78f5e773 100644 --- a/locales/id.json +++ b/locales/id.json @@ -340,34 +340,13 @@ "Yiddish": "Bahasa Yiddi", "Yoruba": "Bahasa Yoruba", "Zulu": "Bahasa Zulu", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` tahun", - "": "`x` tahun" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` bulan", - "": "`x` bulan" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` pekan", - "": "`x` pekan" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` hari", - "": "`x` hari" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` jam", - "": "`x` jam" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` menit", - "": "`x` menit" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` detik", - "": "`x` detik" - }, + "generic_count_years_0": "{{count}} tahun", + "generic_count_months_0": "{{count}} bulan", + "generic_count_weeks_0": "{{count}} pekan", + "generic_count_days_0": "{{count}} hari", + "generic_count_hours_0": "{{count}} jam", + "generic_count_minutes_0": "{{count}} menit", + "generic_count_seconds_0": "{{count}} detik", "Fallback comments: ": "Komentar alternatif: ", "Popular": "Populer", "Search": "Cari", diff --git a/locales/it.json b/locales/it.json index a43e1a49..befdd665 100644 --- a/locales/it.json +++ b/locales/it.json @@ -330,34 +330,20 @@ "Yiddish": "Yiddish", "Yoruba": "Yoruba", "Zulu": "Zulu", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` anno", - "": "`x` anni" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` mese", - "": "`x` mesi" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` settimana", - "": "`x` settimane" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` giorno", - "": "`x` giorni" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ora", - "": "`x` ore" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` minuto", - "": "`x` minuti" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` secondo", - "": "`x` secondi" - }, + "generic_count_years": "{{count}} anno", + "generic_count_years_plural": "{{count}} anni", + "generic_count_months": "{{count}} mese", + "generic_count_months_plural": "{{count}} mesi", + "generic_count_weeks": "{{count}} settimana", + "generic_count_weeks_plural": "{{count}} settimane", + "generic_count_days": "{{count}} giorno", + "generic_count_days_plural": "{{count}} giorni", + "generic_count_hours": "{{count}} ora", + "generic_count_hours_plural": "{{count}} ore", + "generic_count_minutes": "{{count}} minuto", + "generic_count_minutes_plural": "{{count}} minuti", + "generic_count_seconds": "{{count}} secondo", + "generic_count_seconds_plural": "{{count}} secondi", "Fallback comments: ": "Commenti alternativi: ", "Popular": "Popolare", "Search": "Cerca", diff --git a/locales/ja.json b/locales/ja.json index bf858f1f..7423d2ca 100644 --- a/locales/ja.json +++ b/locales/ja.json @@ -340,34 +340,13 @@ "Yiddish": "イディッシュ語", "Yoruba": "ヨルバ語", "Zulu": "ズール語", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x`年", - "": "`x`年" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x`ヶ月", - "": "`x`ヶ月" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x`週", - "": "`x`週" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x`日", - "": "`x`日" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x`時間", - "": "`x`時間" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x`分", - "": "`x`分" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x`秒", - "": "`x`秒" - }, + "generic_count_years_0": "{{count}}年", + "generic_count_months_0": "{{count}}ヶ月", + "generic_count_weeks_0": "{{count}}週", + "generic_count_days_0": "{{count}}日", + "generic_count_hours_0": "{{count}}時間", + "generic_count_minutes_0": "{{count}}分", + "generic_count_seconds_0": "{{count}}秒", "Fallback comments: ": "フォールバック時のコメント: ", "Popular": "人気", "Search": "検索", diff --git a/locales/ko.json b/locales/ko.json index b96b3c0b..96fd41ff 100644 --- a/locales/ko.json +++ b/locales/ko.json @@ -336,36 +336,15 @@ "Scottish Gaelic": "스코틀랜드 게일어", "Popular": "인기", "Fallback comments: ": "대체 댓글: ", - "`x` seconds": { - "": "`x` 초", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 초" - }, "Swahili": "스와힐리어", "Sundanese": "순다어", - "`x` hours": { - "": "`x` 시", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 시" - }, - "`x` minutes": { - "": "`x` 분", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 분" - }, - "`x` days": { - "": "`x` 일", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 일" - }, - "`x` weeks": { - "": "`x` 주", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 주" - }, - "`x` months": { - "": "`x` 월", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 월" - }, - "`x` years": { - "": "`x` 년", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 년" - }, + "generic_count_years_0": "{{count}} 년", + "generic_count_months_0": "{{count}} 월", + "generic_count_weeks_0": "{{count}} 주", + "generic_count_days_0": "{{count}} 일", + "generic_count_hours_0": "{{count}} 시", + "generic_count_minutes_0": "{{count}} 분", + "generic_count_seconds_0": "{{count}} 초", "Zulu": "줄루어", "Yoruba": "요루바어", "Yiddish": "이디시어", diff --git a/locales/pt-BR.json b/locales/pt-BR.json index 6baa2c0d..01407669 100644 --- a/locales/pt-BR.json +++ b/locales/pt-BR.json @@ -340,34 +340,20 @@ "Yiddish": "Iídiche", "Yoruba": "Iorubá", "Zulu": "Zulu", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ano", - "": "`x` anos" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` mês", - "": "`x` meses" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` semana", - "": "`x` semanas" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` dia", - "": "`x` dia" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` hora", - "": "`x` horas" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` minuto", - "": "`x` minutos" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` segundo", - "": "`x` segundos" - }, + "generic_count_years": "{{count}} ano", + "generic_count_years_plural": "{{count}} anos", + "generic_count_months": "{{count}} mês", + "generic_count_months_plural": "{{count}} meses", + "generic_count_weeks": "{{count}} semana", + "generic_count_weeks_plural": "{{count}} semanas", + "generic_count_days": "{{count}} dia", + "generic_count_days_plural": "{{count}} dia", + "generic_count_hours": "{{count}} hora", + "generic_count_hours_plural": "{{count}} horas", + "generic_count_minutes": "{{count}} minuto", + "generic_count_minutes_plural": "{{count}} minutos", + "generic_count_seconds": "{{count}} segundo", + "generic_count_seconds_plural": "{{count}} segundos", "Fallback comments: ": "Comentários alternativos: ", "Popular": "Populares", "Search": "Procurar", diff --git a/locales/pt-PT.json b/locales/pt-PT.json index f3952f12..83b59ab5 100644 --- a/locales/pt-PT.json +++ b/locales/pt-PT.json @@ -340,34 +340,20 @@ "Yiddish": "Iídiche", "Yoruba": "Ioruba", "Zulu": "Zulu", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ano", - "": "`x` anos" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` mês", - "": "`x` meses" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` seman", - "": "`x` semanas" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` dia", - "": "`x` dias" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` hora", - "": "`x` horas" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` minuto", - "": "`x` minutos" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` segundo", - "": "`x` segundos" - }, + "generic_count_years": "{{count}} ano", + "generic_count_years_plural": "{{count}} anos", + "generic_count_months": "{{count}} mês", + "generic_count_months_plural": "{{count}} meses", + "generic_count_weeks": "{{count}} seman", + "generic_count_weeks_plural": "{{count}} semanas", + "generic_count_days": "{{count}} dia", + "generic_count_days_plural": "{{count}} dias", + "generic_count_hours": "{{count}} hora", + "generic_count_hours_plural": "{{count}} horas", + "generic_count_minutes": "{{count}} minuto", + "generic_count_minutes_plural": "{{count}} minutos", + "generic_count_seconds": "{{count}} segundo", + "generic_count_seconds_plural": "{{count}} segundos", "Fallback comments: ": "Comentários alternativos: ", "Popular": "Popular", "Search": "Pesquisar", diff --git a/locales/pt.json b/locales/pt.json index 1976fe38..065170fb 100644 --- a/locales/pt.json +++ b/locales/pt.json @@ -46,34 +46,20 @@ "Default": "Predefinido", "Top": "Destaques", "Search": "Pesquisar", - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` segundo", - "": "`x` segundos" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` minuto", - "": "`x` minutos" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` hora", - "": "`x` horas" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` dia", - "": "`x` dias" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` seman", - "": "`x` semanas" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` mês", - "": "`x` meses" - }, - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ano", - "": "`x` anos" - }, + "generic_count_years": "{{count}} segundo", + "generic_count_years_plural": "{{count}} segundos", + "generic_count_months": "{{count}} minuto", + "generic_count_months_plural": "{{count}} minutos", + "generic_count_weeks": "{{count}} hora", + "generic_count_weeks_plural": "{{count}} horas", + "generic_count_days": "{{count}} dia", + "generic_count_days_plural": "{{count}} dias", + "generic_count_hours": "{{count}} seman", + "generic_count_hours_plural": "{{count}} semanas", + "generic_count_minutes": "{{count}} mês", + "generic_count_minutes_plural": "{{count}} meses", + "generic_count_seconds": "{{count}} ano", + "generic_count_seconds_plural": "{{count}} anos", "Chinese (Traditional)": "Chinês (tradicional)", "Chinese (Simplified)": "Chinês (simplificado)", "Could not pull trending pages.": "Não foi possível obter as páginas de tendências.", diff --git a/locales/zh-CN.json b/locales/zh-CN.json index ed5d82ce..6108a680 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -340,34 +340,13 @@ "Yiddish": "意第绪语", "Yoruba": "约鲁巴语", "Zulu": "祖鲁语", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 年", - "": "`x` 年" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 月", - "": "`x` 个月" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 周", - "": "`x` 周" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 天", - "": "`x` 天" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 小时", - "": "`x` 小时" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 分钟", - "": "`x` 分钟" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 秒", - "": "`x` 秒" - }, + "generic_count_years_0": "{{count}} 年", + "generic_count_months_0": "{{count}} 月", + "generic_count_weeks_0": "{{count}} 周", + "generic_count_days_0": "{{count}} 天", + "generic_count_hours_0": "{{count}} 小时", + "generic_count_minutes_0": "{{count}} 分钟", + "generic_count_seconds_0": "{{count}} 秒", "Fallback comments: ": "后备评论: ", "Popular": "热门频道", "Search": "搜索", diff --git a/locales/zh-TW.json b/locales/zh-TW.json index aad51069..d3580c4d 100644 --- a/locales/zh-TW.json +++ b/locales/zh-TW.json @@ -340,34 +340,13 @@ "Yiddish": "意第緒語", "Yoruba": "約魯巴語", "Zulu": "祖魯語", - "`x` years": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 年", - "": "`x` 年" - }, - "`x` months": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 月", - "": "`x` 月" - }, - "`x` weeks": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 週", - "": "`x` 週" - }, - "`x` days": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 天", - "": "`x` 天" - }, - "`x` hours": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 小時", - "": "`x` 小時" - }, - "`x` minutes": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 天", - "": "`x` 分鐘" - }, - "`x` seconds": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 秒", - "": "`x` 秒" - }, + "generic_count_years_0": "{{count}} 年", + "generic_count_months_0": "{{count}} 月", + "generic_count_weeks_0": "{{count}} 週", + "generic_count_days_0": "{{count}} 天", + "generic_count_hours_0": "{{count}} 小時", + "generic_count_minutes_0": "{{count}} 分鐘", + "generic_count_seconds_0": "{{count}} 秒", "Fallback comments: ": "汰退留言: ", "Popular": "熱門頻道", "Search": "搜尋", diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 8453d605..09181c10 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -123,22 +123,20 @@ def recode_date(time : Time, locale) span = Time.utc - time if span.total_days > 365.0 - span = translate(locale, "`x` years", (span.total_days.to_i // 365).to_s) + return translate_count(locale, "generic_count_years", span.total_days.to_i // 365) elsif span.total_days > 30.0 - span = translate(locale, "`x` months", (span.total_days.to_i // 30).to_s) + return translate_count(locale, "generic_count_months", span.total_days.to_i // 30) elsif span.total_days > 7.0 - span = translate(locale, "`x` weeks", (span.total_days.to_i // 7).to_s) + return translate_count(locale, "generic_count_weeks", span.total_days.to_i // 7) elsif span.total_hours > 24.0 - span = translate(locale, "`x` days", (span.total_days.to_i).to_s) + return translate_count(locale, "generic_count_days", span.total_days.to_i) elsif span.total_minutes > 60.0 - span = translate(locale, "`x` hours", (span.total_hours.to_i).to_s) + return translate_count(locale, "generic_count_hours", span.total_hours.to_i) elsif span.total_seconds > 60.0 - span = translate(locale, "`x` minutes", (span.total_minutes.to_i).to_s) + return translate_count(locale, "generic_count_minutes", span.total_minutes.to_i) else - span = translate(locale, "`x` seconds", (span.total_seconds.to_i).to_s) + return translate_count(locale, "generic_count_seconds", span.total_seconds.to_i) end - - return span end def number_with_separator(number) From 5bb2cb7d71b3e44e7754a4e06e45c831d30211fc Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 27 Dec 2021 15:17:50 +0100 Subject: [PATCH 08/12] i18n: Use plurals for video/view/subscriber/subscription counts --- locales/en-US.json | 30 +++++++------------- locales/fa.json | 25 ++++------------ locales/fr.json | 30 +++++++------------- locales/id.json | 25 ++++------------ locales/it.json | 30 +++++++------------- locales/ja.json | 25 ++++------------ locales/ko.json | 25 ++++------------ locales/vi.json | 9 ++---- locales/zh-CN.json | 25 ++++------------ locales/zh-TW.json | 25 ++++------------ src/invidious/channels/community.cr | 2 +- src/invidious/helpers/i18n.cr | 19 +++++++++++-- src/invidious/views/components/item.ecr | 10 +++---- src/invidious/views/edit_playlist.ecr | 2 +- src/invidious/views/feeds/history.ecr | 4 +-- src/invidious/views/playlist.ecr | 4 +-- src/invidious/views/subscription_manager.ecr | 2 +- src/invidious/views/watch.ecr | 2 +- 18 files changed, 92 insertions(+), 202 deletions(-) diff --git a/locales/en-US.json b/locales/en-US.json index 166143ac..9d3a70d2 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -1,16 +1,14 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` subscriber", - "": "`x` subscribers" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` video", - "": "`x` videos" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` playlist", - "": "`x` playlists" - }, + "generic_views_count": "{{count}} view", + "generic_views_count_plural": "{{count}} views", + "generic_videos_count": "{{count}} video", + "generic_videos_count_plural": "{{count}} videos", + "generic_playlists_count": "{{count}} playlist", + "generic_playlists_count_plural": "{{count}} playlists", + "generic_subscribers_count": "{{count}} subscriber", + "generic_subscribers_count_plural": "{{count}} subscribers", + "generic_subscriptions_count": "{{count}} subscription", + "generic_subscriptions_count_plural": "{{count}} subscriptions", "LIVE": "LIVE", "Shared `x` ago": "Shared `x` ago", "Unsubscribe": "Unsubscribe", @@ -146,10 +144,6 @@ "Subscription manager": "Subscription manager", "Token manager": "Token manager", "Token": "Token", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` subscription", - "": "`x` subscriptions" - }, "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` token", "": "`x` tokens" @@ -195,10 +189,6 @@ "Whitelisted regions: ": "Whitelisted regions: ", "Blacklisted regions: ": "Blacklisted regions: ", "Shared `x`": "Shared `x`", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` view", - "": "`x` views" - }, "Premieres in `x`": "Premieres in `x`", "Premieres `x`": "Premieres `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.", diff --git a/locales/fa.json b/locales/fa.json index d8df2b4f..22ca416c 100644 --- a/locales/fa.json +++ b/locales/fa.json @@ -1,16 +1,9 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` دنبال کننده", - "": "`x` دنبال کننده" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` ویدئو", - "": "`x` ویدئو" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` فهرست پخش", - "": "`x` فهرست پخش" - }, + "generic_views_count_0": "{{count}} بازدید", + "generic_videos_count_0": "{{count}} ویدئو", + "generic_playlists_count_0": "{{count}} فهرست پخش", + "generic_subscribers_count_0": "{{count}} دنبال کننده", + "generic_subscriptions_count_0": "{{count}} اشتراک ها", "LIVE": "زنده", "Shared `x` ago": "`x` پیش به اشتراک گذاشته شده", "Unsubscribe": "لغو اشتراک", @@ -127,10 +120,6 @@ "Subscription manager": "مدیریت اشتراک", "Token manager": "مدیر توکن", "Token": "توکن", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` اشتراک ها", - "": "`x` اشتراک ها" - }, "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` توکن ها", "": "`x` توکن ها" @@ -176,10 +165,6 @@ "Whitelisted regions: ": "مناطق لیست سفید: ", "Blacklisted regions: ": "مناطق لیست سیاه: ", "Shared `x`": "به اشتراک گذاشته شده `x`", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` بازدید", - "": "`x` بازدید" - }, "Premieres in `x`": "برای اولین بار در `x`", "Premieres `x`": "برای اولین بار `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "سلام! مثل اینکه تو جاوا اسکریپت رو خاموش کرده ای. اینجا کلیک کن تا نظرات را ببینی، این رو یادت باشه که ممکنه بارگذاری اونها کمی طول بکشه.", diff --git a/locales/fr.json b/locales/fr.json index f9975a6b..d14a20ac 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -1,16 +1,14 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` abonné", - "": "`x` abonnés" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` vidéo", - "": "`x` vidéos" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` liste de lecture", - "": "`x` listes de lecture" - }, + "generic_views_count": "{{count}} vue", + "generic_views_count_plural":"{{count}} vues", + "generic_videos_count": "{{count}} vidéo", + "generic_videos_count_plural": "{{count}} vidéos", + "generic_playlists_count": "{{count}} liste de lecture", + "generic_playlists_count_plural": "{{count}} listes de lecture", + "generic_subscribers_count": "{{count}} abonné", + "generic_subscribers_count_plural": "{{count}} abonnés", + "generic_subscriptions_count": "{{count}} abonnement", + "generic_subscriptions_count_plural": "{{count}} abonnements", "LIVE": "EN DIRECT", "Shared `x` ago": "Ajoutée il y a `x`", "Unsubscribe": "Se désabonner", @@ -127,10 +125,6 @@ "Subscription manager": "Gestionnaire d'abonnement", "Token manager": "Gestionnaire de token", "Token": "Token", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` abonnements", - "": "`x` abonnements" - }, "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` token", "": "`x` tokens" @@ -176,10 +170,6 @@ "Whitelisted regions: ": "Régions sur liste blanche : ", "Blacklisted regions: ": "Régions sur liste noire : ", "Shared `x`": "Ajoutée le `x`", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` vues", - "": "`x` vues" - }, "Premieres in `x`": "Première dans `x`", "Premieres `x`": "Première le `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "Il semblerait que JavaScript soit désactivé. Cliquez ici pour voir les commentaires, mais gardez à l'esprit que le chargement peut prendre plus de temps.", diff --git a/locales/id.json b/locales/id.json index 78f5e773..949cc69a 100644 --- a/locales/id.json +++ b/locales/id.json @@ -1,16 +1,9 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` pelanggan", - "": "`x` pelanggan" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` video", - "": "`x` video" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` daftar putar", - "": "`x` daftar putar" - }, + "generic_views_count_0": "{{count}} tampilan", + "generic_videos_count_0": "{{count}} video", + "generic_playlists_count_0": "{{count}} daftar putar", + "generic_subscribers_count_0": "{{count}} pelanggan", + "generic_subscriptions_count_0": "{{count}} langganan", "LIVE": "SIARAN LANGSUNG", "Shared `x` ago": "Dibagikan `x` yang lalu", "Unsubscribe": "Batal Langganan", @@ -127,10 +120,6 @@ "Subscription manager": "Pengatur langganan", "Token manager": "Pengatur token", "Token": "Token", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` langganan", - "": "`x` langganan" - }, "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` token", "": "`x` token" @@ -176,10 +165,6 @@ "Whitelisted regions: ": "Wilayah daftar-putih: ", "Blacklisted regions: ": "Wilayah daftar-hitam: ", "Shared `x`": "Berbagi `x`", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` tampilan", - "": "`x` tampilan" - }, "Premieres in `x`": "Tayang dalam `x`", "Premieres `x`": "Tayang `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "Hai! Kelihatannya JavaScript kamu dimatikan. Klik di sini untuk melihat komentar, perlu diingat hal ini mungkin membutuhkan waktu sedikit lebih lama untuk dimuat.", diff --git a/locales/it.json b/locales/it.json index befdd665..2722e7bb 100644 --- a/locales/it.json +++ b/locales/it.json @@ -1,16 +1,10 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` iscritto", - "": "`x` iscritti" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` video", - "": "`x` video" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` playlist", - "": "`x` playlist" - }, + "generic_subscribers_count": "{{count}} iscritto", + "generic_subscribers_count_plural": "{{count}} iscritti", + "generic_videos_count": "{{count}} video", + "generic_videos_count_plural": "{{count}} video", + "generic_playlists_count": "{{count}} playlist", + "generic_playlists_count_plural": "{{count}} playlist", "LIVE": "IN DIRETTA", "Shared `x` ago": "Condiviso `x` fa", "Unsubscribe": "Disiscriviti", @@ -122,10 +116,8 @@ "Subscription manager": "Gestione delle iscrizioni", "Token manager": "Gestione dei gettoni", "Token": "Gettone", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` iscrizione", - "": "`x` iscrizioni" - }, + "generic_subscriptions_count": "{{count}} iscrizione", + "generic_subscriptions_count_plural": "{{count}} iscrizioni", "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` gettone", "": "`x` gettoni" @@ -166,10 +158,8 @@ "Whitelisted regions: ": "Regioni in lista bianca: ", "Blacklisted regions: ": "Regioni in lista nera: ", "Shared `x`": "Condiviso `x`", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` visualizzazione", - "": "`x` visualizzazioni" - }, + "generic_views_count": "{{count}} visualizzazione", + "generic_views_count_plural": "{{count}} visualizzazioni", "Premieres in `x`": "In anteprima in `x`", "Premieres `x`": "In anteprima `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "Ciao! Sembra che tu abbia disattivato JavaScript. Clicca qui per visualizzare i commenti. Considera che potrebbe volerci più tempo.", diff --git a/locales/ja.json b/locales/ja.json index 7423d2ca..52406f0d 100644 --- a/locales/ja.json +++ b/locales/ja.json @@ -1,16 +1,9 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 人の登録者", - "": "`x` 人の登録者" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 個の動画", - "": "`x` 個の動画" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 個の再生リスト", - "": "`x` 個の再生リスト" - }, + "generic_views_count_0": "{{count}} 回視聴", + "generic_videos_count_0": "{{count}} 個の動画", + "generic_playlists_count_0": "{{count}} 個の再生リスト", + "generic_subscribers_count_0": "{{count}} 人の登録者", + "generic_subscriptions_count_0": "{{count}} 個の登録チャンネル", "LIVE": "ライブ", "Shared `x` ago": "`x`前に共有", "Unsubscribe": "登録解除", @@ -127,10 +120,6 @@ "Subscription manager": "登録チャンネルマネージャー", "Token manager": "トークンマネージャー", "Token": "トークン", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 個の登録チャンネル", - "": "`x` 個の登録チャンネル" - }, "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 個のトークン", "": "`x` 個のトークン" @@ -176,10 +165,6 @@ "Whitelisted regions: ": "ホワイトリストの地域: ", "Blacklisted regions: ": "ブラックリストの地域: ", "Shared `x`": "`x`に共有", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 回視聴", - "": "`x` 回視聴" - }, "Premieres in `x`": "`x`後にプレミア公開", "Premieres `x`": "`x`にプレミア公開", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "やあ!君は JavaScript を無効にしているのかな?ここをクリックしてコメントを見れるけど、読み込みには少し時間がかかることがあるのを覚えておいてね。", diff --git a/locales/ko.json b/locales/ko.json index 96fd41ff..16cf59b9 100644 --- a/locales/ko.json +++ b/locales/ko.json @@ -81,18 +81,11 @@ "Subscribe": "구독", "Unsubscribe": "구독 취소", "LIVE": "실시간", - "`x` playlists": { - "": "`x` 재생목록", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 재생목록" - }, - "`x` videos": { - "": "`x` 동영상", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 동영상" - }, - "`x` subscribers": { - "": "`x` 구독자", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 구독자" - }, + "generic_views_count_0": "{{count}} 조회수", + "generic_videos_count_0": "{{count}} 동영상", + "generic_playlists_count_0": "{{count}} 재생목록", + "generic_subscribers_count_0": "{{count}} 구독자", + "generic_subscriptions_count_0": "{{count}} 구독", "playlist": "재생목록", "Korean": "한국어", "Japanese": "일본어", @@ -158,10 +151,6 @@ "": "`x` 토큰", "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 토큰" }, - "`x` subscriptions": { - "": "`x` 구독", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 구독" - }, "Token": "토큰", "Token manager": "토큰 관리자", "Subscription manager": "구독 관리자", @@ -300,10 +289,6 @@ "Shared `x`": "공유된 `x`", "Whitelisted regions: ": "차단되지 않은 지역: ", "views": "조회수", - "`x` views": { - "": "`x` 조회수", - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 조회수" - }, "Please log in": "로그인하세요", "Password cannot be longer than 55 characters": "비밀번호는 55자 이하여야 합니다", "Password cannot be empty": "비밀번호는 비워둘 수 없습니다", diff --git a/locales/vi.json b/locales/vi.json index e433ad55..a8550686 100644 --- a/locales/vi.json +++ b/locales/vi.json @@ -1,11 +1,6 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` subscribers", - "": "`x` subscribers" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` video" - }, + "generic_videos_count_0": "{{count}} video", + "generic_subscribers_count_0": "{{count}} subscribers", "LIVE": "TRỰC TIẾP", "Shared `x` ago": "Đã chia sẻ` x` trước", "Unsubscribe": "Hủy đăng ký", diff --git a/locales/zh-CN.json b/locales/zh-CN.json index 6108a680..f3a6bd98 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -1,16 +1,9 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 位订阅者", - "": "`x` 位订阅者" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 个视频", - "": "`x` 个视频" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 个播放列表", - "": "`x` 个播放列表" - }, + "generic_views_count_0": "{{count}} 播放", + "generic_videos_count_0": "{{count}} 个视频", + "generic_playlists_count_0": "{{count}} 个播放列表", + "generic_subscribers_count_0": "{{count}} 位订阅者", + "generic_subscriptions_count_0": "{{count}} 个订阅", "LIVE": "直播", "Shared `x` ago": "`x` 前分享", "Unsubscribe": "取消订阅", @@ -127,10 +120,6 @@ "Subscription manager": "订阅管理器", "Token manager": "令牌管理器", "Token": "令牌", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 个订阅", - "": "`x` 个订阅" - }, "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 个令牌", "": "`x` 个令牌" @@ -176,10 +165,6 @@ "Whitelisted regions: ": "白名单地区: ", "Blacklisted regions: ": "黑名单地区: ", "Shared `x`": "`x`发布", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 播放", - "": "`x` 次观看" - }, "Premieres in `x`": "首映于 `x` 后", "Premieres `x`": "首映于 `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "你好!看起来你关闭了 JavaScript。点击这里阅读评论。注意它们加载的时间可能会稍长。", diff --git a/locales/zh-TW.json b/locales/zh-TW.json index d3580c4d..1954e34a 100644 --- a/locales/zh-TW.json +++ b/locales/zh-TW.json @@ -1,16 +1,9 @@ { - "`x` subscribers": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 個訂閱者", - "": "`x` 個訂閱者" - }, - "`x` videos": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 部影片", - "": "`x` 部影片" - }, - "`x` playlists": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 播放清單", - "": "`x` 播放清單" - }, + "generic_views_count_0": "{{count}} 次檢視", + "generic_videos_count_0": "{{count}} 部影片", + "generic_playlists_count_0": "{{count}} 播放清單", + "generic_subscribers_count_0": "{{count}} 個訂閱者", + "generic_subscriptions_count_0": "{{count}} 個訂閱", "LIVE": "直播", "Shared `x` ago": "`x` 前分享", "Unsubscribe": "取消訂閱", @@ -127,10 +120,6 @@ "Subscription manager": "訂閱管理員", "Token manager": "Token 管理員", "Token": "Token", - "`x` subscriptions": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 個訂閱", - "": "`x` 個訂閱" - }, "`x` tokens": { "([^.,0-9]|^)1([^.,0-9]|$)": "`x` token", "": "`x` 個存取金鑰" @@ -176,10 +165,6 @@ "Whitelisted regions: ": "白名單區域: ", "Blacklisted regions: ": "黑名單區域: ", "Shared `x`": "`x` 發佈", - "`x` views": { - "([^.,0-9]|^)1([^.,0-9]|$)": "`x` 次檢視", - "": "`x` 次檢視" - }, "Premieres in `x`": "首映於 `x`", "Premieres `x`": "首映於 `x`", "Hi! Looks like you have JavaScript turned off. Click here to view comments, keep in mind they may take a bit longer to load.": "嗨!看來您將 JavaScript 關閉了。點擊這裡以檢視留言,請注意,它們可能需要比較長的時間載入。", diff --git a/src/invidious/channels/community.cr b/src/invidious/channels/community.cr index 9a50f893..4701ecbd 100644 --- a/src/invidious/channels/community.cr +++ b/src/invidious/channels/community.cr @@ -158,7 +158,7 @@ def fetch_channel_community(ucid, continuation, locale, format, thin_mode) view_count = attachment["viewCountText"]?.try &.["simpleText"].as_s.gsub(/\D/, "").to_i64? || 0_i64 json.field "viewCount", view_count - json.field "viewCountText", translate(locale, "`x` views", number_to_short_text(view_count)) + json.field "viewCountText", translate_count(locale, "generic_views_count", view_count, NumberFormatting::Short) end when .has_key?("backstageImageRenderer") attachment = attachment["backstageImageRenderer"] diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.cr index 316e5cda..c7b63f04 100644 --- a/src/invidious/helpers/i18n.cr +++ b/src/invidious/helpers/i18n.cr @@ -54,6 +54,14 @@ CONTENT_REGIONS = { "YE", "ZA", "ZW", } +# Enum for the different types of number formats +enum NumberFormatting + None # Print the number as-is + Separator # Use a separator for thousands + Short # Use short notation (k/M/B) + HtmlSpan # Surround with +end + def load_all_locales locales = {} of String => Hash(String, JSON::Any) @@ -107,7 +115,7 @@ def translate(locale : String?, key : String, text : String | Nil = nil) : Strin return translation end -def translate_count(locale : String, key : String, count : Int) : String +def translate_count(locale : String, key : String, count : Int, format = NumberFormatting::None) : String # Fallback on english if locale doesn't exist locale = "en-US" if !LOCALES.has_key?(locale) @@ -134,7 +142,14 @@ def translate_count(locale : String, key : String, count : Int) : String end end - return translation.gsub("{{count}}", count.to_s) + case format + when .separator? then count_txt = number_with_separator(count) + when .short? then count_txt = number_to_short_text(count) + when .html_span? then count_txt = "" + count.to_s + "" + else count_txt = count.to_s + end + + return translation.gsub("{{count}}", count_txt) end def translate_bool(locale : String?, translation : Bool) diff --git a/src/invidious/views/components/item.ecr b/src/invidious/views/components/item.ecr index a58571aa..5a93d802 100644 --- a/src/invidious/views/components/item.ecr +++ b/src/invidious/views/components/item.ecr @@ -10,8 +10,8 @@ <% end %>

<%= HTML.escape(item.author) %>

-

<%= translate(locale, "`x` subscribers", number_with_separator(item.subscriber_count)) %>

- <% if !item.auto_generated %>

<%= translate(locale, "`x` videos", number_with_separator(item.video_count)) %>

<% end %> +

<%= translate_count(locale, "generic_subscribers_count", item.subscriber_count, NumberFormatting::Separator) %>

+ <% if !item.auto_generated %>

<%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %>

<% end %>
<%= item.description_html %>
<% when SearchPlaylist, InvidiousPlaylist %> <% if item.id.starts_with? "RD" %> @@ -24,7 +24,7 @@ <% if !env.get("preferences").as(Preferences).thin_mode %>
"/> -

<%= number_with_separator(item.video_count) %> videos

+

<%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %>

<% end %>

<%= HTML.escape(item.title) %>

@@ -94,7 +94,7 @@ <% if item.responds_to?(:views) && item.views %>
-

<%= translate(locale, "`x` views", number_to_short_text(item.views || 0)) %>

+

<%= translate_count(locale, "generic_views_count", item.views || 0, NumberFormatting::Short) %>

<% end %> @@ -160,7 +160,7 @@ <% if item.responds_to?(:views) && item.views %>
-

<%= translate(locale, "`x` views", number_to_short_text(item.views || 0)) %>

+

<%= translate_count(locale, "generic_views_count", item.views || 0, NumberFormatting::Short) %>

<% end %> diff --git a/src/invidious/views/edit_playlist.ecr b/src/invidious/views/edit_playlist.ecr index 5046abc1..308bd677 100644 --- a/src/invidious/views/edit_playlist.ecr +++ b/src/invidious/views/edit_playlist.ecr @@ -11,7 +11,7 @@

<%= HTML.escape(playlist.author) %> | - <%= translate(locale, "`x` videos", "#{playlist.video_count}") %> | + <%= translate_count(locale, "generic_videos_count", playlist.video_count) %> | <%= translate(locale, "Updated `x` ago", recode_date(playlist.updated, locale)) %> | ">