Skip to content

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

Fix leaking plural template (#77606)

Nick Fitzpatrick9cb27d398651
diff --git a/frontend/src/metabase/common/components/Schedule/Schedule.tsx b/frontend/src/metabase/common/components/Schedule/Schedule.tsx
index 472b1b72e94..f0f3a53d9b6 100644
--- a/frontend/src/metabase/common/components/Schedule/Schedule.tsx
+++ b/frontend/src/metabase/common/components/Schedule/Schedule.tsx
@@ -1,7 +1,7 @@
import cx from "classnames";
import { type HTMLAttributes, useCallback, useMemo, useState } from "react";
import { match } from "ts-pattern";
-import { c, msgid, ngettext } from "ttag";
+import { c } from "ttag";
import _ from "underscore";
import { CronExpressionInput } from "metabase/common/components/CronExpressioInput";
@@ -204,17 +204,22 @@ export const Schedule = ({
);
return match(schedule_type)
- .with(
- "every_n_minutes",
- () =>
- // Converting to lowercase here, because 'minute` is used without pluralization on the backend,
- // and it's impossible to have both pluralized and single form for the same string.
- c(
- "{0} is a verb like 'Check', {1} is an adverb like 'by the minute', {2} is a number of minutes.",
- )
- // Unjustified type cast. FIXME
- .jt`${verb} ${selectFrequency} every ${selectEveryMinute} ${ngettext(msgid`Minute`, "Minutes", schedule_minute as number).toLocaleLowerCase()}`,
- )
+ .with("every_n_minutes", () => {
+ // "Minute" is registered as a plural msgid elsewhere; give this row's
+ // singular its own context so it's a distinct key (no extraction clash).
+ const minuteUnit = (
+ schedule_minute === 1
+ ? c("Time unit in the schedule builder, e.g. 'every 1 minute'")
+ .t`Minute`
+ : c(
+ "Plural time unit in the schedule builder, e.g. 'every 10 minutes'",
+ ).t`Minutes`
+ ).toLocaleLowerCase();
+ return c(
+ "{0} is a verb like 'Check', {1} is an adverb like 'by the minute', {2} is a number of minutes.",
+ )
+ .jt`${verb} ${selectFrequency} every ${selectEveryMinute} ${minuteUnit}`;
+ })
.with("hourly", () => {
return minutesOnHourPicker
? // For example, "Send hourly at 15 minutes past the hour"
diff --git a/frontend/src/metabase/common/components/Schedule/Schedule.unit.spec.tsx b/frontend/src/metabase/common/components/Schedule/Schedule.unit.spec.tsx
index 199bfee42ed..62bcc4ddc6c 100644
--- a/frontend/src/metabase/common/components/Schedule/Schedule.unit.spec.tsx
+++ b/frontend/src/metabase/common/components/Schedule/Schedule.unit.spec.tsx
@@ -7,6 +7,7 @@ import {
setup,
setupHarness,
} from "metabase/common/components/Schedule/test-utils";
+import { setLocalization } from "metabase/utils/i18n";
const getInputValues = () => {
const inputs = screen.getAllByRole("textbox");
@@ -432,3 +433,49 @@ describe("Schedule", () => {
);
});
});
+
+describe("Schedule i18n (metabase#77265)", () => {
+ // Mirrors the real hu.po: "Minute" is only a {0}-carrying plural entry, plus a
+ // clean standalone "Minutes". A bare ngettext plural here would render "{0} perc".
+ const HU_LOCALE = {
+ headers: {
+ language: "hu",
+ "plural-forms": "nplurals=2; plural=(n != 1);",
+ },
+ translations: {
+ "": {
+ Minute: {
+ msgid_plural: "{0} Minutes",
+ msgstr: ["Perc", "{0} perc"],
+ },
+ Minutes: {
+ msgstr: ["Percek"],
+ },
+ },
+ },
+ };
+
+ afterEach(() => {
+ setLocalization({
+ headers: {
+ language: "en",
+ "plural-forms": "nplurals=2; plural=(n != 1);",
+ },
+ translations: { "": {} },
+ });
+ });
+
+ it("renders the plural unit without leaking a {0} placeholder", () => {
+ setLocalization(HU_LOCALE);
+ const { container } = setup({ cronString: "0 0/5 * * * ? *" });
+ expect(container).not.toHaveTextContent(/\{\s*0\s*\}/);
+ expect(screen.getByText("percek")).toBeInTheDocument();
+ });
+
+ it("renders the singular unit without leaking a {0} placeholder", () => {
+ setLocalization(HU_LOCALE);
+ const { container } = setup({ cronString: "0 0/1 * * * ? *" });
+ expect(container).not.toHaveTextContent(/\{\s*0\s*\}/);
+ expect(screen.getByText("perc")).toBeInTheDocument();
+ });
+});