Skip to content

Commit

Permalink
fix: locales are requested multiple times (#29973)
Browse files Browse the repository at this point in the history
Co-authored-by: Tasso Evangelista <2263066+tassoevan@users.noreply.github.com>
  • Loading branch information
yash-rajpal and tassoevan committed Aug 1, 2023
1 parent bcabc0e commit 6d453f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-ladybugs-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Translation files are requested multiple times
18 changes: 18 additions & 0 deletions apps/meteor/client/providers/TranslationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const parseToJSON = (customTranslations: string): Record<string, Record<string,
}
};

const localeCache = new Map<string, Promise<string>>();

const useI18next = (lng: string): typeof i18next => {
const basePath = useAbsoluteUrl()('/i18n');

Expand Down Expand Up @@ -101,6 +103,22 @@ const useI18next = (lng: string): typeof i18next => {
loadPath: `${basePath}/{{lng}}.json`,
parse: (data: string, lngs?: string | string[], namespaces: string | string[] = []) =>
extractKeys(JSON.parse(data), lngs, namespaces),
request: (_options, url, _payload, callback) => {
const params = url.split('/');
const lng = params[params.length - 1];

let promise = localeCache.get(lng);

if (!promise) {
promise = fetch(url).then((res) => res.text());
localeCache.set(lng, promise);
}

promise.then(
(res) => callback(null, { data: res, status: 200 }),
() => callback(null, { data: '', status: 500 }),
);
},
},
react: {
useSuspense: true,
Expand Down

0 comments on commit 6d453f7

Please sign in to comment.