mirror of
https://github.com/formbricks/formbricks.git
synced 2026-03-12 20:21:10 -05:00
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com> Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com>
29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
import "server-only";
|
|
import { TI18nString } from "@formbricks/types/surveys/types";
|
|
import { structuredClone } from "../pollyfills/structuredClone";
|
|
import { isI18nObject } from "./utils";
|
|
|
|
// Helper function to extract a regular string from an i18nString.
|
|
const extractStringFromI18n = (i18nString: TI18nString, languageCode: string): string => {
|
|
if (typeof i18nString === "object" && i18nString !== null) {
|
|
return i18nString[languageCode] || "";
|
|
}
|
|
return i18nString;
|
|
};
|
|
|
|
// Assuming I18nString and extraction logic are defined
|
|
const reverseTranslateObject = <T extends Record<string, any>>(obj: T, languageCode: string): T => {
|
|
const clonedObj = structuredClone(obj);
|
|
for (let key in clonedObj) {
|
|
const value = clonedObj[key];
|
|
if (isI18nObject(value)) {
|
|
// Now TypeScript knows `value` is I18nString, treat it accordingly
|
|
clonedObj[key] = extractStringFromI18n(value, languageCode) as T[Extract<keyof T, string>];
|
|
} else if (typeof value === "object" && value !== null) {
|
|
// Recursively handle nested objects
|
|
clonedObj[key] = reverseTranslateObject(value, languageCode);
|
|
}
|
|
}
|
|
return clonedObj;
|
|
};
|