Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
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 apps/ledger-live-mobile/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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid adding more ts-ignore in our codebase? 🙏 not sure what the error was but we may have to find a better algorithm.

Also, I'm not a huge fan of the usage of get() here. It will not help performance to have a lot of these kind of code.
I totally understand that i was meant not to be breaking change tho. But, we are making urls dynamic, so in a way it's a new feature, it is ok to make it explicit 🤔 I guess that means changing all parts where we get that URL, but I feel it's for the better good.

There are probably different ways to do this, one way could be a function getURL("key", lang). We can also cache per lang / refresh a map per lang. anything that could try to avoid doing this at runtime 🙏

return obj["_" + key].replace(regex, urlsConfig.locale);
},
});
}
});
};

iterateUrls(urls);
2 changes: 2 additions & 0 deletions apps/ledger-live-mobile/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";

try {
if ("__setDefaultTimeZone" in Intl.DateTimeFormat) {
Expand Down Expand Up @@ -70,6 +71,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