Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function ConsumptionSummary({ userId }: { userId: string }) {
<ul className="mb-3 space-y-1 text-sm text-slate-200">
{blocks.detail.map((d, idx) => (
<li key={`${d.item}-${idx}`}>
• {d.item} · {d.quantity} {displayUnit(d.unit, t)}
• {d.item} · {d.quantity} {displayUnit(d.unit, t, d.quantity)}
</li>
))}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/components/features/event/ExpenseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export function ExpenseForm({
{listItems.map((p) => {
const checked = p.id in links
const remaining = Math.max(1, p.totalQuantity - boughtByOthers(p.id))
const unit = displayUnit(p.unit, t)
const unit = displayUnit(p.unit, t, p.totalQuantity)
const assignee = p.assignedTo
? (event.users.find((u) => u.id === p.assignedTo) ?? null)
: null
Expand Down
4 changes: 2 additions & 2 deletions src/presentation/components/features/event/PurchasesTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export function PurchasesTab() {
<div className="text-sm text-slate-400">
{t(p.kind === 'bring' ? 'purchases.totalToBring' : 'purchases.totalQuantity', {
n: Math.round(p.totalQuantity * 100) / 100,
unit: displayUnit(p.unit, t),
unit: displayUnit(p.unit, t, p.totalQuantity),
})}
</div>
<div className="text-xs text-slate-500">
Expand Down Expand Up @@ -384,7 +384,7 @@ export function PurchasesTab() {
{t('purchases.boughtProgress', {
n: round2(bought),
total: round2(total),
unit: displayUnit(p.unit, t),
unit: displayUnit(p.unit, t, total),
})}
</span>
</div>
Expand Down
14 changes: 14 additions & 0 deletions src/presentation/i18n/locales/ca/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@
"update": "Desa els canvis",
"units": {
"units": "unitats",
"units_one": "unitat",
"units_other": "unitats",
"bottles": "ampolles",
"bottles_one": "ampolla",
"bottles_other": "ampolles",
"cans": "llaunes",
"cans_one": "llauna",
"cans_other": "llaunes",
"kg": "kg",
"liters": "litres",
"liters_one": "litre",
"liters_other": "litres",
"grams": "grams",
"grams_one": "gram",
"grams_other": "grams",
"bag": "bosses",
"bag_one": "bossa",
"bag_other": "bosses",
"tray": "safates",
"tray_one": "safata",
"tray_other": "safates",
"single": "Unitat única"
},
"unitPlaceholder": "ex. ampolles, garrafa 8L",
Expand Down
14 changes: 14 additions & 0 deletions src/presentation/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@
"update": "Save changes",
"units": {
"units": "units",
"units_one": "unit",
"units_other": "units",
"bottles": "bottles",
"bottles_one": "bottle",
"bottles_other": "bottles",
"cans": "cans",
"cans_one": "can",
"cans_other": "cans",
"kg": "kg",
"liters": "liters",
"liters_one": "liter",
"liters_other": "liters",
"grams": "grams",
"grams_one": "gram",
"grams_other": "grams",
"bag": "bags",
"bag_one": "bag",
"bag_other": "bags",
"tray": "trays",
"tray_one": "tray",
"tray_other": "trays",
"single": "Single unit"
},
"unitPlaceholder": "e.g. bottles, garrafa 8L",
Expand Down
14 changes: 14 additions & 0 deletions src/presentation/i18n/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@
"update": "Guardar cambios",
"units": {
"units": "unidades",
"units_one": "unidad",
"units_other": "unidades",
"bottles": "botellas",
"bottles_one": "botella",
"bottles_other": "botellas",
"cans": "latas",
"cans_one": "lata",
"cans_other": "latas",
"kg": "kg",
"liters": "litros",
"liters_one": "litro",
"liters_other": "litros",
"grams": "gramos",
"grams_one": "gramo",
"grams_other": "gramos",
"bag": "bolsas",
"bag_one": "bolsa",
"bag_other": "bolsas",
"tray": "bandejas",
"tray_one": "bandeja",
"tray_other": "bandejas",
"single": "Unidad única"
},
"unitPlaceholder": "ej. botellas, garrafa 8L",
Expand Down
14 changes: 14 additions & 0 deletions src/presentation/i18n/locales/eu/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@
"update": "Aldaketak gorde",
"units": {
"units": "unitateak",
"units_one": "unitate",
"units_other": "unitateak",
"bottles": "botilak",
"bottles_one": "botila",
"bottles_other": "botilak",
"cans": "latak",
"cans_one": "lata",
"cans_other": "latak",
"kg": "kg",
"liters": "litroak",
"liters_one": "litro",
"liters_other": "litroak",
"grams": "gramoak",
"grams_one": "gramo",
"grams_other": "gramoak",
"bag": "poltsak",
"bag_one": "poltsa",
"bag_other": "poltsak",
"tray": "erretiluak",
"tray_one": "erretilu",
"tray_other": "erretiluak",
"single": "Unitate bakarra"
},
"unitPlaceholder": "adib. botilak, garrafa 8L",
Expand Down
14 changes: 14 additions & 0 deletions src/presentation/i18n/locales/gl/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@
"update": "Gardar cambios",
"units": {
"units": "unidades",
"units_one": "unidade",
"units_other": "unidades",
"bottles": "botellas",
"bottles_one": "botella",
"bottles_other": "botellas",
"cans": "latas",
"cans_one": "lata",
"cans_other": "latas",
"kg": "kg",
"liters": "litros",
"liters_one": "litro",
"liters_other": "litros",
"grams": "gramos",
"grams_one": "gramo",
"grams_other": "gramos",
"bag": "bolsas",
"bag_one": "bolsa",
"bag_other": "bolsas",
"tray": "bandexas",
"tray_one": "bandexa",
"tray_other": "bandexas",
"single": "Unidade única"
},
"unitPlaceholder": "ex. botellas, garrafa 8L",
Expand Down
14 changes: 14 additions & 0 deletions src/presentation/i18n/locales/va/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,27 @@
"update": "Guarda els canvis",
"units": {
"units": "unitats",
"units_one": "unitat",
"units_other": "unitats",
"bottles": "botelles",
"bottles_one": "botella",
"bottles_other": "botelles",
"cans": "llandes",
"cans_one": "llanda",
"cans_other": "llandes",
"kg": "kg",
"liters": "litres",
"liters_one": "litre",
"liters_other": "litres",
"grams": "grams",
"grams_one": "gram",
"grams_other": "grams",
"bag": "bosses",
"bag_one": "bossa",
"bag_other": "bosses",
"tray": "safates",
"tray_one": "safata",
"tray_other": "safates",
"single": "Unitat única"
},
"unitPlaceholder": "ex. botelles, garrafa 8L",
Expand Down
4 changes: 2 additions & 2 deletions src/presentation/utils/formatShoppingListText.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { EventSnapshot } from '@/domain/entities/Event'
import type { PurchaseSnapshot } from '@/domain/entities/Purchase'
import { displayUnit } from '@/presentation/utils/units'
type T = (key: string, vars?: Record<string, string>) => string
type T = (key: string, vars?: Record<string, unknown>) => string

function formatQty(n: number): string {
return Number.isInteger(n) ? String(n) : String(Math.round(n * 10) / 10)
Expand Down Expand Up @@ -33,7 +33,7 @@ function renderBuyLine(event: EventSnapshot, p: PurchaseSnapshot, t: T): string
const checkmark = p.purchased ? ' ✅' : ''
const bought = boughtQtyFor(event, p.id)
const total = p.totalQuantity
const unit = displayUnit(p.unit, t)
const unit = displayUnit(p.unit, t, total)
return ` • ${name}${assigneePart} · ${formatQty(bought)}/${formatQty(total)} ${unit}${checkmark}`
}

Expand Down
15 changes: 12 additions & 3 deletions src/presentation/utils/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ export const SELECTABLE_UNITS = [...VALID_UNITS, SHARED_UNIT] as const

const KNOWN = new Set<string>(SELECTABLE_UNITS)

/** Translate a known unit key; fall back to the raw (free-text) unit otherwise. */
export function displayUnit(unit: string, t: (key: string) => string): string {
return KNOWN.has(unit) ? t(`purchases.form.units.${unit}`) : unit
type T = (key: string, opts?: Record<string, unknown>) => string

/**
* Translate a known unit key, picking singular/plural based on `count` via i18next's
* built-in plural support (_one / _other suffixes). Falls back to the raw (free-text)
* unit otherwise. When `count` is omitted, returns the base plural form (used for
* dropdown labels).
*/
export function displayUnit(unit: string, t: T, count?: number): string {
if (!KNOWN.has(unit)) return unit
const key = `purchases.form.units.${unit}`
return count === undefined ? t(key) : t(key, { count })
}
4 changes: 2 additions & 2 deletions tests/presentation/utils/formatShoppingListText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const STUB_TRANSLATIONS: Record<string, string> = {
'purchases.form.units.single': 'single',
}

const t = (key: string, vars?: Record<string, string>): string => {
const t = (key: string, vars?: Record<string, unknown>): string => {
let value = STUB_TRANSLATIONS[key] ?? key
if (vars) {
for (const [k, v] of Object.entries(vars)) {
value = value.replace(`{{${k}}}`, v)
value = value.replace(`{{${k}}}`, String(v))
}
}
return value
Expand Down