Recipe Book0.6.0

4.11React reference

Label packs

Reference for the label packs in @recipe-book/react/labels, which write metric units and most of the parts' controls in German, French, Dutch, Swedish, Danish, Norwegian, Italian, or Spanish.

§ 4.117 min readView as Markdown

Use these exports from @recipe-book/react/labels to show a recipe in another language. Import your language and pass it as labels to a <Recipe.Provider>, with a locale for its numbers. A pack holds the language, units, durations, temperature, and size of core’s pack of the same name from @recipe-book/core/labels, plus the text of the parts’ controls. It leaves out the terms of <Recipe.Times> and <Recipe.Nutrition> and the text of <Recipe.CookMode>, which stay in English unless you set them. No shipped pack labels the US units, such as cup and oz, because each language’s own word for them names another size, so they keep their English labels. handful, jar, stalk, and bag stay English too, since the shipped packs don’t label them. Each pack is frozen, and core’s labelPack made it, so core’s formatting functions take it too in client code. A Server Component formats with core’s pack instead, as Imports on core’s pages shows.

app/recipes/layout.tsx
import * as Recipe from '@recipe-book/react'
import { de } from '@recipe-book/react/labels'
import type { ReactNode } from 'react'

export default function RecipesLayout({ children }: { children: ReactNode }) {
  return (
    <Recipe.Provider locale="de-DE" labels={de}>
      {children}
    </Recipe.Provider>
  )
}

Every <Recipe.Root> inside then writes “2 EL” for 2 tbsp, names the servings select “Portionen”, and names the toggle “Maßsystem”. Set lang on <html> to the same language, so screen readers read the page in it.

The layout is a Server Component, and @recipe-book/react/labels is a client module, so de reaches the provider as a client reference, with its words. A pack you make can’t cross that way: a pack holds its words under a symbol key, and React drops symbol keys from a Server Component’s props. A copy that arrives without the key makes the Root or provider throw invalid_argument, in every build, rather than write English. Make your own pack in a 'use client' module, as in Change one word.

Any serialization drops the key too, JSON included, so a pack can’t go through a React Router loader’s data or an Astro island’s props: the page renders on the server, then the Root or provider throws in the browser. Send the language code instead, and map it to a pack you import in the component that gets it, such as with { de, fr }[language].

All eight packs ship in one module, but each is one call marked pure, so a bundler keeps only the packs you import: import { de } adds about 1.1 kB gzipped to a page that renders ingredients or steps, core’s de included.

Change one word

To change one unit, spread a pack into labelPack with that unit changed, and pass the new pack as labels to a Root or an inner provider. The spread alone, as in labels={{ ...de }}, isn’t a pack, and TypeScript rejects it: it loses the words, and the Root or provider throws invalid_argument. A pack’s words replace the outer pack’s whole, so spread de.units too, and every other German word stays. Make the pack at module scope, in a 'use client' module, so it keeps its identity between renders and keeps its words on the way to the Root.

TSX
'use client'

import * as Recipe from '@recipe-book/react'
import type { Recipe as ParsedRecipe } from '@recipe-book/react/core'
import { de, labelPack } from '@recipe-book/react/labels'

const tbsp = {
  short: { one: 'Essl.', other: 'Essl.' },
  long: { one: 'Esslöffel', other: 'Esslöffel' },
}

const spoons = labelPack({
  ...de,
  units: { ...de.units, tbsp, 'tbsp-au': tbsp },
})

export function RecipeCard({ recipe }: { recipe: ParsedRecipe }) {
  return (
    <Recipe.Root recipe={recipe} labels={spoons}>
      <Recipe.Ingredients />
    </Recipe.Root>
  )
}

Under the provider above, RecipeCard writes “2 Essl.” for 2 tbsp, also in a recipe read with units: { tbsp: 'tbsp-au' } unless it’s in 'us', where 2 of them show as “8 TL”, and still “1 TL” for 1 tsp. Every shipped pack labels tbsp-au as tbsp in a separate entry, so change both. No shipped pack labels cup or cup-metric, so a pack that labels one leaves the other in English. RecipeLabelOverrides describes the merge.

Imports on core’s pages

Core’s pages import from @recipe-book/core. In a React app, which installs only @recipe-book/react, @recipe-book/core becomes @recipe-book/react/core, and @recipe-book/core/schema-org becomes @recipe-book/react/core/schema-org, and @recipe-book/core/labels becomes @recipe-book/react/core/labels. None of them is a client module, so a Server Component calls them, as with formatQuantity here:

TypeScript
import { formatQuantity } from '@recipe-book/react/core'
import { de } from '@recipe-book/react/core/labels'

const text = formatQuantity(
  { amount: 2, unit: 'tbsp' },
  { locale: 'de-DE', labels: de },
)

In a Server Component, a pack from @recipe-book/react/labels is a client reference, which a formatting function can’t use. On Next.js 15 and later, the formatting function throws invalid_argument. On Next.js 14, Next.js itself throws first: “Cannot read Symbol exports. Only named exports are supported on a client module imported on the server.” Import core’s pack from @recipe-book/react/core/labels there instead, as Imports on core’s pages shows.

labelPack

TypeScript
const labelPack: (
  labels: RecipeLabelOverrides,
) => RecipeLabelPack

Core’s labelPack, typed for labels on a Root or a provider, which take only a pack. It takes the controls’ text as well as core’s LabelWords, and checks the words as core’s does. Core’s formatting functions take its pack too, as in Write your own pack. Call it in client code; to make a pack in a Server Component, which can only format, call core’s from @recipe-book/react/core/labels. In client code, a Root or a provider takes core’s packs too, its languages’ and the ones its labelPack makes, with no error or warning, but the controls get no text from them: each key keeps the outer provider’s value, then English. With locale="de-DE" and no outer provider that sets the controls, <Recipe.ServingsSelect> and <Recipe.SystemToggle> read “2 servings … Metric US” under core’s de, where this module’s de gives “2 Portionen … Metrisch USA”. For translated controls, use the pack from @recipe-book/react/labels. A pack’s type is RecipeLabelPack.

de

TypeScript
const de: RecipeLabelPack

German: core’s de, plus this text. Pass it with a German locale, such as de-DE.

Label Text
servings Portionen
servingUnit Portion / Portionen
system Maßsystem
metric Metrisch
us USA
fan Umluft
optional (optional)
to bis

fr

TypeScript
const fr: RecipeLabelPack

French: core’s fr, plus this text. Pass it with a French locale, such as fr-FR.

Label Text
servings Portions
servingUnit portion / portions
system Système de mesure
metric Métrique
us US
fan chaleur tournante
optional (facultatif)
to à

nl

TypeScript
const nl: RecipeLabelPack

Dutch: core’s nl, plus this text. Pass it with a Dutch locale, such as nl-NL.

Label Text
servings Porties
servingUnit portie / porties
system Maatsysteem
metric Metriek
us VS
fan hetelucht
optional (optioneel)
to tot

sv

TypeScript
const sv: RecipeLabelPack

Swedish: core’s sv, plus this text. Pass it with a Swedish locale, such as sv-SE.

Label Text
servings Portioner
servingUnit portion / portioner
system Enhetssystem
metric Metriskt
us USA
fan varmluft
optional (valfritt)
to till

da

TypeScript
const da: RecipeLabelPack

Danish: core’s da, plus this text. Pass it with a Danish locale, such as da-DK.

Label Text
servings Portioner
servingUnit portion / portioner
system Målesystem
metric Metrisk
us USA
fan varmluft
optional (valgfrit)
to til

nb

TypeScript
const nb: RecipeLabelPack

Norwegian Bokmål: core’s nb, plus this text. Pass it with a Norwegian Bokmål locale, such as nb-NO.

Label Text
servings Porsjoner
servingUnit porsjon / porsjoner
system Målesystem
metric Metrisk
us USA
fan varmluft
optional (valgfritt)
to til

it

TypeScript
const it: RecipeLabelPack

Italian: core’s it, plus this text. Pass it with an Italian locale, such as it-IT.

Label Text
servings Porzioni
servingUnit porzione / porzioni
system Sistema di misurazione
metric Metrico
us USA
fan ventilato
optional (facoltativo)
to a

es

TypeScript
const es: RecipeLabelPack

Spanish: core’s es, plus this text. Pass it with a Spanish locale, such as es-ES.

Label Text
servings Raciones
servingUnit ración / raciones
system Sistema de medición
metric Métrico
us EE. UU.
fan con ventilador
optional (opcional)
to a
Contents