Skip to content

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

Add translation for boolean dash filter value (#66050)

Denis Berezina183e8b5b4d2
diff --git a/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts b/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts
index 29449ae1a1a..49898b6a68d 100644
--- a/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts
+++ b/e2e/test/scenarios/admin/i18n/content-translation/dashboards.cy.spec.ts
@@ -676,7 +676,6 @@ describe("scenarios > content translation > static embedding > dashboards", () =
);
cy.wait("@cardQuery");
- H.filterWidget().contains("vrai");
cy.findByTestId("table-body").within(() => {
cy.findAllByText(/vrai/).should("have.length", 2);
cy.findAllByText(/true/).should("have.length", 0);
diff --git a/e2e/test/scenarios/dashboard-filters/dashboard-filters-boolean.cy.spec.ts b/e2e/test/scenarios/dashboard-filters/dashboard-filters-boolean.cy.spec.ts
index f4592a39f5d..8c51ff4c471 100644
--- a/e2e/test/scenarios/dashboard-filters/dashboard-filters-boolean.cy.spec.ts
+++ b/e2e/test/scenarios/dashboard-filters/dashboard-filters-boolean.cy.spec.ts
@@ -82,10 +82,10 @@ describe(
cy.log("assert click behavior");
H.getDashboardCard().findAllByText("true").first().click();
- H.filterWidget().findByText("true").should("be.visible");
+ H.filterWidget().findByText("True").should("be.visible");
H.getDashboardCard().findByText("1 row").should("be.visible");
H.getDashboardCard().findAllByText("true").first().click();
- H.filterWidget().findByText("true").should("not.exist");
+ H.filterWidget().findByText("True").should("not.exist");
H.getDashboardCard().findByText("200 rows").should("be.visible");
});
@@ -128,7 +128,7 @@ describe(
cy.log("assert click behavior");
H.getDashboardCard().findAllByText("true").first().click();
H.dashboardHeader().findByText(DASHBOARD_NAME).should("be.visible");
- H.filterWidget().findByText("true").should("be.visible");
+ H.filterWidget().findByText("True").should("be.visible");
});
it("should allow to use a 'Go to a custom destination - Dashboard' click behavior with a parameter", () => {
@@ -141,7 +141,7 @@ describe(
H.popover().button("Add filter").click();
H.getDashboardCard().findAllByText("true").first().click();
H.dashboardHeader().findByText(DASHBOARD_NAME).should("be.visible");
- H.filterWidget().findByText("true").should("be.visible");
+ H.filterWidget().findByText("True").should("be.visible");
});
});
@@ -166,7 +166,7 @@ describe(
H.getDashboardCard().findByText(QUESTION_NAME).click();
H.queryBuilderHeader().findByText(QUESTION_NAME).should("be.visible");
H.assertQueryBuilderRowCount(1);
- H.filterWidget().findByText("true").should("be.visible");
+ H.filterWidget().findByText("True").should("be.visible");
});
it("should allow to use a 'Go to a custom destination - Saved question' click behavior", () => {
@@ -195,7 +195,7 @@ describe(
cy.log("assert click behavior");
H.getDashboardCard().findAllByText("true").first().click();
H.assertTableRowsCount(1);
- H.filterWidget().findByText("true").should("be.visible");
+ H.filterWidget().findByText("True").should("be.visible");
});
it("should allow to use a 'Go to a custom destination - URL' click behavior", () => {
@@ -227,7 +227,7 @@ describe(
cy.log("assert click behavior");
H.getDashboardCard().findAllByText("true").first().click();
H.assertTableRowsCount(1);
- H.filterWidget().findByText("true").should("be.visible");
+ H.filterWidget().findByText("True").should("be.visible");
},
);
});
@@ -253,7 +253,7 @@ describe(
H.getDashboardCard().findByText(QUESTION_NAME).click();
H.queryBuilderHeader().findByText(QUESTION_NAME).should("be.visible");
H.assertQueryBuilderRowCount(53);
- H.filterWidget().findByText("true").should("be.visible");
+ H.filterWidget().findByText("True").should("be.visible");
});
it("should allow to use boolean parameters mapped to SQL query parameters in a public dashboard", () => {
diff --git a/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.tsx b/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.tsx
index ae179dfbb12..0f6583df78a 100644
--- a/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.tsx
+++ b/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.tsx
@@ -1,3 +1,5 @@
+import { t } from "ttag";
+
import { Ellipsified } from "metabase/common/components/Ellipsified";
import { ParameterFieldWidgetValue } from "metabase/parameters/components/widgets/ParameterFieldWidget/ParameterFieldWidgetValue/ParameterFieldWidgetValue";
import { formatParameterValue } from "metabase/parameters/utils/formatting";
@@ -8,6 +10,7 @@ import {
isFieldFilterUiParameter,
} from "metabase-lib/v1/parameters/utils/parameter-fields";
import {
+ isBooleanParameter,
isDateParameter,
isStringParameter,
isTemporalUnitParameter,
@@ -22,7 +25,7 @@ import type {
export type FormattedParameterValueProps = {
parameter: UiParameter;
- value: string | number | number[];
+ value: string | number | number[] | ParameterValue;
cardId?: CardId;
dashboardId?: DashboardId;
placeholder?: string;
@@ -47,7 +50,9 @@ function FormattedParameterValue({
(value) => getValue(value)?.toString() === first?.toString(),
);
- const label = getLabel(displayValue);
+ const label = !isBooleanParameter(parameter)
+ ? getLabel(displayValue)
+ : getBooleanLabel(first as boolean);
const renderContent = () => {
if (
@@ -101,7 +106,7 @@ function getValue(
}
function getLabel(
- value: string | ParameterValue | undefined,
+ value: boolean | string | ParameterValue | undefined,
): string | undefined {
if (Array.isArray(value)) {
return value[1];
@@ -109,5 +114,9 @@ function getLabel(
return value?.toString();
}
+function getBooleanLabel(value: boolean) {
+ return value ? t`True` : t`False`;
+}
+
// eslint-disable-next-line import/no-default-export -- deprecated usage
export default FormattedParameterValue;
diff --git a/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.unit.spec.tsx b/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.unit.spec.tsx
index b1d03990e53..faee8710d25 100644
--- a/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.unit.spec.tsx
+++ b/frontend/src/metabase/parameters/components/FormattedParameterValue/FormattedParameterValue.unit.spec.tsx
@@ -1,5 +1,11 @@
import { renderWithProviders, screen } from "__support__/ui";
+import { setLocalization } from "metabase/lib/i18n";
import { createMockParameter } from "metabase-types/api/mocks";
+import type { State } from "metabase-types/store";
+import {
+ createMockSettingsState,
+ createMockState,
+} from "metabase-types/store/mocks";
import FormattedParameterValue, {
type FormattedParameterValueProps,
@@ -7,13 +13,23 @@ import FormattedParameterValue, {
type SetupOpts = FormattedParameterValueProps;
-function setup({ parameter, value, placeholder }: SetupOpts) {
+function setup({
+ parameter,
+ value,
+ placeholder,
+ storeInitialState,
+}: SetupOpts & {
+ storeInitialState?: Partial<State>;
+}) {
return renderWithProviders(
<FormattedParameterValue
parameter={parameter}
value={value}
placeholder={placeholder}
/>,
+ {
+ storeInitialState,
+ },
);
}
@@ -45,4 +61,37 @@ describe("FormattedParameterValue", () => {
expect(screen.getByText("B")).toBeInTheDocument();
});
+
+ it("should translate boolean filter value", () => {
+ setLocalization({
+ headers: {
+ language: "fr",
+ "plural-forms": "nplurals=2; plural=(n != 1);",
+ },
+ translations: {
+ "": {
+ True: {
+ msgstr: ["Vrai"],
+ },
+ False: {
+ msgstr: ["Faux"],
+ },
+ },
+ },
+ });
+
+ setup({
+ value: [false],
+ parameter: createMockParameter({
+ name: "Boolean",
+ slug: "boolean",
+ type: "boolean/=",
+ }),
+ storeInitialState: createMockState({
+ settings: createMockSettingsState({ "site-locale": "fr" }),
+ }),
+ });
+
+ expect(screen.getByText("Faux")).toBeInTheDocument();
+ });
});
diff --git a/frontend/src/metabase/parameters/utils/formatting.ts b/frontend/src/metabase/parameters/utils/formatting.ts
index efd2207b269..e44afec2808 100644
--- a/frontend/src/metabase/parameters/utils/formatting.ts
+++ b/frontend/src/metabase/parameters/utils/formatting.ts
@@ -14,6 +14,7 @@ import {
isFieldFilterParameter,
isTemporalUnitParameter,
} from "metabase-lib/v1/parameters/utils/parameter-type";
+import type { ParameterValue, RowValue } from "metabase-types/api";
import { formatDateValue } from "./date-formatting";
@@ -38,14 +39,14 @@ function formatWithInferredType(value: any, parameter: UiParameter) {
}
export function formatParameterValue(
- value: string | number | number[],
+ rawValue: string | number | number[] | ParameterValue,
parameter: UiParameter,
) {
- if (Array.isArray(value) && value.length > 1) {
- return renderNumberOfSelections(value.length);
+ if (Array.isArray(rawValue) && rawValue.length > 1) {
+ return renderNumberOfSelections(rawValue.length);
}
- value = Array.isArray(value) ? value[0] : value;
+ const value: RowValue = Array.isArray(rawValue) ? rawValue[0] : rawValue;
if (isDateParameter(parameter)) {
return formatDateValue(parameter, String(value));