Skip to content

Commit c0bf2d0

Browse files
committed
Improve Eigelb pluralization, add weiße Zuckerschrift plural.
1 parent 8349bbf commit c0bf2d0

2 files changed

Lines changed: 65 additions & 13 deletions

File tree

nextjs-page/app/recipes/[[...slug]]/Recipe.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,16 @@ export function RecipeView({
164164
})}
165165
{ingredient.units && `\u202f${ingredient.units}`}
166166
</td>
167-
<td>{ingredient.name}</td>
167+
<td>
168+
{ingredient.units
169+
? ingredient.name
170+
: pluralize(
171+
ingredient.name,
172+
typeof ingredient.quantity === "number"
173+
? ingredient.quantity * multiplier
174+
: ingredient.quantity
175+
)}
176+
</td>
168177
</tr>
169178
))}
170179
</tbody>
@@ -219,7 +228,14 @@ export function RecipeView({
219228
maximumFractionDigits: 3,
220229
}
221230
)}
222-
{token.units && `\u202f${token.units}`} {token.name}
231+
{token.units
232+
? `\u202f${token.units} ${token.name}`
233+
: ` ${pluralize(
234+
token.name,
235+
typeof token.quantity === "number"
236+
? token.quantity * multiplier
237+
: null
238+
)}`}
223239
</span>
224240
)
225241
)}
@@ -237,7 +253,11 @@ export function RecipeView({
237253
case "ingredient":
238254
return (
239255
<React.Fragment key={tokenIndex}>
240-
{token.name}
256+
{typeof token.quantity === "number"
257+
? pluralize(token.name, token.quantity * multiplier, {
258+
withoutNumber: true,
259+
})
260+
: token.name}
241261
</React.Fragment>
242262
);
243263
case "cookware":

nextjs-page/german.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const words: Record<string, { plural: string; variants?: string[] }> = {
1+
const words: Record<
2+
string,
3+
{ plural: string; pluralWithoutNumber?: string; variants?: string[] }
4+
> = {
25
Ei: {
36
plural: "Eier",
47
},
@@ -12,23 +15,52 @@ const words: Record<string, { plural: string; variants?: string[] }> = {
1215
Schüssel: {
1316
plural: "Schüsseln",
1417
},
18+
"Weiße Zuckerschrift": {
19+
plural: "Weiße Zuckerschrift",
20+
variants: ["weißer Zuckerschrift"],
21+
},
22+
Eigelb: {
23+
plural: "Eigelb",
24+
pluralWithoutNumber: "Eigelbe",
25+
},
1526
};
1627

17-
export function pluralize(word: string, count: number | string) {
28+
function normalize(word: string) {
29+
return (
30+
Object.entries(words).find(
31+
([, entry]) =>
32+
entry.plural === word ||
33+
entry.variants?.some(
34+
(variant) => variant.toLowerCase() === word.toLowerCase()
35+
)
36+
)?.[0] ?? word
37+
);
38+
}
39+
40+
export function pluralize(
41+
word: string,
42+
count: number | string,
43+
options: { withoutNumber?: boolean } = {}
44+
) {
45+
word = normalize(word);
1846
if (typeof count !== "number") {
1947
return word;
2048
}
2149
if (count === 1) {
2250
return word;
2351
}
24-
return getPlural(word);
52+
return getPlural(word, { withoutNumber: options.withoutNumber });
2553
}
2654

27-
export function getPlural(word: string) {
28-
return (
29-
words[word]?.plural ??
30-
Object.values(words).find((entry) => entry.variants?.includes(word))
31-
?.plural ??
32-
word
33-
);
55+
export function getPlural(
56+
word: string,
57+
options: { withoutNumber?: boolean } = {}
58+
) {
59+
const match = words[normalize(word)];
60+
if (!match) {
61+
return word;
62+
}
63+
return options.withoutNumber
64+
? match.pluralWithoutNumber ?? match.plural
65+
: match.plural;
3466
}

0 commit comments

Comments
 (0)