2020-07-22 12:00:07 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module HttpLanguageParser
|
|
|
|
def self.parse(header)
|
|
|
|
# Rails I18n uses underscores between the locale and the region; the request
|
|
|
|
# headers use hyphens.
|
2023-01-09 06:10:19 -06:00
|
|
|
require "http_accept_language" unless defined?(HttpAcceptLanguage)
|
|
|
|
available_locales = I18n.available_locales.map { |locale| locale.to_s.tr("_", "-") }
|
2020-07-22 12:00:07 -05:00
|
|
|
parser = HttpAcceptLanguage::Parser.new(header)
|
2023-01-09 06:10:19 -06:00
|
|
|
matched = parser.language_region_compatible_from(available_locales)&.tr("-", "_")
|
2020-07-22 12:00:07 -05:00
|
|
|
matched || SiteSetting.default_locale
|
|
|
|
end
|
|
|
|
end
|