Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

feat(sdk,embed-js): Add content translations for Collection/MetabaseBrowser components (#68608)

Timofey Kachalovb81aa809ade7
diff --git a/e2e/test-component/scenarios/embedding-sdk/content-translations.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/content-translations.cy.spec.tsx
index e92b9bd0899..478e15118f4 100644
--- a/e2e/test-component/scenarios/embedding-sdk/content-translations.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/content-translations.cy.spec.tsx
@@ -1,4 +1,7 @@
-import { InteractiveQuestion } from "@metabase/embedding-sdk-react";
+import {
+ CollectionBrowser,
+ InteractiveQuestion,
+} from "@metabase/embedding-sdk-react";
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
import { createQuestion, popover } from "e2e/support/helpers";
@@ -289,4 +292,82 @@ describe("scenarios > embedding-sdk > content-translations", () => {
});
});
});
+
+ describe("collection browser", () => {
+ it("should translate collection names, item names, and descriptions", () => {
+ signInAsAdminAndEnableEmbeddingSdk();
+
+ cy.request("POST", "/api/collection", {
+ name: "Test Collection",
+ parent_id: null,
+ }).then(({ body: collection }) => {
+ cy.wrap(collection.id).as("collectionId");
+
+ cy.request("POST", "/api/dashboard", {
+ name: "Test Dashboard",
+ description: "Dashboard description text",
+ collection_id: collection.id,
+ });
+
+ createQuestion({
+ name: "Test Question",
+ description: "Question description text",
+ collection_id: collection.id,
+ query: {
+ "source-table": ORDERS_ID,
+ limit: 1,
+ },
+ });
+ });
+
+ uploadTranslationDictionaryViaAPI([
+ { locale: "de", msgid: "Test Collection", msgstr: "Test Sammlung" },
+ {
+ locale: "de",
+ msgid: "Test Dashboard",
+ msgstr: "Test Armaturenbrett",
+ },
+ {
+ locale: "de",
+ msgid: "Dashboard description text",
+ msgstr: "Armaturenbrett Beschreibungstext",
+ },
+ { locale: "de", msgid: "Test Question", msgstr: "Testfrage" },
+ {
+ locale: "de",
+ msgid: "Question description text",
+ msgstr: "Frage Beschreibungstext",
+ },
+ ]);
+
+ cy.signOut();
+ mockAuthProviderAndJwtSignIn();
+
+ cy.get("@collectionId").then((collectionId) => {
+ mountSdkContent(
+ <Flex p="xl">
+ <CollectionBrowser
+ collectionId={collectionId as unknown as number}
+ visibleColumns={["type", "name", "description"]}
+ />
+ </Flex>,
+ {
+ sdkProviderProps: {
+ locale: "de",
+ },
+ },
+ );
+
+ getSdkRoot().within(() => {
+ cy.findByText("Test Sammlung").should("be.visible");
+ cy.findByText("Test Armaturenbrett").should("be.visible");
+ cy.findByText("Testfrage").should("be.visible");
+ cy.findByText("Armaturenbrett Beschreibungstext").should(
+ "be.visible",
+ );
+ cy.findByText("Frage Beschreibungstext").should("be.visible");
+ });
+ });
+ });
+ });
});
diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding/content-translations.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding/content-translations.cy.spec.ts
new file mode 100644
index 00000000000..d108ec3c35c
--- /dev/null
+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding/content-translations.cy.spec.ts
@@ -0,0 +1,153 @@
+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+import { uploadTranslationDictionaryViaAPI } from "e2e/support/helpers/e2e-content-translation-helpers";
+
+const { H } = cy;
+const { ORDERS_ID } = SAMPLE_DATABASE;
+
+const setupEmbed = (elementHtml: string) => {
+ H.visitCustomHtmlPage(`
+ ${H.getNewEmbedScriptTag()}
+ ${H.getNewEmbedConfigurationScript({ locale: "de" })}
+ ${elementHtml}
+ `);
+};
+
+describe("scenarios > embedding > sdk iframe embedding > content-translations", () => {
+ describe("metabase-browser", () => {
+ const setupContentTranslations = () => {
+ H.createCollection({
+ name: "Test Collection",
+ description: "Test description",
+ }).then(({ body: collection }) => {
+ cy.wrap(collection.id).as("collectionId");
+
+ H.createDashboard({
+ name: "Test Dashboard",
+ description: "Dashboard description text",
+ collection_id: collection.id,
+ });
+
+ H.createQuestion({
+ name: "Test Question",
+ description: "Question description text",
+ collection_id: collection.id,
+ query: {
+ "source-table": ORDERS_ID,
+ limit: 1,
+ },
+ });
+ });
+
+ uploadTranslationDictionaryViaAPI([
+ { locale: "de", msgid: "Test Collection", msgstr: "Test Sammlung" },
+ {
+ locale: "de",
+ msgid: "Test description",
+ msgstr: "Testbeschreibung",
+ },
+ {
+ locale: "de",
+ msgid: "Test Dashboard",
+ msgstr: "Test Armaturenbrett",
+ },
+ {
+ locale: "de",
+ msgid: "Dashboard description text",
+ msgstr: "Armaturenbrett Beschreibungstext",
+ },
+ { locale: "de", msgid: "Test Question", msgstr: "Testfrage" },
+ {
+ locale: "de",
+ msgid: "Question description text",
+ msgstr: "Frage Beschreibungstext",
+ },
+ ]);
+ };
+
+ it("should translate content with read-only='true'", () => {
+ H.prepareSdkIframeEmbedTest({
+ withToken: "bleeding-edge",
+ signOut: false,
+ });
+
+ setupContentTranslations();
+
+ cy.get("@collectionId").then((collectionId) => {
+ setupEmbed(`
+ <metabase-browser
+ initial-collection="${collectionId}"
+ read-only="true"
+ />
+ `);
+
+ H.getSimpleEmbedIframeContent().within(() => {
+ cy.findByTestId("sdk-breadcrumbs").within(() => {
+ cy.findByText("Test Sammlung").should("be.visible");
+ });
+
+ cy.findByTestId("collection-table").within(() => {
+ cy.findByText("Test Armaturenbrett").should("be.visible");
+ cy.findByText("Testfrage").should("be.visible");
+
+ cy.findByText("Testfrage").click();
+ });
+
+ cy.findByTestId("sdk-breadcrumbs").within(() => {
+ cy.findByText("Test Sammlung").should("be.visible");
+ cy.findByText("Testfrage").should("be.visible");
+ });
+ });
+ });
+ });
+
+ it("should translate content with read-only='false'", () => {
+ H.prepareSdkIframeEmbedTest({
+ withToken: "bleeding-edge",
+ signOut: false,
+ });
+
+ setupContentTranslations();
+
+ cy.get("@collectionId").then((collectionId) => {
+ setupEmbed(`
+ <metabase-browser
+ initial-collection="${collectionId}"
+ read-only="false"
+ />
+ `);
+
+ H.getSimpleEmbedIframeContent().within(() => {
+ cy.findByText("Neues Dashboard").click();
+
+ H.modal().within(() => {
+ cy.findByText("Test Sammlung").should("be.visible");
+ cy.findByText("Test Sammlung").click();
+ });
+
+ H.modal()
+ .should("have.length", 2)
+ .last()
+ .within(() => {
+ cy.findAllByTestId("result-item").first().should("be.visible");
+
+ cy.findAllByTestId("result-item")
+ .first()
+ .within(() => {
+ cy.icon("info").click();
+ });
+ });
+
+ H.tooltip().should("contain", "Testbeschreibung");
+
+ H.modal()
+ .should("have.length", 2)
+ .last()
+ .within(() => {
+ cy.findByText("Sammlungen").click();
+ cy.findByText("Test Sammlung").should("be.visible");
+ });
+ });
+ });
+ });
+ });
+});
diff --git a/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx b/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx
index b1f7991ae6a..850270dbe88 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/public/CollectionBrowser/CollectionBrowser.tsx
@@ -155,7 +155,11 @@ export const CollectionBrowserInner = ({
if (item.model === "collection") {
if (isGlobalBreadcrumbEnabled) {
- reportLocation({ type: "collection", id: item.id, name: item.name });
+ reportLocation({
+ type: "collection",
+ id: item.id,
+ name: item.name,
+ });
return;
}
diff --git a/frontend/src/metabase/common/components/CollectionName.jsx b/frontend/src/metabase/common/components/CollectionName.jsx
index 97cb8acf675..d1170ecdaa1 100644
--- a/frontend/src/metabase/common/components/CollectionName.jsx
+++ b/frontend/src/metabase/common/components/CollectionName.jsx
@@ -1,9 +1,12 @@
/* eslint-disable react/prop-types */
import { Collections, ROOT_COLLECTION } from "metabase/entities/collections";
+import { useTranslateContent } from "metabase/i18n/hooks";
const CollectionName = ({ id }) => {
+ const tc = useTranslateContent();
+
if (id === "root" || id === null) {
- return <span>{ROOT_COLLECTION.name}</span>;
+ return <span>{tc(ROOT_COLLECTION.name)}</span>;
} else if (id === undefined || isNaN(id)) {
return null;
} else {
diff --git a/frontend/src/metabase/common/components/EntityPicker/components/ItemList/ItemList.tsx b/frontend/src/metabase/common/components/EntityPicker/components/ItemList/ItemList.tsx
index 64a0fef2df7..a5af143177c 100644
--- a/frontend/src/metabase/common/components/EntityPicker/components/ItemList/ItemList.tsx
+++ b/frontend/src/metabase/common/components/EntityPicker/components/ItemList/ItemList.tsx
@@ -3,6 +3,7 @@ import { useMemo } from "react";
import { t } from "ttag";
import { VirtualizedList } from "metabase/common/components/VirtualizedList";
+import { useTranslateContent } from "metabase/i18n/hooks";
import { useSelector } from "metabase/lib/redux";
import { PLUGIN_MODERATION } from "metabase/plugins";
import { LoadingAndErrorWrapper } from "metabase/public/containers/PublicAction/PublicAction.styled";
@@ -58,6 +59,8 @@ export const ItemList = <
navLinkProps,
containerProps = { pb: "xs" },
}: ItemListProps<Id, Model, Item>) => {
+ const tc = useTranslateContent();
+
const isTenantUser = useSelector(getIsTenantUser);
const filteredItems =
items && shouldShowItem ? items.filter(shouldShowItem) : items;
@@ -118,7 +121,7 @@ export const ItemList = <
mb={0}
label={
<Flex align="center">
- {item.name}{" "}
+ {tc(item.name)}{" "}
<PLUGIN_MODERATION.ModerationStatusIcon
status={item.moderated_status}
filled
diff --git a/frontend/src/metabase/common/components/EntityPicker/components/ResultItem/ResultItem.tsx b/frontend/src/metabase/common/components/EntityPicker/components/ResultItem/ResultItem.tsx
index 6b7fca035e9..cd19c4198e1 100644
--- a/frontend/src/metabase/common/components/EntityPicker/components/ResultItem/ResultItem.tsx
+++ b/frontend/src/metabase/common/components/EntityPicker/components/ResultItem/ResultItem.tsx
@@ -1,6 +1,7 @@
import { t } from "ttag";
import { Ellipsified } from "metabase/common/components/Ellipsified";
+import { useTranslateContent } from "metabase/i18n/hooks";
import { humanize, titleize } from "metabase/lib/formatting";
import { getIcon } from "metabase/lib/icon";
import { getName } from "metabase/lib/name";
@@ -22,6 +23,8 @@ export const ResultItem = ({
isSelected?: boolean;
isLast?: boolean;
}) => {
+ const tc = useTranslateContent();
+
const icon = getIcon(item);
const parentInfo = getParentInfo(item);
@@ -43,7 +46,7 @@ export const ResultItem = ({
}}
/>
<Ellipsified style={{ fontWeight: "bold" }}>
- {getName(item)}
+ {tc(getName(item))}
</Ellipsified>
<PLUGIN_MODERATION.ModerationStatusIcon
status={item.moderated_status}
@@ -51,7 +54,7 @@ export const ResultItem = ({
size={14}
/>
{item.description && (
- <Tooltip maw="20rem" multiline label={item.description}>
+ <Tooltip maw="20rem" multiline label={tc(item.description)}>
<FixedSizeIcon c="brand" name="info" />
</Tooltip>
)}
diff --git a/frontend/src/metabase/common/components/ItemsTable/Columns.tsx b/frontend/src/metabase/common/components/ItemsTable/Columns.tsx
index eb0c4d8ccf4..6d7ef8521e8 100644
--- a/frontend/src/metabase/common/components/ItemsTable/Columns.tsx
+++ b/frontend/src/metabase/common/components/ItemsTable/Columns.tsx
@@ -10,6 +10,7 @@ import EntityItem from "metabase/common/components/EntityItem";
import Markdown from "metabase/common/components/Markdown";
import { ArchiveButton } from "metabase/embedding/components/ArchiveButton";
import { isEmbeddingSdk } from "metabase/embedding-sdk/config";
+import { useTranslateContent } from "metabase/i18n/hooks";
import { modelToUrl } from "metabase/lib/urls";
import { getUserName } from "metabase/lib/user";
import { PLUGIN_MODERATION } from "metabase/plugins";
@@ -165,10 +166,12 @@ export const Columns = {
includeDescription?: boolean;
onClick?: (item: CollectionItem) => void;
}) => {
+ const tc = useTranslateContent();
+
return (
<ItemNameCell data-testid={`${testIdPrefix}-name`}>
<ItemLinkComponent onClick={onClick} item={item}>
- <EntityItem.Name name={item.name} variant="list" />
+ <EntityItem.Name name={tc(item.name)} variant="list" />
<PLUGIN_MODERATION.ModerationStatusIcon
size={16}
status={item.moderated_status}
@@ -179,7 +182,7 @@ export const Columns = {
size={16}
tooltip={
<Markdown dark disallowHeading unstyleLinks lineClamp={8}>
- {item.description}
+ {tc(item.description)}
</Markdown>
}
/>
@@ -214,9 +217,11 @@ export const Columns = {
testIdPrefix?: string;
onClick?: (item: CollectionItem) => void;
}) => {
+ const tc = useTranslateContent();
+
return (
<ItemCell data-testid={`${testIdPrefix}-description`}>
- <Ellipsified>{item.description ?? ""}</Ellipsified>
+ <Ellipsified>{tc(item.description) ?? ""}</Ellipsified>
</ItemCell>
);
},
diff --git a/frontend/src/metabase/entities/containers/EntityName.tsx b/frontend/src/metabase/entities/containers/EntityName.tsx
index e301a7631d1..47fbe813889 100644
--- a/frontend/src/metabase/entities/containers/EntityName.tsx
+++ b/frontend/src/metabase/entities/containers/EntityName.tsx
@@ -1,5 +1,7 @@
import { useMemo } from "react";
+import { useTranslateContent } from "metabase/i18n/hooks";
+
import type { EntityDefinition, EntityId, EntityType } from "./rtk-query";
interface Props {
@@ -14,6 +16,8 @@ export const EntityName = <Entity, EntityWrapper>({
entityType,
entityId,
}: Props) => {
+ const tc = useTranslateContent();
+
const entityDefinition: EntityDefinition<Entity, EntityWrapper> =
useMemo(() => {
// dynamic require due to circular dependencies
@@ -38,5 +42,5 @@ export const EntityName = <Entity, EntityWrapper>({
return null;
}
- return <span>{entityDefinition.objectSelectors.getName(entity)}</span>;
+ return <span>{tc(entityDefinition.objectSelectors.getName(entity))}</span>;
};
diff --git a/frontend/src/metabase/nav/components/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx b/frontend/src/metabase/nav/components/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx
index 7cef7cb14a5..65c3523efc4 100644
--- a/frontend/src/metabase/nav/components/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx
+++ b/frontend/src/metabase/nav/components/CollectionBreadcrumbs/CollectionBreadcrumbs.tsx
@@ -3,6 +3,7 @@ import { t } from "ttag";
import { Badge } from "metabase/common/components/Badge";
import { useToggle } from "metabase/common/hooks/use-toggle";
+import { useTranslateContent } from "metabase/i18n/hooks";
import * as Urls from "metabase/lib/urls";
import { CollectionBadge } from "metabase/questions/components/CollectionBadge";
import { ActionIcon, Box, Flex, Icon } from "metabase/ui";
@@ -29,6 +30,7 @@ export const CollectionBreadcrumbs = ({
baseCollectionId = null,
}: CollectionBreadcrumbsProps): JSX.Element | null => {
const [isExpanded, { toggle }] = useToggle(false);
+ const tc = useTranslateContent();
if (!collection) {
return null;
@@ -100,7 +102,7 @@ export const CollectionBreadcrumbs = ({
isSingleLine
to={Urls.dashboard(dashboard)}
>
- {dashboard.name}
+ {tc(dashboard.name)}
</Badge>
</>
)}
diff --git a/frontend/src/metabase/questions/components/CollectionBadge.tsx b/frontend/src/metabase/questions/components/CollectionBadge.tsx
index 906fb4a8114..cf36e78028e 100644
--- a/frontend/src/metabase/questions/components/CollectionBadge.tsx
+++ b/frontend/src/metabase/questions/components/CollectionBadge.tsx
@@ -2,6 +2,7 @@ import type { ComponentType, PropsWithChildren } from "react";
import { Badge } from "metabase/common/components/Badge";
import { Collections } from "metabase/entities/collections";
+import { useTranslateContent } from "metabase/i18n/hooks";
import { getIcon } from "metabase/lib/icon";
import { modelToUrl } from "metabase/lib/urls/modelToUrl";
import { PLUGIN_COLLECTIONS } from "metabase/plugins";
@@ -34,6 +35,8 @@ const CollectionBadgeInner = ({
isSingleLine,
onClick,
}: CollectionBadgeProps) => {
+ const tc = useTranslateContent();
+
if (!collection) {
return null;
}
@@ -56,7 +59,7 @@ const CollectionBadgeInner = ({
isSingleLine={isSingleLine}
{...clickActionProps}
>
- {collection.getName()}
+ {tc(collection.getName())}
</Badge>
);
};