diff options
Diffstat (limited to 'app/controllers/concerns/preferred_language_switcher.rb')
-rw-r--r-- | app/controllers/concerns/preferred_language_switcher.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/app/controllers/concerns/preferred_language_switcher.rb b/app/controllers/concerns/preferred_language_switcher.rb index 872652100c9..5ac75e40b04 100644 --- a/app/controllers/concerns/preferred_language_switcher.rb +++ b/app/controllers/concerns/preferred_language_switcher.rb @@ -2,6 +2,8 @@ module PreferredLanguageSwitcher extend ActiveSupport::Concern + include Gitlab::Utils::StrongMemoize + include PreferredLanguageSwitcherHelper private @@ -11,8 +13,36 @@ module PreferredLanguageSwitcher def preferred_language cookies[:preferred_language].presence_in(Gitlab::I18n.available_locales) || + selectable_language(marketing_site_language) || + selectable_language(browser_languages) || Gitlab::CurrentSettings.default_preferred_language end + + def selectable_language(language_options) + language_options.find { |lan| ordered_selectable_locales_codes.include?(lan) } + end + + def browser_languages + http_language_header = request.env['HTTP_ACCEPT_LANGUAGE'] + return [] unless http_language_header + + http_language_header.tr!('-', '_').split(%r{[;,]}).reject { |str| str.start_with?('q') } + end + strong_memoize_attr :browser_languages + + def marketing_site_language + return [] unless params[:glm_source] + + locale = params[:glm_source].scan(%r{(\w{2})-(\w{2})/}).flatten + + return [] if locale.empty? + + [locale[0], "#{locale[0]}_#{locale[1]}"] + end + + def ordered_selectable_locales_codes + ordered_selectable_locales.map { |locale| locale[:value] } # rubocop:disable Rails/Pluck + end end PreferredLanguageSwitcher.prepend_mod |