Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

LIVE-2003 - Feature - Pass urls through regex to replace template by locale #2451

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/config/urls.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Place {locale} inside url to replace it by current locale. Ex: ledger.com/{locale}/ become ledger.com/fr/
export const urls = {
faq:
"https://support.ledgerwallet.com/hc/en-us?utm_source=ledger_live_mobile&utm_medium=self_referral&utm_content=faq",
Expand Down Expand Up @@ -164,3 +165,41 @@ export const urls = {
"https://www.ledger.com/supported-services?utm_source=ledger_live&utm_medium=self_referral&utm_content=discover",
},
};

const regex = /{locale}/g;

class UrlsConfig {
public locale = "en";

changeLocale(locale: string) {
this.locale = locale;
}
}

export const urlsConfig = new UrlsConfig();

// Iterate through url strings and substitute them by getter function with regex replacing template {locale} by current locale
const iterateUrls = (obj: typeof urls) => {
Object.keys(obj).forEach(key => {
// @ts-ignore
if (typeof obj[key] === "object" && obj[key] !== null) {
// @ts-ignore
iterateUrls(obj[key]);
}

// @ts-ignore
if (typeof obj[key] === "string") {
// @ts-ignore
// eslint-disable-next-line no-param-reassign
obj["_" + key] = obj[key];
Object.defineProperty(obj, key, {
get() {
// @ts-ignore
return obj["_" + key].replace(regex, urlsConfig.locale);
},
});
}
});
};

iterateUrls(urls);
2 changes: 2 additions & 0 deletions src/context/Locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
locales,
} from "../languages";
import { languageSelector } from "../reducers/settings";
import { urlsConfig } from "../config/urls";

if ("__setDefaultTimeZone" in Intl.DateTimeFormat) {
/** https://formatjs.io/docs/polyfills/intl-datetimeformat/#default-timezone */
Expand Down Expand Up @@ -65,6 +66,7 @@ export default function LocaleProvider({ children }: Props) {
const language = useSelector(languageSelector);
useEffect(() => {
i18next.changeLanguage(language);
urlsConfig.changeLocale(language);
}, [language]);

const value: LocaleState = useMemo(
Expand Down