Skip to content

Commit 02c0b72

Browse files
committed
Revert "fix: refactor i18n loadTranslations and set timeout to 3s (calcom#22633)"
This reverts commit 6ff20b9.
1 parent 2934984 commit 02c0b72

9 files changed

Lines changed: 53 additions & 96 deletions

File tree

apps/web/test/lib/handleChildrenEventTypes.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ vi.mock("@calcom/emails/email-manager", () => {
2626

2727
vi.mock("@calcom/lib/server/i18n", () => {
2828
return {
29-
getTranslation: async (locale: string, namespace: string) => {
30-
const t = (key: string) => key;
31-
t.locale = locale;
32-
t.namespace = namespace;
33-
return t;
34-
},
29+
getTranslation: (key: string) => key,
3530
};
3631
});
3732

packages/features/ee/billing/credit-service.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ vi.mock("@calcom/prisma", () => {
2525

2626
vi.mock("stripe");
2727

28-
vi.mock("@calcom/lib/server/i18n", () => {
29-
return {
30-
getTranslation: async (locale: string, namespace: string) => {
31-
const t = (key: string) => key;
32-
t.locale = locale;
33-
t.namespace = namespace;
34-
return t;
35-
},
36-
};
37-
});
38-
3928
vi.mock("@calcom/lib/constants", async () => {
4029
const actual = (await vi.importActual("@calcom/lib/constants")) as typeof import("@calcom/lib/constants");
4130
return {

packages/features/ee/organizations/lib/server/createOrganizationFromOnboarding.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ vi.mock("@calcom/lib/domainManager/organization", () => ({
4343
createDomain: vi.fn(),
4444
}));
4545

46-
vi.mock("@calcom/lib/server/i18n", () => {
47-
return {
48-
getTranslation: async (locale: string, namespace: string) => {
49-
const t = (key: string) => key;
50-
t.locale = locale;
51-
t.namespace = namespace;
52-
return t;
53-
},
54-
};
55-
});
56-
5746
const mockOrganizationOnboarding = {
5847
name: "Test Org",
5948
slug: "test-org",

packages/features/ee/workflows/lib/reminders/reminderScheduler.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ vi.mock("@calcom/features/ee/workflows/lib/reminders/providers/emailProvider", (
1717
sendOrScheduleWorkflowEmails: vi.fn(),
1818
}));
1919

20-
vi.mock("@calcom/lib/server/i18n", () => {
21-
return {
22-
getTranslation: async (locale: string, namespace: string) => {
23-
const t = (key: string) => key;
24-
t.locale = locale;
25-
t.namespace = namespace;
26-
return t;
27-
},
28-
};
29-
});
3020
describe("reminderScheduler", () => {
3121
beforeEach(() => {
3222
vi.clearAllMocks();

packages/features/ee/workflows/lib/test/compareReminderBodyToTemplate.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createInstance } from "i18next";
2-
import { vi, expect, test, describe } from "vitest";
2+
import { expect, test, describe } from "vitest";
33

44
import { getTranslation } from "@calcom/lib/server/i18n";
55
import { TimeFormat } from "@calcom/lib/timeFormat";
@@ -10,17 +10,6 @@ import { getTemplateBodyForAction } from "../actionHelperFunctions";
1010
import compareReminderBodyToTemplate from "../compareReminderBodyToTemplate";
1111
import plainTextReminderTemplates from "../reminders/templates/plainTextTemplates";
1212

13-
vi.mock("@calcom/lib/server/i18n", () => {
14-
return {
15-
getTranslation: async (locale: string, namespace: string) => {
16-
const t = (key: string) => key;
17-
t.locale = locale;
18-
t.namespace = namespace;
19-
return t;
20-
},
21-
};
22-
});
23-
2413
const translation = async () => {
2514
const _i18n = createInstance();
2615
await _i18n.init({

packages/lib/fetchWithTimeout.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/lib/server/i18n.ts

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,72 @@
11
import { createInstance } from "i18next";
22

3-
import { i18n } from "@calcom/config/next-i18next.config";
43
import { WEBAPP_URL } from "@calcom/lib/constants";
54

6-
import { fetchWithTimeout } from "../fetchWithTimeout";
7-
import logger from "../logger";
8-
95
const translationCache = new Map<string, Record<string, string>>();
106
const i18nInstanceCache = new Map<string, any>();
11-
const SUPPORTED_NAMESPACES = ["common"];
7+
8+
/**
9+
* Loads English fallback translations for when requested locale translations fail
10+
* Implements caching to avoid redundant network requests
11+
* @returns {Promise<Record<string, string>>} English translations object or empty object on failure
12+
*/
13+
async function loadFallbackTranslations() {
14+
const cacheKey = "en-common";
15+
16+
if (translationCache.has(cacheKey)) {
17+
return translationCache.get(cacheKey);
18+
}
19+
20+
try {
21+
const res = await fetch(`${WEBAPP_URL}/static/locales/en/common.json`, {
22+
cache: process.env.NODE_ENV === "production" ? "force-cache" : "no-store",
23+
});
24+
25+
if (!res.ok) {
26+
throw new Error(`Failed to fetch fallback translations: ${res.status}`);
27+
}
28+
29+
const translations = await res.json();
30+
translationCache.set(cacheKey, translations);
31+
return translations;
32+
} catch (error) {
33+
console.error("Could not fetch fallback translations:", error);
34+
return {};
35+
}
36+
}
1237

1338
/**
1439
* Loads translations for a specific locale and namespace with optimized caching
1540
* @param {string} _locale - The locale code (e.g., 'en', 'fr', 'zh')
1641
* @param {string} ns - The namespace for the translations
1742
* @returns {Promise<Record<string, string>>} Translations object or fallback translations on failure
1843
*/
19-
export async function loadTranslations(_locale: string, _ns: string) {
20-
let locale = _locale === "zh" ? "zh-CN" : _locale;
21-
locale = i18n.locales.includes(locale) ? locale : "en";
22-
const ns = SUPPORTED_NAMESPACES.includes(_ns) ? _ns : "common";
44+
export async function loadTranslations(_locale: string, ns: string) {
45+
const locale = _locale === "zh" ? "zh-CN" : _locale;
2346
const cacheKey = `${locale}-${ns}`;
2447

2548
if (translationCache.has(cacheKey)) {
2649
return translationCache.get(cacheKey);
2750
}
2851

29-
const url = `${WEBAPP_URL}/static/locales/${locale}/${ns}.json`;
30-
const response = await fetchWithTimeout(
31-
url,
32-
{
52+
try {
53+
const url = `${WEBAPP_URL}/static/locales/${locale}/${ns}.json`;
54+
const response = await fetch(url, {
3355
cache: process.env.NODE_ENV === "production" ? "force-cache" : "no-store",
34-
},
35-
3000
36-
);
56+
});
3757

38-
if (!response.ok) {
39-
logger.error(`Failed to fetch translations: ${response.status}`);
40-
return {};
41-
}
58+
if (!response.ok) {
59+
throw new Error(`Failed to fetch translations: ${response.status}`);
60+
}
4261

43-
const translations = await response.json();
44-
translationCache.set(cacheKey, translations);
45-
return translations;
62+
const translations = await response.json();
63+
translationCache.set(cacheKey, translations);
64+
return translations;
65+
} catch (error) {
66+
console.warn(`Failed to load translations for ${locale}/${ns}, falling back to English:`, error);
67+
const fallbackTranslations = await loadFallbackTranslations();
68+
return fallbackTranslations;
69+
}
4670
}
4771

4872
/**

packages/lib/server/repository/user.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ import { UserRepository } from "./user";
99

1010
vi.mock("@calcom/lib/server/i18n", () => {
1111
return {
12-
getTranslation: async (locale: string, namespace: string) => {
13-
const t = (key: string) => key;
14-
t.locale = locale;
15-
t.namespace = namespace;
16-
return t;
12+
getTranslation: (key: string) => {
13+
return () => key;
1714
},
1815
};
1916
});

packages/lib/server/service/userCreationService.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ import { UserCreationService } from "./userCreationService";
1111

1212
vi.mock("@calcom/lib/server/i18n", () => {
1313
return {
14-
getTranslation: async (locale: string, namespace: string) => {
15-
const t = (key: string) => key;
16-
t.locale = locale;
17-
t.namespace = namespace;
18-
return t;
14+
getTranslation: (key: string) => {
15+
return () => key;
1916
},
2017
};
2118
});

0 commit comments

Comments
 (0)