From a2c4c969fb67a1481b5ba62b5ef62aec7b3fbbe5 Mon Sep 17 00:00:00 2001 From: Helene Lotsberg Date: Sun, 22 Nov 2026 19:33:30 +0100 Subject: [PATCH] translate watering, sunlight and care values on the plant cards --- README.md | 2 +- docs/architecture.md | 2 +- docs/tests.md | 2 +- e2e/uiExplorePlants.spec.ts | 3 +- .../src/components/plantCard/PlantCard.tsx | 17 ++++++-- .../src/components/plantCard/plantTraits.ts | 41 +++++++++++++++++++ frontend/src/i18n/locales/en/translation.json | 20 ++++++++- frontend/src/i18n/locales/no/translation.json | 20 ++++++++- frontend/src/tests/PlantCard.test.tsx | 38 ++++++++++++++++- 9 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 frontend/src/components/plantCard/plantTraits.ts diff --git a/README.md b/README.md index f3fbda33..0d2f96aa 100644 --- a/README.md +++ b/README.md @@ -147,4 +147,4 @@ See [quality.md](docs/quality.md) for work practices, accessibility, sustainable ## Known issues -- The plant information on the plant cards is not translated to Norwegian. +- The plant descriptions and names on the plant cards are not translated to Norwegian, since they come from the English Perenual API. The watering, sunlight and care values are translated. diff --git a/docs/architecture.md b/docs/architecture.md index 636749c5..d6571ab8 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -58,7 +58,7 @@ The frontend is built with React and TypeScript (Vite). - **The steps are data:** `walkthroughSteps.ts` lists the steps in order. Each step has an `id`, a picture (one of the avatars) and, if it is about a page, the `route` of that page. The texts are in the translation files under `walkthrough.steps.`. A step can also have a `mobileHint`, a sentence that is only shown on small screens, such as how to open the menu with ☰, since the sidebar is always visible on large screens. It is hidden with CSS at the same breakpoint as the sidebar. A new step therefore only needs one line in the list and a title and text in each language. `stepForPath()` finds the step for the current page. - **`App.tsx` decides when it is open:** The walkthrough opens by itself when `walkthroughSeen` is not in `localStorage`, and the key is set when it is closed, so it is only shown once. The ? button in the `Header` opens it again at the step for the current page (`stepForPath`). When it closes, focus goes back to the ? button, except when the "Go to" link has moved the user to another page, where focus moves to the start of the new page like for any other navigation. - **`Walkthrough.tsx` shows one step at a time:** It uses the native `` element with `showModal()`, so the browser grays out the page behind, keeps keyboard focus inside the dialog and closes it with Escape. It can also be closed with ×, Finish or a click outside the box. The user moves between steps with Back and Next or the arrow keys, and steps about a page have a "Go to" link that opens the page and closes the walkthrough. The `LanguageSwitcher` from the sidebar is reused at the bottom of the dialog, so the language can be changed without leaving the walkthrough. This matters on mobile, where the sidebar is hidden behind the menu button. -- **Internationalisation:** Text is stored in translation files for Norwegian and English (i18next). +- **Internationalisation:** Text is stored in translation files for Norwegian and English (i18next). The watering, sunlight and care values on the plant cards come from Perenual in English, so `plantTraits.ts` maps each known value to a translation key. Values it does not know are shown as they are, and the plant names and descriptions stay in English. - **Shared types:** `Plant`, `User` and `Preference` are defined in `types/`. ### Communication with the backend diff --git a/docs/tests.md b/docs/tests.md index 4f340b14..1da5243c 100644 --- a/docs/tests.md +++ b/docs/tests.md @@ -147,7 +147,7 @@ The tests are in `frontend/src/tests`. Snapshot tests are only used for small co - `NotFoundPage.test.tsx` - An unknown path shows the 404 page, with a link back to Plant Match. - `PlantCard.test.tsx` - - The back of the card shows the whole plant after clicking information, missing facts and images are shown as unknown, and watering, sunlight and care level are turned into the right preference chips. + - The back of the card shows the whole plant after clicking information, missing facts and images are shown as unknown, watering, sunlight and care level are turned into the right preference chips, and the values on the back are translated in any casing (with "Moderate" as medium), while unknown values are shown as they are. - `PlantMatchPage.test.tsx` - The instructions and the first plant, going to the next plant with like and dislike and the empty card at the end, that a liked plant is saved as a match and a skipped one is not, the loading and error states, that preferences saved on the profile are highlighted on the card and also said in text for screen readers, and that the info button is described by the instructions, which Escape hides. - Loading in batches: the first batch is fetched with the batch size and no excluded plants, the next batch is only fetched when few cards are left and leaves out the plants already shown, every plant is shown exactly once, no more requests are made after a short batch, and a second request is not started while one is in progress. diff --git a/e2e/uiExplorePlants.spec.ts b/e2e/uiExplorePlants.spec.ts index 8a635f57..1eb34b61 100644 --- a/e2e/uiExplorePlants.spec.ts +++ b/e2e/uiExplorePlants.spec.ts @@ -63,7 +63,8 @@ test("user pages, filters, searches, opens a plant, sorts and resets", async ({ await expect(shownSide).not.toContainText("Crassula ovata"); await jadeCard.getByRole("button", { name: names.tapForDetails }).click(); await expect(shownSide).toContainText("Crassula ovata"); - await expect(shownSide).toContainText("Low"); + // Jade Plant has the care level "Low", translated to easy care + await expect(shownSide).toContainText(/Lettstelt|Easy/); await expect(shownSide).toContainText( "A succulent with round, glossy leaves.", ); diff --git a/frontend/src/components/plantCard/PlantCard.tsx b/frontend/src/components/plantCard/PlantCard.tsx index b39feaba..9f7cf969 100644 --- a/frontend/src/components/plantCard/PlantCard.tsx +++ b/frontend/src/components/plantCard/PlantCard.tsx @@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next"; import "./PlantCard.css"; import PlantPreferenceChips from "../plantPreferenceChips/PlantPreferenceChips"; import type { Plant } from "../../types/Plant"; +import { traitLabel } from "./plantTraits"; type PlantCardData = Omit; @@ -133,21 +134,31 @@ export default function PlantCard({
{t("plantCard.watering")}
-
{watering ?? t("plantCard.unknown")}
+
+ {watering + ? traitLabel(t, "watering", watering) + : t("plantCard.unknown")} +
{t("plantCard.sunlight")}
{sunlight && sunlight.length > 0 - ? sunlight.join(", ") + ? sunlight + .map((value) => traitLabel(t, "sunlight", value)) + .join(", ") : t("plantCard.unknown")}
{t("plantCard.careLevel")}
-
{careLevel ?? t("plantCard.unknown")}
+
+ {careLevel + ? traitLabel(t, "careLevel", careLevel) + : t("plantCard.unknown")} +
diff --git a/frontend/src/components/plantCard/plantTraits.ts b/frontend/src/components/plantCard/plantTraits.ts new file mode 100644 index 00000000..0358a6a4 --- /dev/null +++ b/frontend/src/components/plantCard/plantTraits.ts @@ -0,0 +1,41 @@ +// The watering, sunlight and care values come from Perenual in English, and +// are not consistent: the casing varies, and "Medium" and "Moderate" (and "Low" +// and "Easy") mean the same. Each known value is mapped to a fixed key, which +// is used to find its translation (plantCard.values.* in translation.json). +// The same rules are used for the preferences in PREFERENCE_MATCHERS in +// backend/src/queryBuilders.ts. +const TRAIT_KEYS = { + watering: { + minimum: "minimum", + average: "average", + frequent: "frequent", + }, + sunlight: { + "full sun": "fullSun", + "part shade": "partShade", + "filtered shade": "filteredShade", + "part sun/part shade": "partSunPartShade", + }, + careLevel: { + low: "low", + easy: "low", + medium: "medium", + moderate: "medium", + high: "high", + }, +} as const satisfies Record>; + +export type Trait = keyof typeof TRAIT_KEYS; + +// The translated text for a value, e.g. "Minimum" becomes "Sjeldent" in +// Norwegian. A value that is not known is shown as it is, in English, so +// nothing is lost if the data has a value we have not seen. +export function traitLabel( + t: (key: string) => string, + trait: Trait, + value: string, +): string { + const keys: Record = TRAIT_KEYS[trait]; + const key = keys[value.trim().toLowerCase()]; + return key ? t(`plantCard.values.${trait}.${key}`) : value; +} diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index 83f5aced..48bbd7b4 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -125,7 +125,25 @@ "careLevel": "Care level", "unknown": "Unknown", "tapForDetails": "Tap the corner of the card for details", - "tapToFlipBack": "Tap to flip back" + "tapToFlipBack": "Tap to flip back", + "values": { + "watering": { + "minimum": "Low", + "average": "Average", + "frequent": "Frequent" + }, + "sunlight": { + "fullSun": "Full sun", + "partShade": "Part shade", + "filteredShade": "Filtered shade", + "partSunPartShade": "Part sun/part shade" + }, + "careLevel": { + "low": "Easy", + "medium": "Medium", + "high": "High" + } + } }, "plantQuiz": { "title": "What kind of plant owner are you?", diff --git a/frontend/src/i18n/locales/no/translation.json b/frontend/src/i18n/locales/no/translation.json index 9135b43e..478c3b17 100644 --- a/frontend/src/i18n/locales/no/translation.json +++ b/frontend/src/i18n/locales/no/translation.json @@ -125,7 +125,25 @@ "careLevel": "Stell", "unknown": "Ukjent", "tapForDetails": "Trykk på hjørnet for detaljer om planten", - "tapToFlipBack": "Trykk for å snu tilbake" + "tapToFlipBack": "Trykk for å snu tilbake", + "values": { + "watering": { + "minimum": "Sjeldent", + "average": "Jevnlig", + "frequent": "Ofte" + }, + "sunlight": { + "fullSun": "Full sol", + "partShade": "Delvis skygge", + "filteredShade": "Filtrert skygge", + "partSunPartShade": "Delvis sol/delvis skygge" + }, + "careLevel": { + "low": "Lettstelt", + "medium": "Middels", + "high": "Høy" + } + } }, "plantQuiz": { "title": "Hvilken type planteeier er du?", diff --git a/frontend/src/tests/PlantCard.test.tsx b/frontend/src/tests/PlantCard.test.tsx index 994794cd..0cddc283 100644 --- a/frontend/src/tests/PlantCard.test.tsx +++ b/frontend/src/tests/PlantCard.test.tsx @@ -64,7 +64,18 @@ describe("PlantCard", () => { expect(container.firstChild).toHaveClass("is-flipped"); expect(screen.getByText("Orchidaceae, Phalaenopsis")).toBeInTheDocument(); expect(screen.getByText("A flowering plant")).toBeInTheDocument(); - expect(screen.getByText("Full sun, Part shade")).toBeInTheDocument(); + // The care values are translated (the mock returns the translation keys) + expect( + screen.getByText("plantCard.values.watering.average"), + ).toBeInTheDocument(); + expect( + screen.getByText( + "plantCard.values.sunlight.fullSun, plantCard.values.sunlight.partShade", + ), + ).toBeInTheDocument(); + expect( + screen.getByText("plantCard.values.careLevel.low"), + ).toBeInTheDocument(); await user.click( screen.getByRole("button", { name: "plantCard.tapToFlipBack" }), @@ -98,6 +109,31 @@ describe("PlantCard", () => { ).toHaveFocus(); }); + it("translates values in any casing, treats Moderate as medium, and keeps unknown values", () => { + render( + , + ); + + expect( + screen.getByText("plantCard.values.watering.minimum"), + ).toBeInTheDocument(); + // "deep shade" has no translation, so it is shown as it is + expect( + screen.getByText( + "plantCard.values.sunlight.partSunPartShade, deep shade", + ), + ).toBeInTheDocument(); + expect( + screen.getByText("plantCard.values.careLevel.medium"), + ).toBeInTheDocument(); + }); + it("shows unknown for missing facts and no image", () => { const { container } = render( ,