It wouldn’t be as bad if a global setting was added and the accept-language header was parsed before it was used in the cache_key. Something like this?
# lib/anonymous_locale.rb
class AnonymousLocale
def self.locale(accept_language_header)
if GlobalSetting.locale_from_header
begin
require 'http_accept_language' unless defined? HttpAcceptLanguage
available_locales = I18n.available_locales.map { |locale| locale.to_s.gsub(/_/, '-') }
parser = HttpAcceptLanguage::Parser.new(accept_language_header)
parser.language_region_compatible_from(available_locales).gsub(/-/, '_')
rescue
I18n.default_locale
end
else
I18n.default_locale
end
end
end
And then in anonymous_cache:
def cache_key
@cache_key ||= "ANON_CACHE_#{@env["HTTP_ACCEPT"]}_#{AnonymousLocale.locale(@env["HTTP_ACCEPT_LANGUAGE"])}_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}|c=#{is_crawler?}"
end