FIX: Include resolved locale in anonymous cache key (#10289)

This only applies when set_locale_from_accept_language_header is enabled
This commit is contained in:
David Taylor
2020-07-22 18:00:07 +01:00
committed by GitHub
parent bcb0e62363
commit c09b5807f3
5 changed files with 52 additions and 13 deletions

View File

@@ -0,0 +1,13 @@
# 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)
matched = parser.language_region_compatible_from(available_locales)&.tr('-', '_')
matched || SiteSetting.default_locale
end
end

View File

@@ -3,6 +3,7 @@
require_dependency "mobile_detection"
require_dependency "crawler_detection"
require_dependency "guardian"
require_dependency "http_language_parser"
module Middleware
class AnonymousCache
@@ -13,7 +14,8 @@ module Middleware
c: 'key_is_crawler?',
b: 'key_has_brotli?',
t: 'key_cache_theme_ids',
ca: 'key_compress_anon'
ca: 'key_compress_anon',
l: 'key_locale'
}
end
@@ -89,6 +91,14 @@ module Middleware
@has_brotli == :true
end
def key_locale
if SiteSetting.set_locale_from_accept_language_header
HttpLanguageParser.parse(@env["HTTP_ACCEPT_LANGUAGE"])
else
"" # No need to key, it is the same for all anon users
end
end
def is_crawler?
@is_crawler ||=
begin