2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-08-18 18:18:09 +08:00
discourse/lib/http_language_parser.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

14 lines
554 B
Ruby
Raw Normal View History

# 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.
require "http_accept_language" unless defined?(HttpAcceptLanguage)
available_locales = I18n.available_locales.map { |locale| locale.to_s.tr("_", "-") }
parser = HttpAcceptLanguage::Parser.new(header&.tr("_", "-"))
matched = parser.language_region_compatible_from(available_locales)&.tr("-", "_")
matched || SiteSetting.default_locale
end
end