blob: 5ac75e40b0402288345bb66a39ed2b213c667769 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# frozen_string_literal: true
module PreferredLanguageSwitcher
extend ActiveSupport::Concern
include Gitlab::Utils::StrongMemoize
include PreferredLanguageSwitcherHelper
private
def init_preferred_language
cookies[:preferred_language] = preferred_language
end
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
|