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

@ -41,10 +41,11 @@ module Onebox
canonical_uri = Addressable::URI.parse(canonical_link)
if canonical_link && canonical_uri &&
"#{canonical_uri.host}#{canonical_uri.path}" != "#{uri.host}#{uri.path}"
canonical_options = Oneboxer.get_final_destination_options(canonical_link)
canonical_options["extra_headers"] = { "Accept-Language" => accept_language }
uri = FinalDestination.new(canonical_link, canonical_options).resolve
uri =
FinalDestination.new(
canonical_link,
Oneboxer.get_final_destination_options(canonical_link),
).resolve
if uri.present?
response =
(
@ -104,7 +105,7 @@ module Onebox
headers ||= {}
headers["User-Agent"] ||= user_agent if user_agent
headers["Accept-Language"] ||= accept_language if accept_language
headers["Accept-Language"] ||= Oneboxer.accept_language
request = Net::HTTP::Get.new(uri.request_uri, headers)
start_time = Time.now
@ -236,14 +237,6 @@ module Onebox
user_agent
end
def self.accept_language
if SiteSetting.default_locale == "en"
"en;q=0.9, *;q=0.5"
else
"#{SiteSetting.default_locale.gsub(/_/, "-")};q=0.9, en;q=0.8, *;q=0.5"
end
end
# Percent-encodes a URI string per RFC3986 - https://tools.ietf.org/html/rfc3986
def self.uri_encode(url)
return "" unless url