Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add translations helpers #560

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/interfaces/languageSwitcherInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ export interface HandleGroupedContentArgs extends HandleContentBaseArgs {
export interface HandleContentArgs extends HandleContentBaseArgs {
withinConfigurationExperience: boolean;
}

export interface UpdateValueFromTranslationArgs {
fieldName: string;
stringValue: string;
parentValue: any;
}
export interface ProcessTranslationValueArgs {
parentValue: any;
params: string[];
stringValue: string;
}

export interface ProcessTranslationNestedValueArgs {
parentValue: any;
param: string;
params: string[];
stringValue: string;
}
160 changes: 160 additions & 0 deletions src/structuralFunctionality/language-switcher/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import { isWithinConfigurationExperience } from "../../../functionality/configur
import {
HandleGroupedContentArgs,
LanguageData,
ProcessTranslationNestedValueArgs,
ProcessTranslationValueArgs,
UpdateValueFromTranslationArgs,
} from "../../../interfaces/languageSwitcherInterfaces";

// Determines default locale based on portal or locale url param
Expand Down Expand Up @@ -327,3 +330,160 @@ export async function getT9nData(
...configSettings,
};
}

export function convertT9nToConfigData(
data: { [key: string]: string },
base: ApplicationBase
): { [key: string]: any } {
const config = structuredClone(base.config);
const t9nData = {};

for (const key in data) {
const { fieldName, value } = getValuesToWrite(key, data[key], config);
t9nData[fieldName] = value;
}

return t9nData;
}

function getValuesToWrite(
fieldName: string,
value: string,
config: ApplicationConfig
): { fieldName: string; value: any } {
const { idItems } = getNestedIDs(fieldName);
let fieldNameToUse = fieldName;
let valueToUse = value;

if (idItems.length > 1) {
const parentID = idItems[0];
const parentValue = getDefaultValue(parentID, config);
fieldNameToUse = parentID;
valueToUse = updateValueFromTranslation({
fieldName,
stringValue: value,
parentValue,
});
}
return { fieldName: fieldNameToUse, value: valueToUse };
}

export function updateValueFromTranslation({
...args
}: UpdateValueFromTranslationArgs): any {
const { fieldName, stringValue, parentValue } = args;
const params = fieldName.split(".");
if (
Array.isArray(parentValue) ||
(typeof parentValue === "object" && parentValue !== null)
) {
params.shift();
processValueFromTranslation({
parentValue,
params,
stringValue,
});
return parentValue;
} else {
return stringValue;
}
}

function processValueFromTranslation({
...args
}: ProcessTranslationValueArgs): void {
const { parentValue, params, stringValue } = args;
const [param, ...restParams] = params;
if (Array.isArray(parentValue)) {
processArrayFromTranslation({
parentValue,
param,
params: restParams,
stringValue,
});
} else if (typeof parentValue === "object" && parentValue !== null) {
processObjectFromTranslation({
parentValue,
param,
params: restParams,
stringValue,
});
}
}

function processArrayFromTranslation({
...args
}: ProcessTranslationNestedValueArgs): void {
let { parentValue, param, params, stringValue } = args;
const [key, uid] = param.split("-");
for (const item of parentValue) {
if (item._uid === uid) {
if (typeof item[key] === "string" && item[key] !== stringValue) {
item[key] = stringValue;
break;
}
processValueFromTranslation({
parentValue: item[key],
params,
stringValue,
});
}
}
}

function processObjectFromTranslation({
...args
}: ProcessTranslationNestedValueArgs): void {
let { parentValue, param, params, stringValue } = args;
const result = parentValue[param];
if (Array.isArray(result)) {
processArrayFromTranslation({
parentValue: result,
param: params[0],
params: params.slice(1),
stringValue,
});
} else if (typeof result === "object" && result !== null) {
processValueFromTranslation({
parentValue: result,
params,
stringValue,
});
} else if (result !== stringValue) {
parentValue[param] = stringValue;
}
}

export function getNestedIDs(key: string): {
idItems: string[];
uid: string | null;
} {
const IDs = getNestedIDsArray(key);
const uid = IDs.length > 2 ? IDs[IDs.length - 1] : null;
if (uid) IDs.pop();
const data: { idItems: string[]; uid: string | null } = {
idItems: IDs,
uid: null,
};
if (uid) data.uid = uid;
return data;
}

function getNestedIDsArray(key: string): string[] {
const subtrings = key.split("-");
const subtrings2 = subtrings[0].split(".");
const IDs = [...subtrings2, subtrings[1]].filter(Boolean);
return IDs;
}

function getDefaultValue(
fieldName: string,
templateAppDataValues: ApplicationConfig
): any {
return templateAppDataValues?.draft &&
fieldName in templateAppDataValues.draft
? templateAppDataValues.draft[fieldName]
: fieldName in templateAppDataValues
? templateAppDataValues[fieldName]
: null;
}
Loading