Invidious is an alternative front-end to YouTube
i18n: pass only the ISO code string to 'translate()'
Samantaz Fox139786b9ef90
diff --git a/src/invidious.cr b/src/invidious.crindex 21a12ff2..ade13608 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -408,7 +408,7 @@ define_video_playback_routes()# Userspost "/watch_ajax" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -478,7 +478,7 @@ end# /modify_notifications?receive_all_updates=false&receive_no_updates=false# will "unding" all subscriptions.get "/modify_notifications" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -551,7 +551,7 @@ get "/modify_notifications" do |env|endpost "/subscription_ajax" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -621,7 +621,7 @@ post "/subscription_ajax" do |env|endget "/subscription_manager" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -724,7 +724,7 @@ get "/subscription_manager" do |env|endget "/data_control" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"referer = get_referer(env)@@ -739,7 +739,7 @@ get "/data_control" do |env|endpost "/data_control" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"referer = get_referer(env)@@ -902,7 +902,7 @@ post "/data_control" do |env|endget "/change_password" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -920,7 +920,7 @@ get "/change_password" do |env|endpost "/change_password" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -976,7 +976,7 @@ post "/change_password" do |env|endget "/delete_account" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -994,7 +994,7 @@ get "/delete_account" do |env|endpost "/delete_account" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -1028,7 +1028,7 @@ post "/delete_account" do |env|endget "/clear_watch_history" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -1046,7 +1046,7 @@ get "/clear_watch_history" do |env|endpost "/clear_watch_history" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -1071,7 +1071,7 @@ post "/clear_watch_history" do |env|endget "/authorize_token" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -1099,7 +1099,7 @@ get "/authorize_token" do |env|endpost "/authorize_token" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -1147,7 +1147,7 @@ post "/authorize_token" do |env|endget "/token_manager" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -1165,7 +1165,7 @@ get "/token_manager" do |env|endpost "/token_ajax" do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -1225,7 +1225,7 @@ end{"/channel/:ucid/live", "/user/:user/live", "/c/:user/live"}.each do |route|get route do |env|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).locale# Appears to be a bug in routing, having several routes configured# as `/a/:a`, `/b/:a`, `/c/:a` results in 404@@ -1347,7 +1347,7 @@ error 404 do |env|enderror 500 do |env, ex|- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeerror_template(500, ex)enddiff --git a/src/invidious/helpers/errors.cr b/src/invidious/helpers/errors.crindex e5c77fbc..d10762c5 100644--- a/src/invidious/helpers/errors.cr+++ b/src/invidious/helpers/errors.cr@@ -22,7 +22,7 @@ def github_details(summary : String, content : String)return HTML.escape(details)end-def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)+def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)if exception.is_a?(InfoException)return error_template_helper(env, locale, status_code, exception.message || "")end@@ -46,7 +46,7 @@ def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSOreturn templated "error"end-def error_template_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)+def error_template_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)env.response.content_type = "text/html"env.response.status_code = status_codeerror_message = translate(locale, message)@@ -58,7 +58,7 @@ macro error_atom(*args)error_atom_helper(env, locale, {{*args}})end-def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)+def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)if exception.is_a?(InfoException)return error_atom_helper(env, locale, status_code, exception.message || "")end@@ -67,7 +67,7 @@ def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Areturn "<error>#{exception.inspect_with_backtrace}</error>"end-def error_atom_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)+def error_atom_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)env.response.content_type = "application/atom+xml"env.response.status_code = status_codereturn "<error>#{message}</error>"@@ -77,7 +77,7 @@ macro error_json(*args)error_json_helper(env, locale, {{*args}})end-def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)+def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception, additional_fields : Hash(String, Object) | Nil)if exception.is_a?(InfoException)return error_json_helper(env, locale, status_code, exception.message || "", additional_fields)end@@ -90,11 +90,11 @@ def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Areturn error_message.to_jsonend-def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, exception : Exception)+def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, exception : Exception)return error_json_helper(env, locale, status_code, exception, nil)end-def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)+def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String, additional_fields : Hash(String, Object) | Nil)env.response.content_type = "application/json"env.response.status_code = status_codeerror_message = {"error" => message}@@ -104,11 +104,11 @@ def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Areturn error_message.to_jsonend-def error_json_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil, status_code : Int32, message : String)+def error_json_helper(env : HTTP::Server::Context, locale : String?, status_code : Int32, message : String)error_json_helper(env, locale, status_code, message, nil)end-def error_redirect_helper(env : HTTP::Server::Context, locale : Hash(String, JSON::Any) | Nil)+def error_redirect_helper(env : HTTP::Server::Context, locale : String?)request_path = env.request.pathif request_path.starts_with?("/search") || request_path.starts_with?("/watch") ||diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.crindex c3b356a9..96a78eb9 100644--- a/src/invidious/helpers/helpers.cr+++ b/src/invidious/helpers/helpers.cr@@ -190,7 +190,7 @@ def create_notification_stream(env, topics, connection_channel)connection = Channel(PQ::Notification).new(8)connection_channel.send({true, connection})- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localesince = env.params.query["since"]?.try &.to_i?id = 0diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.crindex 0380ad1e..10622517 100644--- a/src/invidious/helpers/i18n.cr+++ b/src/invidious/helpers/i18n.cr@@ -64,31 +64,36 @@ def load_all_localesreturn localesend-def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, text : String | Nil = nil)- # if locale && !locale[translation]?- # puts "Could not find translation for #{translation.dump}"- # end+def translate(locale : String?, key : String, text : String | Nil = nil) : String+ # Raise an eception if "key" doesn't exist in en-US locale+ raise "Invalid translation key \"#{key}\"" unless LOCALES["en-US"].has_key?(key)- if locale && locale[translation]?- case locale[translation]- when .as_h?- match_length = 0+ # Default to english, whenever the locale doesn't exist,+ # or the key requested has not been translated+ if locale && LOCALES.has_key?(locale) && LOCALES[locale].has_key?(key)+ raw_data = LOCALES[locale][key]+ else+ raw_data = LOCALES["en-US"][key]+ end++ case raw_data+ when .as_h?+ # Init+ translation = ""+ match_length = 0- locale[translation].as_h.each do |key, value|- if md = text.try &.match(/#{key}/)- if md[0].size >= match_length- translation = value.as_s- match_length = md[0].size- end+ raw_data.as_h.each do |key, value|+ if md = text.try &.match(/#{key}/)+ if md[0].size >= match_length+ translation = value.as_s+ match_length = md[0].sizeendend- when .as_s?- if !locale[translation].as_s.empty?- translation = locale[translation].as_s- end- else- raise "Invalid translation #{translation}"end+ when .as_s?+ translation = raw_data.as_s+ else+ raise "Invalid translation \"#{raw_data}\""endif text@@ -98,7 +103,7 @@ def translate(locale : Hash(String, JSON::Any) | Nil, translation : String, textreturn translationend-def translate_bool(locale : Hash(String, JSON::Any) | Nil, translation : Bool)+def translate_bool(locale : String?, translation : Bool)case translationwhen truereturn translate(locale, "Yes")diff --git a/src/invidious/helpers/serialized_yt_data.cr b/src/invidious/helpers/serialized_yt_data.crindex f92b7b89..bfbc237c 100644--- a/src/invidious/helpers/serialized_yt_data.cr+++ b/src/invidious/helpers/serialized_yt_data.cr@@ -64,7 +64,7 @@ struct SearchVideoendend- def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)+ def to_json(locale : String?, json : JSON::Builder)json.object dojson.field "type", "video"json.field "title", self.title@@ -96,7 +96,7 @@ struct SearchVideoend# TODO: remove the locale and follow the crystal convention- def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)+ def to_json(locale : String?, _json : Nil)JSON.build do |json|to_json(locale, json)end@@ -130,7 +130,7 @@ struct SearchPlaylistproperty videos : Array(SearchPlaylistVideo)property thumbnail : String?- def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)+ def to_json(locale : String?, json : JSON::Builder)json.object dojson.field "type", "playlist"json.field "title", self.title@@ -161,7 +161,7 @@ struct SearchPlaylistend# TODO: remove the locale and follow the crystal convention- def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)+ def to_json(locale : String?, _json : Nil)JSON.build do |json|to_json(locale, json)end@@ -183,7 +183,7 @@ struct SearchChannelproperty description_html : Stringproperty auto_generated : Bool- def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)+ def to_json(locale : String?, json : JSON::Builder)json.object dojson.field "type", "channel"json.field "author", self.author@@ -214,7 +214,7 @@ struct SearchChannelend# TODO: remove the locale and follow the crystal convention- def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)+ def to_json(locale : String?, _json : Nil)JSON.build do |json|to_json(locale, json)end@@ -234,7 +234,7 @@ class Categoryproperty description_html : Stringproperty badges : Array(Tuple(String, String))?- def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)+ def to_json(locale : String?, json : JSON::Builder)json.object dojson.field "type", "category"json.field "title", self.title@@ -249,7 +249,7 @@ class Categoryend# TODO: remove the locale and follow the crystal convention- def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)+ def to_json(locale : String?, _json : Nil)JSON.build do |json|to_json(locale, json)enddiff --git a/src/invidious/routes/api/v1/authenticated.cr b/src/invidious/routes/api/v1/authenticated.crindex cdd9e2f6..aaf728ff 100644--- a/src/invidious/routes/api/v1/authenticated.cr+++ b/src/invidious/routes/api/v1/authenticated.cr@@ -36,7 +36,7 @@ module Invidious::Routes::API::V1::Authenticatedenv.response.content_type = "application/json"user = env.get("user").as(User)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localemax_results = env.params.query["max_results"]?.try &.to_i?max_results ||= user.preferences.max_results@@ -122,7 +122,7 @@ module Invidious::Routes::API::V1::Authenticatedenddef self.list_playlists(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"user = env.get("user").as(User)@@ -141,7 +141,7 @@ module Invidious::Routes::API::V1::Authenticateddef self.create_playlist(env)env.response.content_type = "application/json"user = env.get("user").as(User)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localetitle = env.params.json["title"]?.try &.as(String).delete("<>").byte_slice(0, 150)if !title@@ -167,7 +167,7 @@ module Invidious::Routes::API::V1::Authenticatedenddef self.update_playlist_attribute(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"user = env.get("user").as(User)@@ -200,7 +200,7 @@ module Invidious::Routes::API::V1::Authenticatedenddef self.delete_playlist(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"user = env.get("user").as(User)@@ -223,7 +223,7 @@ module Invidious::Routes::API::V1::Authenticatedenddef self.insert_video_into_playlist(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"user = env.get("user").as(User)@@ -281,7 +281,7 @@ module Invidious::Routes::API::V1::Authenticatedenddef self.delete_video_in_playlist(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"user = env.get("user").as(User)@@ -334,7 +334,7 @@ module Invidious::Routes::API::V1::Authenticateddef self.register_token(env)user = env.get("user").as(User)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localecase env.request.headers["Content-Type"]?when "application/x-www-form-urlencoded"@@ -396,7 +396,7 @@ module Invidious::Routes::API::V1::Authenticatedenddef self.unregister_token(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"user = env.get("user").as(User)scopes = env.get("scopes").as(Array(String))diff --git a/src/invidious/routes/api/v1/channels.cr b/src/invidious/routes/api/v1/channels.crindex da39661c..8b6df3fd 100644--- a/src/invidious/routes/api/v1/channels.cr+++ b/src/invidious/routes/api/v1/channels.cr@@ -1,6 +1,6 @@module Invidious::Routes::API::V1::Channelsdef self.home(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -124,7 +124,7 @@ module Invidious::Routes::API::V1::Channelsenddef self.latest(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -146,7 +146,7 @@ module Invidious::Routes::API::V1::Channelsenddef self.videos(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -182,7 +182,7 @@ module Invidious::Routes::API::V1::Channelsenddef self.playlists(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -219,7 +219,7 @@ module Invidious::Routes::API::V1::Channelsenddef self.community(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -242,7 +242,7 @@ module Invidious::Routes::API::V1::Channelsenddef self.search(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"diff --git a/src/invidious/routes/api/v1/feeds.cr b/src/invidious/routes/api/v1/feeds.crindex bb8f661b..41865f34 100644--- a/src/invidious/routes/api/v1/feeds.cr+++ b/src/invidious/routes/api/v1/feeds.cr@@ -1,6 +1,6 @@module Invidious::Routes::API::V1::Feedsdef self.trending(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -25,7 +25,7 @@ module Invidious::Routes::API::V1::Feedsenddef self.popular(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"diff --git a/src/invidious/routes/api/v1/misc.cr b/src/invidious/routes/api/v1/misc.crindex 80b59fd5..1621c9ef 100644--- a/src/invidious/routes/api/v1/misc.cr+++ b/src/invidious/routes/api/v1/misc.cr@@ -1,7 +1,7 @@module Invidious::Routes::API::V1::Misc# Stats API endpoint for Invidiousdef self.stats(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"if !CONFIG.statistics_enabled@@ -15,7 +15,7 @@ module Invidious::Routes::API::V1::Misc# user playlists and Invidious playlists. This means that we can't# reasonably split them yet. This should be addressed in APIv2def self.get_playlist(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"plid = env.params.url["plid"]@@ -84,7 +84,7 @@ module Invidious::Routes::API::V1::Miscenddef self.mixes(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"diff --git a/src/invidious/routes/api/v1/search.cr b/src/invidious/routes/api/v1/search.crindex 7234dcdd..a3b6c795 100644--- a/src/invidious/routes/api/v1/search.cr+++ b/src/invidious/routes/api/v1/search.cr@@ -1,6 +1,6 @@module Invidious::Routes::API::V1::Searchdef self.search(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeregion = env.params.query["region"]?env.response.content_type = "application/json"@@ -43,7 +43,7 @@ module Invidious::Routes::API::V1::Searchenddef self.search_suggestions(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeregion = env.params.query["region"]?env.response.content_type = "application/json"diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.crindex 1edee29c..4c7179ce 100644--- a/src/invidious/routes/api/v1/videos.cr+++ b/src/invidious/routes/api/v1/videos.cr@@ -1,6 +1,6 @@module Invidious::Routes::API::V1::Videosdef self.videos(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -20,7 +20,7 @@ module Invidious::Routes::API::V1::Videosenddef self.captions(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -149,7 +149,7 @@ module Invidious::Routes::API::V1::Videos# thumbnails for individual scenes in a video.# See https://support.jwplayer.com/articles/how-to-add-preview-thumbnailsdef self.storyboards(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/json"@@ -223,7 +223,7 @@ module Invidious::Routes::API::V1::Videosenddef self.annotations(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "text/xml"@@ -293,7 +293,7 @@ module Invidious::Routes::API::V1::Videosenddef self.comments(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeregion = env.params.query["region"]?env.response.content_type = "application/json"diff --git a/src/invidious/routes/channels.cr b/src/invidious/routes/channels.crindex 29748cd0..6cb1e1f7 100644--- a/src/invidious/routes/channels.cr+++ b/src/invidious/routes/channels.cr@@ -104,7 +104,7 @@ module Invidious::Routes::Channels# Redirects brand url channels to a normal /channel/:ucid routedef self.brand_redirect(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).locale# /attribution_link endpoint needs both the `a` and `u` parameter# and in order to avoid detection from YouTube we should only send the required ones@@ -148,7 +148,7 @@ module Invidious::Routes::Channelsendprivate def self.fetch_basic_information(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"if userdiff --git a/src/invidious/routes/embed.cr b/src/invidious/routes/embed.crindex ffbf8c14..049ee344 100644--- a/src/invidious/routes/embed.cr+++ b/src/invidious/routes/embed.cr@@ -2,7 +2,7 @@module Invidious::Routes::Embeddef self.redirect(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeif plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")begin@@ -26,7 +26,7 @@ module Invidious::Routes::Embedenddef self.show(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeid = env.params.url["id"]plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "")diff --git a/src/invidious/routes/feeds.cr b/src/invidious/routes/feeds.crindex f4a8467b..9650bcf4 100644--- a/src/invidious/routes/feeds.cr+++ b/src/invidious/routes/feeds.cr@@ -6,7 +6,7 @@ module Invidious::Routes::Feedsenddef self.playlists(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"referer = get_referer(env)@@ -31,7 +31,7 @@ module Invidious::Routes::Feedsenddef self.popular(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeif CONFIG.popular_enabledtemplated "feeds/popular"@@ -42,7 +42,7 @@ module Invidious::Routes::Feedsenddef self.trending(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localetrending_type = env.params.query["type"]?trending_type ||= "Default"@@ -60,7 +60,7 @@ module Invidious::Routes::Feedsenddef self.subscriptions(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -108,7 +108,7 @@ module Invidious::Routes::Feedsenddef self.history(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"referer = get_referer(env)@@ -137,7 +137,7 @@ module Invidious::Routes::Feeds# RSS feedsdef self.rss_channel(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.headers["Content-Type"] = "application/atom+xml"env.response.content_type = "application/atom+xml"@@ -209,7 +209,7 @@ module Invidious::Routes::Feedsenddef self.rss_private(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.headers["Content-Type"] = "application/atom+xml"env.response.content_type = "application/atom+xml"@@ -253,7 +253,7 @@ module Invidious::Routes::Feedsenddef self.rss_playlist(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.headers["Content-Type"] = "application/atom+xml"env.response.content_type = "application/atom+xml"@@ -374,7 +374,7 @@ module Invidious::Routes::Feedsenddef self.push_notifications_post(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localetoken = env.params.url["token"]body = env.request.body.not_nil!.gets_to_enddiff --git a/src/invidious/routes/login.cr b/src/invidious/routes/login.crindex 562d88e5..2a50561d 100644--- a/src/invidious/routes/login.cr+++ b/src/invidious/routes/login.cr@@ -2,7 +2,7 @@module Invidious::Routes::Logindef self.login_page(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"@@ -31,7 +31,7 @@ module Invidious::Routes::Loginenddef self.login(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localereferer = get_referer(env, "/feed/subscriptions")@@ -491,7 +491,7 @@ module Invidious::Routes::Loginenddef self.signout(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"diff --git a/src/invidious/routes/misc.cr b/src/invidious/routes/misc.crindex 3ea4c272..d6bd9571 100644--- a/src/invidious/routes/misc.cr+++ b/src/invidious/routes/misc.cr@@ -3,7 +3,7 @@module Invidious::Routes::Miscdef self.home(env)preferences = env.get("preferences").as(Preferences)- locale = LOCALES[preferences.locale]?+ locale = preferences.localeuser = env.get? "user"case preferences.default_home@@ -29,12 +29,12 @@ module Invidious::Routes::Miscenddef self.privacy(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localetemplated "privacy"enddef self.licenses(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localerendered "licenses"enddiff --git a/src/invidious/routes/playlists.cr b/src/invidious/routes/playlists.crindex 21126d7e..7b7bd03f 100644--- a/src/invidious/routes/playlists.cr+++ b/src/invidious/routes/playlists.cr@@ -2,7 +2,7 @@module Invidious::Routes::Playlistsdef self.new(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -18,7 +18,7 @@ module Invidious::Routes::Playlistsenddef self.create(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -56,7 +56,7 @@ module Invidious::Routes::Playlistsenddef self.subscribe(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"referer = get_referer(env)@@ -73,7 +73,7 @@ module Invidious::Routes::Playlistsenddef self.delete_page(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -96,7 +96,7 @@ module Invidious::Routes::Playlistsenddef self.delete(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -129,7 +129,7 @@ module Invidious::Routes::Playlistsenddef self.edit(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -169,7 +169,7 @@ module Invidious::Routes::Playlistsenddef self.update(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -213,7 +213,7 @@ module Invidious::Routes::Playlistsenddef self.add_playlist_items_page(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -260,7 +260,7 @@ module Invidious::Routes::Playlistsenddef self.playlist_ajax(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get? "user"sid = env.get? "sid"@@ -387,7 +387,7 @@ module Invidious::Routes::Playlistsenddef self.show(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeuser = env.get?("user").try &.as(User)referer = get_referer(env)@@ -435,7 +435,7 @@ module Invidious::Routes::Playlistsenddef self.mix(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localerdid = env.params.query["list"]?if !rdiddiff --git a/src/invidious/routes/preferences.cr b/src/invidious/routes/preferences.crindex 8793d4e9..edf9e1e7 100644--- a/src/invidious/routes/preferences.cr+++ b/src/invidious/routes/preferences.cr@@ -2,7 +2,7 @@module Invidious::Routes::PreferencesRoutedef self.show(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localereferer = get_referer(env)@@ -12,7 +12,7 @@ module Invidious::Routes::PreferencesRouteenddef self.update(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localereferer = get_referer(env)video_loop = env.params.body["video_loop"]?.try &.as(String)@@ -227,7 +227,7 @@ module Invidious::Routes::PreferencesRouteenddef self.toggle_theme(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localereferer = get_referer(env, unroll: false)redirect = env.params.query["redirect"]?diff --git a/src/invidious/routes/search.cr b/src/invidious/routes/search.crindex 3f1e219f..c256d156 100644--- a/src/invidious/routes/search.cr+++ b/src/invidious/routes/search.cr@@ -2,7 +2,7 @@module Invidious::Routes::Searchdef self.opensearch(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeenv.response.content_type = "application/opensearchdescription+xml"XML.build(indent: " ", encoding: "UTF-8") do |xml|@@ -18,7 +18,7 @@ module Invidious::Routes::Searchenddef self.results(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localequery = env.params.query["search_query"]?query ||= env.params.query["q"]?@@ -37,7 +37,7 @@ module Invidious::Routes::Searchenddef self.search(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeregion = env.params.query["region"]?query = env.params.query["search_query"]?diff --git a/src/invidious/routes/video_playback.cr b/src/invidious/routes/video_playback.crindex 5c64f669..cfc25782 100644--- a/src/invidious/routes/video_playback.cr+++ b/src/invidious/routes/video_playback.cr@@ -1,7 +1,7 @@module Invidious::Routes::VideoPlayback# /videoplaybackdef self.get_video_playback(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localequery_params = env.params.queryfvip = query_params["fvip"]? || "3"diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.crindex abcf427e..b24222ff 100644--- a/src/invidious/routes/watch.cr+++ b/src/invidious/routes/watch.cr@@ -2,7 +2,7 @@module Invidious::Routes::Watchdef self.handle(env)- locale = LOCALES[env.get("preferences").as(Preferences).locale]?+ locale = env.get("preferences").as(Preferences).localeregion = env.params.query["region"]?if env.params.query.to_s.includes?("%20") || env.params.query.to_s.includes?("+")diff --git a/src/invidious/videos.cr b/src/invidious/videos.crindex d3e5800c..d6ecd326 100644--- a/src/invidious/videos.cr+++ b/src/invidious/videos.cr@@ -275,7 +275,7 @@ struct Videoendend- def to_json(locale : Hash(String, JSON::Any) | Nil, json : JSON::Builder)+ def to_json(locale : String?, json : JSON::Builder)json.object dojson.field "type", "video"@@ -475,7 +475,7 @@ struct Videoend# TODO: remove the locale and follow the crystal convention- def to_json(locale : Hash(String, JSON::Any) | Nil, _json : Nil)+ def to_json(locale : String?, _json : Nil)JSON.build { |json| to_json(locale, json) }enddiff --git a/src/invidious/views/template.ecr b/src/invidious/views/template.ecrindex 3fb2fe18..5b6e6ab8 100644--- a/src/invidious/views/template.ecr+++ b/src/invidious/views/template.ecr@@ -19,8 +19,10 @@<link rel="stylesheet" href="/css/default.css?v=<%= ASSET_COMMIT %>"></head>-<% locale = LOCALES[env.get("preferences").as(Preferences).locale]? %>-<% dark_mode = env.get("preferences").as(Preferences).dark_mode %>+<%+ locale = env.get("preferences").as(Preferences).locale+ dark_mode = env.get("preferences").as(Preferences).dark_mode+%><body class="<%= dark_mode.blank? ? "no" : dark_mode %>-theme"><span style="display:none" id="dark_mode_pref"><%= env.get("preferences").as(Preferences).dark_mode %></span>