1
0
mirror of https://github.com/mastodon/mastodon synced 2024-11-27 22:38:27 +09:00

Add global locale param (#17464)

- Remove the session-based locale stickyness
This commit is contained in:
Eugen Rochko 2022-02-08 02:34:56 +01:00 committed by GitHub
parent 35850f8195
commit 85b86fe28c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 20 deletions

View File

@ -7,27 +7,24 @@ module Localized
around_action :set_locale around_action :set_locale
end end
def set_locale def set_locale(&block)
locale = current_user.locale if respond_to?(:user_signed_in?) && user_signed_in? I18n.with_locale(requested_locale || I18n.default_locale, &block)
locale ||= session[:locale] ||= default_locale
locale = default_locale unless I18n.available_locales.include?(locale.to_sym)
I18n.with_locale(locale) do
yield
end
end end
private private
def default_locale def requested_locale
if ENV['DEFAULT_LOCALE'].present? requested_locale_name = available_locale_or_nil(params[:locale])
I18n.default_locale requested_locale_name ||= available_locale_or_nil(current_user.locale) if respond_to?(:user_signed_in?) && user_signed_in?
else requested_locale_name ||= http_accept_language if ENV['DEFAULT_LOCALE'].blank?
request_locale || I18n.default_locale requested_locale_name
end
end end
def request_locale def http_accept_language
http_accept_language.language_region_compatible_from(I18n.available_locales) HttpAcceptLanguage::Parser.new(request.headers.fetch('Accept-Language')).language_region_compatible_from(I18n.available_locales) if request.headers.key?('Accept-Language')
end
def available_locale_or_nil(locale_name)
locale_name.to_sym if locale_name.present? && I18n.available_locales.include?(locale_name.to_sym)
end end
end end

View File

@ -149,10 +149,14 @@ module Mastodon
:'zh-TW', :'zh-TW',
] ]
config.i18n.default_locale = ENV['DEFAULT_LOCALE']&.to_sym config.i18n.default_locale = begin
custom_default_locale = ENV['DEFAULT_LOCALE']&.to_sym
unless config.i18n.available_locales.include?(config.i18n.default_locale) if config.i18n.available_locales.include?(custom_default_locale)
config.i18n.default_locale = :en custom_default_locale
else
:en
end
end end
# config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb') # config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
@ -169,7 +173,6 @@ module Mastodon
Doorkeeper::Application.send :include, ApplicationExtension Doorkeeper::Application.send :include, ApplicationExtension
Doorkeeper::AccessToken.send :include, AccessTokenExtension Doorkeeper::AccessToken.send :include, AccessTokenExtension
Devise::FailureApp.send :include, AbstractController::Callbacks Devise::FailureApp.send :include, AbstractController::Callbacks
Devise::FailureApp.send :include, HttpAcceptLanguage::EasyAccess
Devise::FailureApp.send :include, Localized Devise::FailureApp.send :include, Localized
end end
end end