summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/invidious/channels/about.cr139
-rw-r--r--src/invidious/channels/community.cr2
-rw-r--r--src/invidious/comments.cr10
-rw-r--r--src/invidious/helpers/i18n.cr44
-rw-r--r--src/invidious/helpers/i18next.cr511
-rw-r--r--src/invidious/helpers/utils.cr16
-rw-r--r--src/invidious/jobs/refresh_channels_job.cr4
-rw-r--r--src/invidious/routes/api/v1/channels.cr2
-rw-r--r--src/invidious/views/components/item.ecr10
-rw-r--r--src/invidious/views/edit_playlist.ecr2
-rw-r--r--src/invidious/views/feeds/history.ecr4
-rw-r--r--src/invidious/views/feeds/subscriptions.ecr2
-rw-r--r--src/invidious/views/playlist.ecr4
-rw-r--r--src/invidious/views/subscription_manager.ecr2
-rw-r--r--src/invidious/views/token_manager.ecr2
-rw-r--r--src/invidious/views/watch.ecr2
16 files changed, 649 insertions, 107 deletions
diff --git a/src/invidious/channels/about.cr b/src/invidious/channels/about.cr
index d93ee681..8cae7ae2 100644
--- a/src/invidious/channels/about.cr
+++ b/src/invidious/channels/about.cr
@@ -1,33 +1,26 @@
# TODO: Refactor into either SearchChannel or InvidiousChannel
-struct AboutChannel
- include DB::Serializable
-
- property ucid : String
- property author : String
- property auto_generated : Bool
- property author_url : String
- property author_thumbnail : String
- property banner : String?
- property description_html : String
- property total_views : Int64
- property sub_count : Int32
- property joined : Time
- property is_family_friendly : Bool
- property allowed_regions : Array(String)
- property related_channels : Array(AboutRelatedChannel)
- property tabs : Array(String)
-end
-
-struct AboutRelatedChannel
- include DB::Serializable
-
- property ucid : String
- property author : String
- property author_url : String
- property author_thumbnail : String
-end
-
-def get_about_info(ucid, locale)
+record AboutChannel,
+ ucid : String,
+ author : String,
+ auto_generated : Bool,
+ author_url : String,
+ author_thumbnail : String,
+ banner : String?,
+ description_html : String,
+ total_views : Int64,
+ sub_count : Int32,
+ joined : Time,
+ is_family_friendly : Bool,
+ allowed_regions : Array(String),
+ tabs : Array(String)
+
+record AboutRelatedChannel,
+ ucid : String,
+ author : String,
+ author_url : String,
+ author_thumbnail : String
+
+def get_about_info(ucid, locale) : AboutChannel
begin
# "EgVhYm91dA==" is the base64-encoded protobuf object {"2:string":"about"}
initdata = YoutubeAPI.browse(browse_id: ucid, params: "EgVhYm91dA==")
@@ -63,8 +56,6 @@ def get_about_info(ucid, locale)
is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool
allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s)
-
- related_channels = [] of AboutRelatedChannel
else
author = initdata["metadata"]["channelMetadataRenderer"]["title"].as_s
author_url = initdata["metadata"]["channelMetadataRenderer"]["channelUrl"].as_s
@@ -85,38 +76,6 @@ def get_about_info(ucid, locale)
is_family_friendly = initdata["microformat"]["microformatDataRenderer"]["familySafe"].as_bool
allowed_regions = initdata["microformat"]["microformatDataRenderer"]["availableCountries"].as_a.map(&.as_s)
-
- related_channels = initdata["contents"]["twoColumnBrowseResultsRenderer"]
- .["secondaryContents"]?.try &.["browseSecondaryContentsRenderer"]["contents"][0]?
- .try &.["verticalChannelSectionRenderer"]?.try &.["items"]?.try &.as_a.map do |node|
- renderer = node["miniChannelRenderer"]?
- related_id = renderer.try &.["channelId"]?.try &.as_s?
- related_id ||= ""
-
- related_title = renderer.try &.["title"]?.try &.["simpleText"]?.try &.as_s?
- related_title ||= ""
-
- related_author_url = renderer.try &.["navigationEndpoint"]?.try &.["commandMetadata"]?.try &.["webCommandMetadata"]?
- .try &.["url"]?.try &.as_s?
- related_author_url ||= ""
-
- related_author_thumbnails = renderer.try &.["thumbnail"]?.try &.["thumbnails"]?.try &.as_a?
- related_author_thumbnails ||= [] of JSON::Any
-
- related_author_thumbnail = ""
- if related_author_thumbnails.size > 0
- related_author_thumbnail = related_author_thumbnails[-1]["url"]?.try &.as_s?
- related_author_thumbnail ||= ""
- end
-
- AboutRelatedChannel.new({
- ucid: related_id,
- author: related_title,
- author_url: related_author_url,
- author_thumbnail: related_author_thumbnail,
- })
- end
- related_channels ||= [] of AboutRelatedChannel
end
total_views = 0_i64
@@ -155,20 +114,44 @@ def get_about_info(ucid, locale)
sub_count = initdata["header"]["c4TabbedHeaderRenderer"]?.try &.["subscriberCountText"]?.try &.["simpleText"]?.try &.as_s?
.try { |text| short_text_to_number(text.split(" ")[0]) } || 0
- AboutChannel.new({
- ucid: ucid,
- author: author,
- auto_generated: auto_generated,
- author_url: author_url,
- author_thumbnail: author_thumbnail,
- banner: banner,
- description_html: description_html,
- total_views: total_views,
- sub_count: sub_count,
- joined: joined,
+ AboutChannel.new(
+ ucid: ucid,
+ author: author,
+ auto_generated: auto_generated,
+ author_url: author_url,
+ author_thumbnail: author_thumbnail,
+ banner: banner,
+ description_html: description_html,
+ total_views: total_views,
+ sub_count: sub_count,
+ joined: joined,
is_family_friendly: is_family_friendly,
- allowed_regions: allowed_regions,
- related_channels: related_channels,
- tabs: tabs,
- })
+ allowed_regions: allowed_regions,
+ tabs: tabs,
+ )
+end
+
+def fetch_related_channels(about_channel : AboutChannel) : Array(AboutRelatedChannel)
+ # params is {"2:string":"channels"} encoded
+ channels = YoutubeAPI.browse(browse_id: about_channel.ucid, params: "EghjaGFubmVscw%3D%3D")
+
+ tabs = channels.dig?("contents", "twoColumnBrowseResultsRenderer", "tabs").try(&.as_a?) || [] of JSON::Any
+ tab = tabs.find { |tab| tab.dig?("tabRenderer", "title").try(&.as_s?) == "Channels" }
+ return [] of AboutRelatedChannel if tab.nil?
+
+ items = tab.dig?("tabRenderer", "content", "sectionListRenderer", "contents", 0, "itemSectionRenderer", "contents", 0, "gridRenderer", "items").try(&.as_a?) || [] of JSON::Any
+
+ items.map do |item|
+ related_id = item.dig("gridChannelRenderer", "channelId").as_s
+ related_title = item.dig("gridChannelRenderer", "title", "simpleText").as_s
+ related_author_url = item.dig("gridChannelRenderer", "navigationEndpoint", "browseEndpoint", "canonicalBaseUrl").as_s
+ related_author_thumbnail = item.dig("gridChannelRenderer", "thumbnail", "thumbnails", -1, "url").as_s
+
+ AboutRelatedChannel.new(
+ ucid: related_id,
+ author: related_title,
+ author_url: related_author_url,
+ author_thumbnail: related_author_thumbnail,
+ )
+ end
end
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/comments.cr b/src/invidious/comments.cr
index 5b7d63e0..256a294e 100644
--- a/src/invidious/comments.cr
+++ b/src/invidious/comments.cr
@@ -303,13 +303,19 @@ def template_youtube_comments(comments, locale, thin_mode, is_replies = false)
root = comments["comments"].as_a
root.each do |child|
if child["replies"]?
+ replies_count_text = translate_count(locale,
+ "comments_view_x_replies",
+ child["replies"]["replyCount"].as_i64 || 0,
+ NumberFormatting::Separator
+ )
+
replies_html = <<-END_HTML
<div id="replies" class="pure-g">
<div class="pure-u-1-24"></div>
<div class="pure-u-23-24">
<p>
<a href="javascript:void(0)" data-continuation="#{child["replies"]["continuation"]}"
- data-onclick="get_youtube_replies" data-load-replies>#{translate(locale, "View `x` replies", number_with_separator(child["replies"]["replyCount"]))}</a>
+ data-onclick="get_youtube_replies" data-load-replies>#{replies_count_text}</a>
</p>
</div>
</div>
@@ -471,7 +477,7 @@ def template_reddit_comments(root, locale)
<p>
<a href="javascript:void(0)" data-onclick="toggle_parent">[ - ]</a>
<b><a href="https://www.reddit.com/user/#{child.author}">#{child.author}</a></b>
- #{translate(locale, "`x` points", number_with_separator(child.score))}
+ #{translate_count(locale, "comments_points_count", child.score, NumberFormatting::Separator)}
<span title="#{child.created_utc.to_s(translate(locale, "%a %B %-d %T %Y UTC"))}">#{translate(locale, "`x` ago", recode_date(child.created_utc, locale))}</span>
<a href="https://www.reddit.com#{child.permalink}" title="#{translate(locale, "permalink")}">#{translate(locale, "permalink")}</a>
</p>
diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.cr
index fd3ddbad..e88e4491 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 <span id="count"></span>
+end
+
def load_all_locales
locales = {} of String => Hash(String, JSON::Any)
@@ -107,6 +115,42 @@ def translate(locale : String?, key : String, text : String | Nil = nil) : Strin
return translation
end
+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)
+
+ # 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
+ elsif locale != "en-US"
+ # Try #2: Fallback to english
+ translation = translate_count("en-US", key, count)
+ else
+ # Return key if we're already in english, as the tranlation is missing
+ LOGGER.warn("i18n: Missing translation key \"#{key}\"")
+ return key
+ end
+ end
+
+ 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 = "<span id=\"count\">" + count.to_s + "</span>"
+ else count_txt = count.to_s
+ end
+
+ return translation.gsub("{{count}}", count_txt)
+end
+
def translate_bool(locale : String?, translation : Bool)
case translation
when true
diff --git a/src/invidious/helpers/i18next.cr b/src/invidious/helpers/i18next.cr
new file mode 100644
index 00000000..e84f88fb
--- /dev/null
+++ b/src/invidious/helpers/i18next.cr
@@ -0,0 +1,511 @@
+# I18next-compatible implementation of plural forms
+#
+module I18next::Plurals
+ # -----------------------------------
+ # I18next plural forms definition
+ # -----------------------------------
+
+ 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
+ Special_Odia = 23
+ 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,
+ "or" => PluralForms::Special_Odia,
+ "pl" => PluralForms::Special_Polish_Kashubian,
+ "ro" => PluralForms::Special_Romanian,
+ "sk" => PluralForms::Special_Czech_Slovak,
+ "sl" => PluralForms::Special_Slovenian,
+ }
+
+ # These are the v1 and v2 compatible suffixes.
+ # 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
+ [2, 1], # 23 (Odia)
+ ]
+
+ # -----------------------------------
+ # I18next plural resolver class
+ # -----------------------------------
+
+ RESOLVER = Resolver.new
+
+ class Resolver
+ private property forms = {} of String => PluralForms
+ property version : UInt8 = 3
+
+ # Options
+ property simplify_plural_suffix : Bool = true
+
+ 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_u8
+ else
+ @version = version.to_u8
+ end
+
+ self.init_rules
+ end
+
+ def init_rules
+ # Look into sets
+ PLURAL_SETS.each do |form, langs|
+ langs.each { |lang| self.forms[lang] = form }
+ end
+
+ # Add plurals from the "singles" set
+ 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 and pt-PT which needs to be kept as-is.
+ if !locale.matches?(/^pt-(BR|PT)$/)
+ locale = locale.split('-')[0]
+ end
+
+ 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,
+ # 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
+
+ # 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)
+
+ # 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)
+
+ # Simple plurals are handled differently in all versions (but v4)
+ if @simplify_plural_suffix && is_simple_plural(plural_form)
+ return (idx == 1) ? "_plural" : ""
+ end
+
+ # More complex plurals
+ # 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
+ # 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
+
+ # -----------------------------
+ # 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)
+ when .special_odia? then return special_odia(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 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
+ 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
+
+ # 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
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)
diff --git a/src/invidious/jobs/refresh_channels_job.cr b/src/invidious/jobs/refresh_channels_job.cr
index c224c745..941089c1 100644
--- a/src/invidious/jobs/refresh_channels_job.cr
+++ b/src/invidious/jobs/refresh_channels_job.cr
@@ -13,7 +13,7 @@ class Invidious::Jobs::RefreshChannelsJob < Invidious::Jobs::BaseJob
loop do
LOGGER.debug("RefreshChannelsJob: Refreshing all channels")
- db.query("SELECT id FROM channels ORDER BY updated") do |rs|
+ PG_DB.query("SELECT id FROM channels ORDER BY updated") do |rs|
rs.each do
id = rs.read(String)
@@ -30,7 +30,7 @@ class Invidious::Jobs::RefreshChannelsJob < Invidious::Jobs::BaseJob
spawn do
begin
LOGGER.trace("RefreshChannelsJob: #{id} fiber : Fetching channel")
- channel = fetch_channel(id, db, CONFIG.full_refresh)
+ channel = fetch_channel(id, CONFIG.full_refresh)
lim_fibers = max_fibers
diff --git a/src/invidious/routes/api/v1/channels.cr b/src/invidious/routes/api/v1/channels.cr
index 8b6df3fd..322ac42e 100644
--- a/src/invidious/routes/api/v1/channels.cr
+++ b/src/invidious/routes/api/v1/channels.cr
@@ -96,7 +96,7 @@ module Invidious::Routes::API::V1::Channels
json.field "relatedChannels" do
json.array do
- channel.related_channels.each do |related_channel|
+ fetch_related_channels(channel).each do |related_channel|
json.object do
json.field "author", related_channel.author
json.field "authorId", related_channel.ucid
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 %>
<p dir="auto"><%= HTML.escape(item.author) %></p>
</a>
- <p><%= translate(locale, "`x` subscribers", number_with_separator(item.subscriber_count)) %></p>
- <% if !item.auto_generated %><p><%= translate(locale, "`x` videos", number_with_separator(item.video_count)) %></p><% end %>
+ <p><%= translate_count(locale, "generic_subscribers_count", item.subscriber_count, NumberFormatting::Separator) %></p>
+ <% if !item.auto_generated %><p><%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %></p><% end %>
<h5><%= item.description_html %></h5>
<% when SearchPlaylist, InvidiousPlaylist %>
<% if item.id.starts_with? "RD" %>
@@ -24,7 +24,7 @@
<% if !env.get("preferences").as(Preferences).thin_mode %>
<div class="thumbnail">
<img loading="lazy" class="thumbnail" src="<%= URI.parse(item.thumbnail || "/").request_target %>"/>
- <p class="length"><%= number_with_separator(item.video_count) %> videos</p>
+ <p class="length"><%= translate_count(locale, "generic_videos_count", item.video_count, NumberFormatting::Separator) %></p>
</div>
<% end %>
<p dir="auto"><%= HTML.escape(item.title) %></p>
@@ -94,7 +94,7 @@
<% if item.responds_to?(:views) && item.views %>
<div class="flex-right">
- <p dir="auto"><%= translate(locale, "`x` views", number_to_short_text(item.views || 0)) %></p>
+ <p dir="auto"><%= translate_count(locale, "generic_views_count", item.views || 0, NumberFormatting::Short) %></p>
</div>
<% end %>
</div>
@@ -160,7 +160,7 @@
<% if item.responds_to?(:views) && item.views %>
<div class="flex-right">
- <p class="video-data" dir="auto"><%= translate(locale, "`x` views", number_to_short_text(item.views || 0)) %></p>
+ <p class="video-data" dir="auto"><%= translate_count(locale, "generic_views_count", item.views || 0, NumberFormatting::Short) %></p>
</div>
<% end %>
</div>
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 @@
<h3><input class="pure-input-1" maxlength="150" name="title" type="text" value="<%= title %>"></h3>
<b>
<%= 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)) %> |
<i class="icon <%= {"ion-md-globe", "ion-ios-unlock", "ion-ios-lock"}[playlist.privacy.value] %>"></i>
<select name="privacy">
diff --git a/src/invidious/views/feeds/history.ecr b/src/invidious/views/feeds/history.ecr
index 40584979..6c1243c5 100644
--- a/src/invidious/views/feeds/history.ecr
+++ b/src/invidious/views/feeds/history.ecr
@@ -4,11 +4,11 @@
<div class="pure-g h-box">
<div class="pure-u-1-3">
- <h3><%= translate(locale, "`x` videos", %(<span id="count">#{user.watched.size}</span>)) %></h3>
+ <h3><%= translate_count(locale, "generic_videos_count", user.watched.size, NumberFormatting::HtmlSpan) %></h3>
</div>
<div class="pure-u-1-3">
<h3 style="text-align:center">
- <a href="/feed/subscriptions"><%= translate(locale, "`x` subscriptions", %(<span id="count">#{user.subscriptions.size}</span>)) %></a>
+ <a href="/feed/subscriptions"><%= translate_count(locale, "generic_subscriptions_count", user.subscriptions.size, NumberFormatting::HtmlSpan) %></a>
</h3>
</div>
<div class="pure-u-1-3">
diff --git a/src/invidious/views/feeds/subscriptions.ecr b/src/invidious/views/feeds/subscriptions.ecr
index 97184e2b..8d56ad14 100644
--- a/src/invidious/views/feeds/subscriptions.ecr
+++ b/src/invidious/views/feeds/subscriptions.ecr
@@ -24,7 +24,7 @@
</div>
<center>
- <%= translate(locale, "`x` unseen notifications", "#{notifications.size}") %>
+ <%= translate_count(locale, "subscriptions_unseen_notifs_count", notifications.size) %>
</center>
<% if !notifications.empty? %>
diff --git a/src/invidious/views/playlist.ecr b/src/invidious/views/playlist.ecr
index b720bbc2..df3112db 100644
--- a/src/invidious/views/playlist.ecr
+++ b/src/invidious/views/playlist.ecr
@@ -16,7 +16,7 @@
<% else %>
<%= author %> |
<% end %>
- <%= 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)) %> |
<% case playlist.as(InvidiousPlaylist).privacy when %>
<% when PlaylistPrivacy::Public %>
@@ -30,7 +30,7 @@
<% else %>
<b>
<a href="/channel/<%= playlist.ucid %>"><%= author %></a> |
- <%= 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)) %>
</b>
<% end %>
diff --git a/src/invidious/views/subscription_manager.ecr b/src/invidious/views/subscription_manager.ecr
index acf015f5..5fa7d203 100644
--- a/src/invidious/views/subscription_manager.ecr
+++ b/src/invidious/views/subscription_manager.ecr
@@ -6,7 +6,7 @@
<div class="pure-u-1-3">
<h3>
<a href="/feed/subscriptions">
- <%= translate(locale, "`x` subscriptions", %(<span id="count">#{subscriptions.size}</span>)) %>
+ <%= translate_count(locale, "generic_subscriptions_count", subscriptions.size, NumberFormatting::HtmlSpan) %>
</a>
</h3>
</div>
diff --git a/src/invidious/views/token_manager.ecr b/src/invidious/views/token_manager.ecr
index e48aec2f..12e0e8c9 100644
--- a/src/invidious/views/token_manager.ecr
+++ b/src/invidious/views/token_manager.ecr
@@ -5,7 +5,7 @@
<div class="pure-g h-box">
<div class="pure-u-1-3">
<h3>
- <%= translate(locale, "`x` tokens", %(<span id="count">#{tokens.size}</span>)) %>
+ <%= translate_count(locale, "tokens_count", tokens.size, NumberFormatting::HtmlSpan) %>
</h3>
</div>
<div class="pure-u-1-3"></div>
diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr
index 363f1262..00f5f8b7 100644
--- a/src/invidious/views/watch.ecr
+++ b/src/invidious/views/watch.ecr
@@ -323,7 +323,7 @@ we're going to need to do it here in order to allow for translations.
<div class="pure-u-10-24" style="text-align:right">
<% if views = rv["short_view_count_text"]?.try &.delete(", views watching") %>
<% if !views.empty? %>
- <b class="width:100%"><%= translate(locale, "`x` views", views) %></b>
+ <b class="width:100%"><%= translate_count(locale, "generic_views_count", views.to_i? || 0) %></b>
<% end %>
<% end %>
</div>