summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2021-11-18 22:18:53 +0100
committerSamantaz Fox <coding@samantaz.fr>2022-01-10 22:26:45 +0100
commit67d2635e41313f4bd1fb4259dfc1ce8b923a498f (patch)
treee2e2a27602c1c5fc495a4082376fdf446b91e930
parent71a1ad307c89a343880e09b8ca0b610f2106511a (diff)
downloadinvidious-67d2635e41313f4bd1fb4259dfc1ce8b923a498f.tar.gz
invidious-67d2635e41313f4bd1fb4259dfc1ce8b923a498f.tar.bz2
invidious-67d2635e41313f4bd1fb4259dfc1ce8b923a498f.zip
i18n: Add i18next plural rules and selector
-rw-r--r--src/invidious/helpers/i18next.cr282
1 files changed, 281 insertions, 1 deletions
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