|
1 | 1 | import { createInstance } from "i18next"; |
2 | 2 |
|
| 3 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 4 | +const { i18n } = require("@calcom/config/next-i18next.config"); |
3 | 5 | import { WEBAPP_URL } from "@calcom/lib/constants"; |
4 | 6 |
|
| 7 | +import { fetchWithTimeout } from "../fetchWithTimeout"; |
| 8 | +import logger from "../logger"; |
| 9 | + |
5 | 10 | const translationCache = new Map<string, Record<string, string>>(); |
6 | 11 | const i18nInstanceCache = new Map<string, any>(); |
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 | | -} |
| 12 | +const SUPPORTED_NAMESPACES = ["common"]; |
37 | 13 |
|
38 | 14 | /** |
39 | 15 | * Loads translations for a specific locale and namespace with optimized caching |
40 | 16 | * @param {string} _locale - The locale code (e.g., 'en', 'fr', 'zh') |
41 | 17 | * @param {string} ns - The namespace for the translations |
42 | 18 | * @returns {Promise<Record<string, string>>} Translations object or fallback translations on failure |
43 | 19 | */ |
44 | | -export async function loadTranslations(_locale: string, ns: string) { |
45 | | - const locale = _locale === "zh" ? "zh-CN" : _locale; |
| 20 | +export async function loadTranslations(_locale: string, _ns: string) { |
| 21 | + let locale = _locale === "zh" ? "zh-CN" : _locale; |
| 22 | + locale = i18n.locales.includes(locale) ? locale : "en"; |
| 23 | + const ns = SUPPORTED_NAMESPACES.includes(_ns) ? _ns : "common"; |
46 | 24 | const cacheKey = `${locale}-${ns}`; |
47 | 25 |
|
48 | 26 | if (translationCache.has(cacheKey)) { |
49 | 27 | return translationCache.get(cacheKey); |
50 | 28 | } |
51 | 29 |
|
52 | | - try { |
53 | | - const url = `${WEBAPP_URL}/static/locales/${locale}/${ns}.json`; |
54 | | - const response = await fetch(url, { |
| 30 | + const url = `${WEBAPP_URL}/static/locales/${locale}/${ns}.json`; |
| 31 | + const response = await fetchWithTimeout( |
| 32 | + url, |
| 33 | + { |
55 | 34 | cache: process.env.NODE_ENV === "production" ? "force-cache" : "no-store", |
56 | | - }); |
57 | | - |
58 | | - if (!response.ok) { |
59 | | - throw new Error(`Failed to fetch translations: ${response.status}`); |
60 | | - } |
| 35 | + }, |
| 36 | + 3000 |
| 37 | + ); |
61 | 38 |
|
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; |
| 39 | + if (!response.ok) { |
| 40 | + logger.error(`Failed to fetch translations: ${response.status}`); |
| 41 | + return {}; |
69 | 42 | } |
| 43 | + |
| 44 | + const translations = await response.json(); |
| 45 | + translationCache.set(cacheKey, translations); |
| 46 | + return translations; |
70 | 47 | } |
71 | 48 |
|
72 | 49 | /** |
|
0 commit comments