Skip to content

Commit 7918973

Browse files
authored
add Swedish locale (#4451)
* add Swedish locale * fix suggestions from CodeRabbit * fix suggestions from CodeRabbit * add Swedish as supported locale
1 parent 8e827b1 commit 7918973

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

packages/docs/content/error-customization.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ The following locales are available:
356356
- `pt` — Portuguese
357357
- `ru` — Russian
358358
- `sl` — Slovenian
359-
- `ta` — Tamil
359+
- `sv` — Swedish
360+
- `ta` — Tamil
360361
- `th` — Thai
361362
- `tr` — Türkçe
362363
- `ua` — Ukrainian

packages/zod/src/v4/locales/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export { default as pl } from "./pl.js";
2525
export { default as pt } from "./pt.js";
2626
export { default as ru } from "./ru.js";
2727
export { default as sl } from "./sl.js";
28+
export { default as sv } from "./sv.js";
2829
export { default as ta } from "./ta.js";
2930
export { default as th } from "./th.js";
3031
export { default as tr } from "./tr.js";

packages/zod/src/v4/locales/sv.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import type { $ZodStringFormats } from "../core/checks.js";
2+
import type * as errors from "../core/errors.js";
3+
import * as util from "../core/util.js";
4+
5+
const Sizable: Record<string, { unit: string; verb: string }> = {
6+
string: { unit: "tecken", verb: "att ha" },
7+
file: { unit: "bytes", verb: "att ha" },
8+
array: { unit: "objekt", verb: "att innehålla" },
9+
set: { unit: "objekt", verb: "att innehålla" },
10+
};
11+
12+
function getSizing(origin: string): { unit: string; verb: string } | null {
13+
return Sizable[origin] ?? null;
14+
}
15+
16+
export const parsedType = (data: any): string => {
17+
const t = typeof data;
18+
19+
switch (t) {
20+
case "number": {
21+
return Number.isNaN(data) ? "NaN" : "antal";
22+
}
23+
case "object": {
24+
if (Array.isArray(data)) {
25+
return "lista";
26+
}
27+
if (data === null) {
28+
return "null";
29+
}
30+
31+
if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
32+
return data.constructor.name;
33+
}
34+
}
35+
}
36+
return t;
37+
};
38+
39+
const Nouns: {
40+
[k in $ZodStringFormats | (string & {})]?: string;
41+
} = {
42+
regex: "reguljärt uttryck",
43+
email: "e-postadress",
44+
url: "URL",
45+
emoji: "emoji",
46+
uuid: "UUID",
47+
uuidv4: "UUIDv4",
48+
uuidv6: "UUIDv6",
49+
nanoid: "nanoid",
50+
guid: "GUID",
51+
cuid: "cuid",
52+
cuid2: "cuid2",
53+
ulid: "ULID",
54+
xid: "XID",
55+
ksuid: "KSUID",
56+
datetime: "ISO-datum och tid",
57+
date: "ISO-datum",
58+
time: "ISO-tid",
59+
duration: "ISO-varaktighet",
60+
ipv4: "IPv4-intervall",
61+
ipv6: "IPv6-intervall",
62+
cidrv4: "IPv4-spektrum",
63+
cidrv6: "IPv6-spektrum",
64+
base64: "base64-kodad sträng",
65+
base64url: "base64url-kodad sträng",
66+
json_string: "JSON-sträng",
67+
e164: "E.164-nummer",
68+
jwt: "JWT",
69+
template_literal: "mall-literal",
70+
};
71+
72+
const error: errors.$ZodErrorMap = (issue) => {
73+
switch (issue.code) {
74+
case "invalid_type":
75+
return `Ogiltig inmatning: förväntat ${issue.expected}, fick ${parsedType(issue.input)}`;
76+
case "invalid_value":
77+
if (issue.values.length === 1) return `Ogiltig inmatning: förväntat ${util.stringifyPrimitive(issue.values[0])}`;
78+
return `Ogiltigt val: förväntade en av ${util.joinValues(issue.values, "|")}`;
79+
case "too_big": {
80+
const adj = issue.inclusive ? "<=" : "<";
81+
const sizing = getSizing(issue.origin);
82+
if (sizing) {
83+
return `För stor(t): förväntade ${issue.origin ?? "värdet"} att ha ${adj}${issue.maximum.toString()} ${sizing.unit ?? "element"}`;
84+
}
85+
return `För stor(t): förväntat ${issue.origin ?? "värdet"} att ha ${adj}${issue.maximum.toString()}`;
86+
}
87+
case "too_small": {
88+
const adj = issue.inclusive ? ">=" : ">";
89+
const sizing = getSizing(issue.origin);
90+
if (sizing) {
91+
return `För lite(t): förväntade ${issue.origin ?? "värdet"} att ha ${adj}${issue.minimum.toString()} ${sizing.unit}`;
92+
}
93+
return `För lite(t): förväntade ${issue.origin ?? "värdet"} att ha ${adj}${issue.minimum.toString()}`;
94+
}
95+
case "invalid_format": {
96+
const _issue = issue as errors.$ZodStringFormatIssues;
97+
if (_issue.format === "starts_with") {
98+
return `Ogiltig sträng: måste börja med "${_issue.prefix}"`;
99+
}
100+
if (_issue.format === "ends_with") return `Ogiltig sträng: måste sluta med "${_issue.suffix}"`;
101+
if (_issue.format === "includes") return `Ogiltig sträng: måste innehålla "${_issue.includes}"`;
102+
if (_issue.format === "regex") return `Ogiltig sträng: måste matcha mönstret "${_issue.pattern}"`;
103+
return `Ogiltig(t) ${Nouns[_issue.format] ?? issue.format}`;
104+
}
105+
case "not_multiple_of":
106+
return `Ogiltigt tal: måste vara en multipel av ${issue.divisor}`;
107+
case "unrecognized_keys":
108+
return `${issue.keys.length > 1 ? "Okända nycklar" : "Okänd nyckel"}: ${util.joinValues(issue.keys, ", ")}`;
109+
case "invalid_key":
110+
return `Ogiltig nyckel i ${issue.origin ?? "värdet"}`;
111+
case "invalid_union":
112+
return "Ogiltig input";
113+
case "invalid_element":
114+
return `Ogiltigt värde i ${issue.origin ?? "värdet"}`;
115+
default:
116+
return `Ogiltig input`;
117+
}
118+
};
119+
120+
export { error };
121+
122+
export default function (): { localeError: errors.$ZodErrorMap } {
123+
return {
124+
localeError: error,
125+
};
126+
}

0 commit comments

Comments
 (0)