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

Detect language through Accept-Language header
This commit is contained in:
syeopite 2021-08-24 19:59:27 +00:00 committed by GitHub
parent 0db23f9252
commit fceb8093f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 3 deletions

View File

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

View File

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

View File

@ -17,6 +17,7 @@
require "digest/md5"
require "file_utils"
require "kemal"
require "athena-negotiation"
require "openssl/hmac"
require "option_parser"
require "pg"
@ -166,10 +167,20 @@ def popular_videos
end
before_all do |env|
preferences = begin
Preferences.from_json(URI.decode_www_form(env.request.cookies["PREFS"]?.try &.value || "{}"))
preferences = Preferences.from_json("{}")
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
Preferences.from_json("{}")
preferences = Preferences.from_json("{}")
end
env.set "preferences", preferences

View File

@ -434,6 +434,13 @@ module Invidious::Routes::Login
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
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[4] = user_array[4].to_json # User preferences