Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,11 @@ describe("ReviewsSection", async () => {
streamDocument: document,
});

const templateProps = {
document: document,
data: data,
};

const updatedData = await injectTranslations(templateProps);
const translations = await injectTranslations(document);

const { container } = reactRender(
<VisualEditorProvider templateProps={updatedData}>
<Render config={puckConfig} data={updatedData.data} />
<VisualEditorProvider templateProps={{ document, translations }}>
<Render config={puckConfig} data={data} />
</VisualEditorProvider>
);

Expand Down
61 changes: 14 additions & 47 deletions packages/visual-editor/src/utils/i18n/components.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import { getTranslations } from "./getTranslations";
import { StreamDocument } from "../applyTheme";
import { normalizeLocalesInObject } from "../normalizeLocale";

const NAMESPACE = "visual-editor";

Expand Down Expand Up @@ -28,55 +31,18 @@ export interface TemplateProps {
* by consumers of visual-editor in transformProps of a template.
*/
export const injectTranslations = async (
templateProps: TemplateProps
): Promise<TemplateProps> => {
if (!templateProps?.document?.locale) {
return templateProps;
}

const translations =
(await getTranslations(templateProps?.document?.locale)) || {};

return {
...templateProps,
translations,
};
};

/**
* Dynamically imports the translation file for the given locale.
*/
const getTranslations = async (
locale: string,
isRetry = false
): Promise<Record<string, string>> => {
if (!locale) {
streamDocument: StreamDocument
): Promise<Record<string, string> | Record<string, any>> => {
if (!streamDocument?.locale) {
return {};
}

try {
const module = await import(
`../../../locales/components/${locale}/visual-editor.json`
);
return module.default;
} catch (e) {
if (isRetry || locale === "en") {
console.error(
"Error loading translations for locale",
locale,
e,
"No fallback available."
);
return {};
}
console.error(
"Error loading translations for locale",
locale,
e,
"Falling back to en."
);
return getTranslations("en", true);
}
return (
(await getTranslations(
normalizeLocalesInObject(streamDocument).locale,
"components"
)) || {}
);
};

/**
Expand All @@ -92,7 +58,8 @@ export const loadComponentTranslations = async (
return;
}

const translationsToInject = translations || (await getTranslations(locale));
const translationsToInject =
translations || (await getTranslations(locale, "components"));

if (translationsToInject && Object.keys(translationsToInject).length > 0) {
i18nComponentsInstance.addResourceBundle(
Expand Down
43 changes: 0 additions & 43 deletions packages/visual-editor/src/utils/i18n/fallbacks.ts

This file was deleted.

46 changes: 46 additions & 0 deletions packages/visual-editor/src/utils/i18n/getTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const supportedRegionalLocales = ["en-GB", "zh-TW"];

/**
* Dynamically imports the translation file for the given locale.
*/
export const getTranslations = async (
locale: string,
instance: "platform" | "components",
isRetry = false
): Promise<Record<string, string>> => {
if (!locale) {
return {};
}

let strippedLocale = locale;
if (!supportedRegionalLocales.includes(locale)) {
strippedLocale = locale.split("-")[0];
}
if (locale.includes("zh-Hant")) {
strippedLocale = "zh-TW";
}

try {
const module = await import(
`../../../locales/${instance}/${strippedLocale}/visual-editor.json`
);
return module.default;
} catch (e) {
if (isRetry || strippedLocale === "en") {
console.error(
`Error loading ${instance} translations for locale`,
locale,
e,
"No fallback available."
);
return {};
}
console.error(
`Error loading ${instance} translations for locale`,
locale,
e,
"Falling back to en."
);
return getTranslations("en", instance, true);
}
};
41 changes: 2 additions & 39 deletions packages/visual-editor/src/utils/i18n/platform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import i18next, { TOptions } from "i18next";
import { initReactI18next, useTranslation } from "react-i18next";
import { applyI18nFallbacks, defaultI18nFallbacks } from "./fallbacks.ts";
import { getTranslations } from "./getTranslations.ts";

const NAMESPACE = "visual-editor";

Expand All @@ -16,42 +16,6 @@ i18nPlatformInstance.use(initReactI18next).init({
resources: {},
});

/**
* Dynamically imports the translation file for the given locale.
*/
const getTranslations = async (
locale: string,
isRetry = false
): Promise<Record<string, string>> => {
if (!locale) {
return {};
}

try {
const module = await import(
`../../../locales/platform/${locale}/visual-editor.json`
);
return module.default;
} catch (e) {
if (isRetry || locale === "en") {
console.error(
"Error loading translations for locale",
locale,
e,
"No fallback available."
);
return {};
}
console.error(
"Error loading translations for locale",
locale,
e,
"Falling back to en."
);
return getTranslations("en", true);
}
};

/**
* Loads translations into the i18n instance for the given locale.
*/
Expand All @@ -60,8 +24,7 @@ export const loadPlatformTranslations = async (locale: string) => {
return;
}

const translationsToInject = await getTranslations(locale);
applyI18nFallbacks(translationsToInject, defaultI18nFallbacks);
const translationsToInject = await getTranslations(locale, "platform");

if (translationsToInject && Object.keys(translationsToInject).length > 0) {
i18nPlatformInstance.addResourceBundle(
Expand Down
12 changes: 6 additions & 6 deletions packages/visual-editor/src/vite-plugin/templates/directory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ export const getPath: GetPath<TemplateProps> = ({ document }) => {

export const transformProps: TransformProps<TemplateProps> = async (props) => {
const { document } = props;

const migratedData = migrate(
JSON.parse(document.__.layout),
migrationRegistry,
directoryConfig,
document
);
const updatedData = await injectTranslations(
await resolveAllData(migratedData, directoryConfig, {
streamDocument: document,
})
);
const resolvedPuckData = await resolveAllData(migratedData, directoryConfig, {
streamDocument: document,
});
const translations = await injectTranslations(document);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests are failing because I had to do this in that fr test manually, so that would need the update too.


return { ...props, data: updatedData };
return { ...props, data: resolvedPuckData, translations };
};

const Directory: Template<TemplateRenderProps> = (props) => {
Expand Down
12 changes: 6 additions & 6 deletions packages/visual-editor/src/vite-plugin/templates/locator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ export const getPath: GetPath<TemplateProps> = ({ document }) => {

export const transformProps: TransformProps<TemplateProps> = async (props) => {
const { document } = props;

const migratedData = migrate(
JSON.parse(document.__.layout),
migrationRegistry,
locatorConfig,
document
);
const updatedData = await injectTranslations(
await resolveAllData(migratedData, locatorConfig, {
streamDocument: document,
})
);
const resolvedPuckData = await resolveAllData(migratedData, locatorConfig, {
streamDocument: document,
});
const translations = await injectTranslations(document);

return { ...props, data: updatedData };
return { ...props, data: resolvedPuckData, translations };
};

const Locator: Template<TemplateRenderProps> = (props) => {
Expand Down
13 changes: 7 additions & 6 deletions packages/visual-editor/src/vite-plugin/templates/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,24 @@ export const getPath: GetPath<TemplateProps> = ({

export const transformProps: TransformProps<TemplateProps> = async (props) => {
const { document } = props;

const migratedData = migrate(
JSON.parse(document.__.layout),
migrationRegistry,
mainConfig,
document
);
const updatedData = await injectTranslations(
await resolveAllData(migratedData, mainConfig, {
streamDocument: document,
})
);
const resolvedPuckData = await resolveAllData(migratedData, mainConfig, {
streamDocument: document,
});
const translations = await injectTranslations(document);

return { ...props, data: updatedData };
return { ...props, data: resolvedPuckData, translations };
};

const Location: Template<TemplateRenderProps> = (props) => {
const { document, data } = props;

const filteredConfig = filterComponentsFromConfig(
mainConfig,
document?._additionalLayoutComponents,
Expand Down
3 changes: 2 additions & 1 deletion starter/src/templates/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export const getHeadConfig: GetHeadConfig<TemplateRenderProps> = (
export const transformProps: TransformProps<TemplateProps<any>> = async (
data,
) => {
return await injectTranslations(data);
const translations = await injectTranslations(data.document);
return { ...data, translations };
};

export const getPath: GetPath<TemplateProps> = ({ document }) => {
Expand Down
Binary file removed yext-visual-editor-1.0.5test3.tgz
Binary file not shown.
Loading