Use `athena-negotiation` to detect language through Accept-Language header (#2324)

Detect language through Accept-Language header
pull/2356/head
syeopite 3 years ago committed by GitHub
parent 0db23f9252
commit fceb8093f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,9 @@
version: 2.0 version: 2.0
shards: shards:
athena-negotiation:
git: https://github.com/athena-framework/negotiation.git
version: 0.1.1
db: db:
git: https://github.com/crystal-lang/crystal-db.git git: https://github.com/crystal-lang/crystal-db.git
version: 0.10.1 version: 0.10.1

@ -25,6 +25,9 @@ dependencies:
lsquic: lsquic:
github: iv-org/lsquic.cr github: iv-org/lsquic.cr
version: ~> 2.18.1-2 version: ~> 2.18.1-2
athena-negotiation:
github: athena-framework/negotiation
version: ~> 0.1.1
crystal: ">= 1.0.0, < 2.0.0" crystal: ">= 1.0.0, < 2.0.0"

@ -17,6 +17,7 @@
require "digest/md5" require "digest/md5"
require "file_utils" require "file_utils"
require "kemal" require "kemal"
require "athena-negotiation"
require "openssl/hmac" require "openssl/hmac"
require "option_parser" require "option_parser"
require "pg" require "pg"
@ -166,10 +167,20 @@ def popular_videos
end end
before_all do |env| before_all do |env|
preferences = begin preferences = Preferences.from_json("{}")
Preferences.from_json(URI.decode_www_form(env.request.cookies["PREFS"]?.try &.value || "{}"))
begin
if prefs_cookie = env.request.cookies["PREFS"]?
preferences = Preferences.from_json(URI.decode_www_form(prefs_cookie.value))
else
if language_header = env.request.headers["Accept-Language"]?
if language = ANG.language_negotiator.best(language_header, LOCALES.keys)
preferences.locale = language.header
end
end
end
rescue rescue
Preferences.from_json("{}") preferences = Preferences.from_json("{}")
end end
env.set "preferences", preferences env.set "preferences", preferences

@ -434,6 +434,13 @@ module Invidious::Routes::Login
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32)) sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
user, sid = create_user(sid, email, password) user, sid = create_user(sid, email, password)
if language_header = env.request.headers["Accept-Language"]?
if language = ANG.language_negotiator.best(language_header, LOCALES.keys)
user.preferences.locale = language.header
end
end
user_array = user.to_a user_array = user.to_a
user_array[4] = user_array[4].to_json # User preferences user_array[4] = user_array[4].to_json # User preferences

Loading…
Cancel
Save