FEATURE: Add a onebox_locale site setting. (#30655)

Following on from f369db5ae9a29a23299dff5f14768167230b0b79, this change adds the ability to choose a custom locale to send to onebox providers.

If this setting is left blank, it will fall back to using default_locale.
This commit is contained in:
Gary Pendergast
2025-01-09 14:11:37 +11:00
committed by GitHub
parent 590b3e11fb
commit f53c734ba6
7 changed files with 66 additions and 17 deletions

View File

@ -152,7 +152,7 @@ module Oneboxer
end
def self.redis_cached_response_body_key(uri)
"CACHED_RESPONSE_#{SiteSetting.default_locale}_#{uri}"
"CACHED_RESPONSE_#{onebox_locale}_#{uri}"
end
# Parse URLs out of HTML, returning the document when finished.
@ -677,6 +677,9 @@ module Oneboxer
force_custom_user_agent_hosts: force_custom_user_agent_hosts,
preserve_fragment_url_hosts: preserve_fragment_url_hosts,
timeout: 5,
headers: {
"Accept-Language" => accept_language,
},
}
uri = URI(url)
@ -705,4 +708,16 @@ module Oneboxer
fd_options
end
def self.onebox_locale
SiteSetting.onebox_locale.presence || SiteSetting.default_locale
end
def self.accept_language
if onebox_locale == "en"
"en;q=0.9, *;q=0.5"
else
"#{onebox_locale.gsub(/_/, "-")};q=0.9, en;q=0.8, *;q=0.5"
end
end
end