refactor: extract dedicated @calcom/i18n package (#28141)
This commit is contained in:
1 parent
f00be08e8c
commit
648ad72a54
208 files changed
+548
-459
No files matched your search
@@ -5,13 +5,13 @@ import { useCallback } from "react";
|
||||
|
||||
import type { API_VERSIONS_ENUM } from "@calcom/platform-constants";
|
||||
import { IconSprites } from "@calcom/ui/components/icon";
|
||||
import deTranslations from "@calcom/web/public/static/locales/de/common.json";
|
||||
import enTranslations from "@calcom/web/public/static/locales/en/common.json";
|
||||
import esTranslations from "@calcom/web/public/static/locales/es/common.json";
|
||||
import frTranslations from "@calcom/web/public/static/locales/fr/common.json";
|
||||
import itTranslations from "@calcom/web/public/static/locales/it/common.json";
|
||||
import nlTranslations from "@calcom/web/public/static/locales/nl/common.json";
|
||||
import ptBrTranslations from "@calcom/web/public/static/locales/pt-BR/common.json";
|
||||
import deTranslations from "@calcom/i18n/locales/de/common.json";
|
||||
import enTranslations from "@calcom/i18n/locales/en/common.json";
|
||||
import esTranslations from "@calcom/i18n/locales/es/common.json";
|
||||
import frTranslations from "@calcom/i18n/locales/fr/common.json";
|
||||
import itTranslations from "@calcom/i18n/locales/it/common.json";
|
||||
import nlTranslations from "@calcom/i18n/locales/nl/common.json";
|
||||
import ptBrTranslations from "@calcom/i18n/locales/pt-BR/common.json";
|
||||
|
||||
import { AtomsContext } from "../hooks/useAtomsContext";
|
||||
import { useMe } from "../hooks/useMe";
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import type deTranslations from "@calcom/web/public/static/locales/de/common.json";
|
||||
import type enTranslations from "@calcom/web/public/static/locales/en/common.json";
|
||||
import type esTranslations from "@calcom/web/public/static/locales/es/common.json";
|
||||
import type frTranslations from "@calcom/web/public/static/locales/fr/common.json";
|
||||
import type itTranslations from "@calcom/web/public/static/locales/it/common.json";
|
||||
import type nlTranslations from "@calcom/web/public/static/locales/nl/common.json";
|
||||
import type ptBrTranslations from "@calcom/web/public/static/locales/pt-BR/common.json";
|
||||
import type deTranslations from "@calcom/i18n/locales/de/common.json";
|
||||
import type enTranslations from "@calcom/i18n/locales/en/common.json";
|
||||
import type esTranslations from "@calcom/i18n/locales/es/common.json";
|
||||
import type frTranslations from "@calcom/i18n/locales/fr/common.json";
|
||||
import type itTranslations from "@calcom/i18n/locales/it/common.json";
|
||||
import type nlTranslations from "@calcom/i18n/locales/nl/common.json";
|
||||
import type ptBrTranslations from "@calcom/i18n/locales/pt-BR/common.json";
|
||||
|
||||
export type enTranslationKeys = keyof typeof enTranslations;
|
||||
export type frTranslationKeys = keyof typeof frTranslations;
|
||||
|
||||
@@ -78,6 +78,7 @@
|
||||
},
|
||||
"types": "./dist/index.d.ts",
|
||||
"dependencies": {
|
||||
"@calcom/i18n": "workspace:*",
|
||||
"@radix-ui/react-dialog-atoms": "npm:@radix-ui/[email protected]",
|
||||
"@radix-ui/react-popover-atoms": "npm:@radix-ui/[email protected]",
|
||||
"@radix-ui/react-slot": "1.1.2",
|
||||
|
||||
@@ -96,10 +96,6 @@ export default defineConfig(({ mode }) => {
|
||||
"@calcom/platform-constants": path.resolve(__dirname, "../constants/index.ts"),
|
||||
"@calcom/platform-types": path.resolve(__dirname, "../types/index.ts"),
|
||||
"@calcom/platform-utils": path.resolve(__dirname, "../constants/index.ts"),
|
||||
"@calcom/web/public/static/locales/en/common.json": path.resolve(
|
||||
__dirname,
|
||||
"../../../apps/web/public/static/locales/en/common.json"
|
||||
),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
import { createInstance } from "i18next";
|
||||
import type { i18n as I18nInstance } from "i18next";
|
||||
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { fetchWithTimeout } from "@calcom/lib/fetchWithTimeout";
|
||||
import logger from "@calcom/lib/logger";
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
const { i18n } = require("@calcom/config/next-i18next.config");
|
||||
const path = require("node:path");
|
||||
const translationsPath = path.resolve(__dirname, "../../../../apps/web/public/static/locales/en/common.json");
|
||||
const englishTranslations: Record<string, string> = require(translationsPath);
|
||||
/* eslint-enable @typescript-eslint/no-require-imports */
|
||||
|
||||
const translationCache = new Map<string, Record<string, string>>([["en-common", englishTranslations]]);
|
||||
const i18nInstanceCache = new Map<string, I18nInstance>();
|
||||
const SUPPORTED_NAMESPACES = ["common"];
|
||||
|
||||
/**
|
||||
* Loads translations for a specific locale and namespace with optimized caching
|
||||
* @param {string} _locale - The locale code (e.g., 'en', 'fr', 'zh')
|
||||
* @param {string} ns - The namespace for the translations
|
||||
* @returns {Promise<Record<string, string>>} Translations object or fallback translations on failure
|
||||
*/
|
||||
export async function loadTranslations(_locale: string, _ns: string) {
|
||||
let locale = _locale === "zh" ? "zh-CN" : _locale;
|
||||
locale = i18n.locales.includes(locale) ? locale : "en";
|
||||
const ns = SUPPORTED_NAMESPACES.includes(_ns) ? _ns : "common";
|
||||
const cacheKey = `${locale}-${ns}`;
|
||||
|
||||
if (translationCache.has(cacheKey)) {
|
||||
return translationCache.get(cacheKey);
|
||||
}
|
||||
|
||||
const url = `${WEBAPP_URL}/static/locales/${locale}/${ns}.json`;
|
||||
try {
|
||||
const response = await fetchWithTimeout(
|
||||
url,
|
||||
{
|
||||
cache: "no-store",
|
||||
},
|
||||
process.env.NODE_ENV === "development" ? 30000 : 3000
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
logger.warn(`Failed to fetch translations for ${locale}: ${response.status}, falling back to English`);
|
||||
return englishTranslations;
|
||||
}
|
||||
|
||||
const translations = await response.json();
|
||||
translationCache.set(cacheKey, translations);
|
||||
return translations;
|
||||
} catch (err) {
|
||||
logger.warn(`Failed to load translations for ${locale}, falling back to English:`, err);
|
||||
return englishTranslations;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates or retrieves a cached i18next translation function for the specified locale and namespace
|
||||
* @param {string} locale - The locale code (e.g., 'en', 'fr')
|
||||
* @param {string} ns - The namespace for the translations
|
||||
* @returns {Promise<Function>} A translation function bound to the specified locale and namespace
|
||||
*/
|
||||
export const getTranslation = async (locale: string, ns: string) => {
|
||||
const cacheKey = `${locale}-${ns}`;
|
||||
const cachedInstance = i18nInstanceCache.get(cacheKey);
|
||||
if (cachedInstance) {
|
||||
return cachedInstance.getFixedT(locale, ns);
|
||||
}
|
||||
|
||||
const resources = await loadTranslations(locale, ns);
|
||||
|
||||
const _i18n = createInstance();
|
||||
_i18n.init({
|
||||
lng: locale,
|
||||
resources: {
|
||||
[locale]: {
|
||||
[ns]: resources,
|
||||
},
|
||||
},
|
||||
fallbackLng: "en",
|
||||
});
|
||||
|
||||
// Cache the i18n instance
|
||||
i18nInstanceCache.set(cacheKey, _i18n);
|
||||
return _i18n.getFixedT(locale, ns);
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getTranslation } from "@calcom/i18n/server";
|
||||
import { getBookingForReschedule } from "@calcom/features/bookings/lib/get-booking";
|
||||
import getAllUserBookings from "@calcom/features/bookings/lib/getAllUserBookings";
|
||||
import { getBookingFieldsWithSystemFields } from "@calcom/features/bookings/lib/getBookingFields";
|
||||
@@ -13,7 +14,6 @@ import { handleCreatePhoneCall } from "@calcom/features/handleCreatePhoneCall";
|
||||
import handleMarkNoShow from "@calcom/features/handleMarkNoShow";
|
||||
import { getRoutedUrl } from "@calcom/features/routing-forms/lib/getRoutedUrl";
|
||||
import { symmetricDecrypt, symmetricEncrypt } from "@calcom/lib/crypto";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
import { paymentDataSelect } from "@calcom/prisma/selects/payment";
|
||||
@@ -120,6 +120,7 @@ export { verifyPhoneNumber, sendVerificationCode };
|
||||
export { verifyCodeUnAuthenticated } from "@calcom/features/auth/lib/verifyCodeUnAuthenticated";
|
||||
export { sendEmailVerificationByCode } from "@calcom/features/auth/lib/verifyEmail";
|
||||
export { BookingAccessService } from "@calcom/features/bookings/services/BookingAccessService";
|
||||
export type { OrgMembershipLookup } from "@calcom/features/di/modules/OrgMembershipLookup";
|
||||
export { StripeBillingService } from "@calcom/features/ee/billing/service/billingProvider/StripeBillingService";
|
||||
export { TeamService } from "@calcom/features/ee/teams/services/teamService";
|
||||
export type { OAuth2Tokens } from "@calcom/features/oauth/services/OAuthService";
|
||||
@@ -132,4 +133,3 @@ export { verifyCodeChallenge } from "@calcom/lib/pkce";
|
||||
export { validateUrlForSSRFSync } from "@calcom/lib/ssrfProtection";
|
||||
export { checkEmailVerificationRequired } from "@calcom/trpc/server/routers/publicViewer/checkIfUserEmailVerificationRequired.handler";
|
||||
export { verifyCode as verifyCodeAuthenticated } from "@calcom/trpc/server/routers/viewer/organizations/verifyCode.handler";
|
||||
export type { OrgMembershipLookup } from "@calcom/features/di/modules/OrgMembershipLookup";
|
||||
@@ -18,6 +18,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@calcom/features": "workspace:*",
|
||||
"@calcom/i18n": "workspace:*",
|
||||
"@calcom/lib": "workspace:*"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -57,6 +57,7 @@ export default defineConfig({
|
||||
},
|
||||
rollupOptions: {
|
||||
external: [
|
||||
"@calcom/i18n",
|
||||
"react",
|
||||
"fs",
|
||||
"path",
|
||||
@@ -210,9 +211,6 @@ export default defineConfig({
|
||||
resolve: {
|
||||
conditions: ["node", "import", "require", "default"],
|
||||
alias: {
|
||||
"@calcom/lib/server/i18n": path.resolve(__dirname, "./i18n.ts"),
|
||||
"./server/i18n": path.resolve(__dirname, "./i18n.ts"),
|
||||
"../server/i18n": path.resolve(__dirname, "./i18n.ts"),
|
||||
"@": path.resolve(__dirname, "./src"),
|
||||
"@calcom/lib": path.resolve(__dirname, "../../lib"),
|
||||
"@calcom/trpc": resolve("../../trpc"),
|
||||
|
||||
Reference in new issue
Block a user