Aggiunge i18n IT/EN/FR/DE/ES, pagine pubbliche squadre e UX archivio.
Il selettore lingua funziona sul web e sulle app native; su Android la preferenza è persistita e applicata al riavvio. Incluse anche eliminazione replay a scope e campi pagina pubblica squadra. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module LocaleResolver
|
||||
COOKIE_NAME = "mltv_locale"
|
||||
COOKIE_MAX_AGE = 2.years.to_i
|
||||
|
||||
module_function
|
||||
|
||||
def available
|
||||
I18n.available_locales.map(&:to_sym)
|
||||
end
|
||||
|
||||
def normalize(value)
|
||||
code = value.to_s.strip.downcase.split(/[-_]/).first
|
||||
return nil if code.blank?
|
||||
|
||||
sym = code.to_sym
|
||||
available.include?(sym) ? sym : nil
|
||||
end
|
||||
|
||||
def from_cookie(cookies)
|
||||
return nil if cookies.nil?
|
||||
|
||||
value = if cookies.respond_to?(:[])
|
||||
cookies[COOKIE_NAME]
|
||||
elsif cookies.respond_to?(:fetch)
|
||||
cookies.fetch(COOKIE_NAME, nil)
|
||||
end
|
||||
normalize(value)
|
||||
end
|
||||
|
||||
def from_accept_language(header)
|
||||
return nil if header.blank?
|
||||
|
||||
header.to_s.split(",").each do |part|
|
||||
tag = part.split(";").first.to_s.strip
|
||||
locale = normalize(tag)
|
||||
return locale if locale
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def resolve(cookie_jar: nil, cookies: nil, accept_language: nil)
|
||||
jar = cookie_jar || cookies
|
||||
from_cookie(jar) || from_accept_language(accept_language) || I18n.default_locale
|
||||
end
|
||||
|
||||
def persist!(cookie_jar, locale)
|
||||
normalized = normalize(locale) || I18n.default_locale
|
||||
cookie_jar[COOKIE_NAME] = {
|
||||
value: normalized.to_s,
|
||||
expires: COOKIE_MAX_AGE.seconds.from_now,
|
||||
path: "/",
|
||||
same_site: :lax,
|
||||
httponly: false
|
||||
}
|
||||
normalized
|
||||
end
|
||||
|
||||
def language_options
|
||||
[
|
||||
{ code: :it, label: "Italiano", native: "Italiano", flag: "🇮🇹" },
|
||||
{ code: :en, label: "English", native: "English", flag: "🇬🇧" },
|
||||
{ code: :fr, label: "Français", native: "Français", flag: "🇫🇷" },
|
||||
{ code: :de, label: "Deutsch", native: "Deutsch", flag: "🇩🇪" },
|
||||
{ code: :es, label: "Español", native: "Español", flag: "🇪🇸" }
|
||||
].select { |opt| available.include?(opt[:code]) }
|
||||
end
|
||||
|
||||
def current_language_option
|
||||
language_options.find { |opt| opt[:code] == I18n.locale.to_sym } || language_options.first
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user