Skip to content

Commit

Permalink
Language links (#2355)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshull authored Feb 3, 2025
1 parent 34dbf18 commit 2c44639
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
32 changes: 32 additions & 0 deletions assets/js/translate-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default () => {
if (typeof MutationObserver === "function") {
// Tell the observer to monitor for changes to HTML attributes
const config = { attributes: true };

// Get the language of the page on load.
const oldCookieLanguage = getCookieLanguage();

// Build the function to run when a change is observed
const callback = mutationList => {
// Loop through each observed change
Expand All @@ -36,6 +40,15 @@ export default () => {
event: "translate",
language: newLanguage && newLanguage !== "en" ? newLanguage : ""
});

// Get the new language from the cookie on language change.
const newCookieLanguage = getCookieLanguage();

// If the page loaded with one language and switched to another, we have to reload the page.
// This is so that page content can be rendered differently with the added cookie information.
if (oldCookieLanguage !== newCookieLanguage) {
window.location.reload();
}
}
}
}
Expand All @@ -48,3 +61,22 @@ export default () => {
observer.observe(document.getElementsByTagName("html")[0], config);
}
};

// Get the current language of the page from the `googtrans` cookie.
function getCookieLanguage() {
const cookie =
document.cookie
.split(";")
.map(cookie => cookie.trim())
.map(cookie => cookie.split("="))
.find(cookie => cookie[0] === "googtrans");

if (cookie) {
const [_key, value] = cookie;
const [_space, _from, to] = value.split("/");

return to;
} else {
return "en";
}
}
1 change: 1 addition & 0 deletions lib/dotcom/content_rewriter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ defmodule Dotcom.ContentRewriter do
element
|> Links.add_target_to_redirect()
|> Links.add_preview_params(conn)
|> Links.add_locale_params(conn)
|> rewrite_children(conn, context)
end

Expand Down
20 changes: 20 additions & 0 deletions lib/dotcom/content_rewriters/links.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,24 @@ defmodule Dotcom.ContentRewriters.Links do
element
end
end

@doc """
If the user has used Google Translate on dotcom, we want to forward them to MyCharlie with the same language.
"""
def add_locale_params({"a", attrs, children} = element, %{cookies: %{"googtrans" => languages}}) do
attr_map = Enum.into(attrs, %{})
href = Map.get(attr_map, "href", "")
locale = String.split(languages, "/") |> List.last()

if String.match?(href, ~r/mycharlie.mbta.com/) do
updated_href = "#{href}?locale=#{locale}"
updated_attrs = attr_map |> Map.replace!("href", updated_href) |> Map.to_list()

{"a", updated_attrs, children}
else
element
end
end

def add_locale_params(element, _conn), do: element
end

0 comments on commit 2c44639

Please sign in to comment.