From 32da0780060fda153d58f4334a560269ec0b0b7f Mon Sep 17 00:00:00 2001 From: YiJie <51358815+NWYLZW@users.noreply.github.com> Date: Thu, 7 Dec 2023 08:34:51 +0800 Subject: [PATCH] feat: better chinese translate (zh-CN) * feat: better chinese translate * feat(zh-CN): update unit test and translate * fix(zh-CN): update datetime translate * feat(zh-CN): update date to small inclusive translate * feat(zh-CN): update `arguments` and `return_type` translate * feat(zh-CN): update `invalid_union` translate * fix(zh-CN): update unit test * chore: update .idea `gitignore` file * fix: update example local json --- .idea/.gitignore | 1 + .../public/locales/zh-CN/zod.json | 118 +++++++++-------- packages/core/locales/zh-CN/zod.json | 120 ++++++++++-------- .../core/tests/integrations/zh-CN.test.ts | 107 ++++++++-------- 4 files changed, 183 insertions(+), 163 deletions(-) diff --git a/.idea/.gitignore b/.idea/.gitignore index 13566b8..30d58b1 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -6,3 +6,4 @@ # Datasource local storage ignored files /dataSources/ /dataSources.local.xml +/inspectionProfiles/ diff --git a/examples/with-next-i18next/public/locales/zh-CN/zod.json b/examples/with-next-i18next/public/locales/zh-CN/zod.json index 36acef4..0006a4d 100644 --- a/examples/with-next-i18next/public/locales/zh-CN/zod.json +++ b/examples/with-next-i18next/public/locales/zh-CN/zod.json @@ -1,102 +1,112 @@ { "errors": { - "invalid_type": "期望输入的是{{expected}}, 而输入的是{{received}}", - "invalid_type_received_undefined": "必填项", - "invalid_literal": "无效的输入, 请输入 {{expected}}", - "unrecognized_keys": "无法识别对象的键: {{- keys}}", - "invalid_union": "输入格式错误", - "invalid_union_discriminator": "无效的标识符。请输入 {{- options}}", - "invalid_enum_value": "无效的 {{received}} 值。请输入 {{- options}}", - "invalid_arguments": "参数错误", - "invalid_return_type": "错误的返回值类型", - "invalid_date": "错误的日期", - "custom": "格式错误", - "invalid_intersection_types": "交集类型无法合并", - "not_multiple_of": "必须是 {{multipleOf}} 的倍数", - "not_finite": "不能为无限值", + "invalid_type": "预期输入为{{expected}},而输入为{{received}}", + "invalid_type_received_undefined": "必填", + "invalid_literal": "错误的字面量值,请输入 {{expected}}", + "unrecognized_keys": "对象中的键无法识别: {{- keys}}", + "invalid_union": "不满足联合类型中的选项", + "invalid_union_discriminator": "标识值无法被区分。请输入 {{- options}}", + "invalid_enum_value": "错误的枚举值 '{{received}}'。请输入 {{- options}}", + "invalid_arguments": "错误的函数参数格式", + "invalid_return_type": "错误的函数返回值格式", + "invalid_date": "错误的日期格式", + "custom": "错误的输入格式", + "invalid_intersection_types": "交叉类型结果无法被合并", + "not_multiple_of": "数值必须是 {{multipleOf}} 的倍数", + "not_finite": "数值必须有限", "invalid_string": { - "email": "{{validation}}格式错误", - "url": "{{validation}} 格式错误", - "uuid": "{{validation}} 格式错误", - "cuid": "{{validation}} 格式错误", - "regex": "格式错误", - "datetime": "{{validation}} 格式错误", - "startsWith": "必须以 {{startsWith}} 起始", - "endsWith": "必须以 {{endsWith}} 结尾" + "email": "错误的{{validation}}格式", + "url": "错误的{{validation}}格式", + "uuid": "错误的{{validation}}格式", + "cuid": "错误的{{validation}}格式", + "regex": "错误的格式", + "datetime": "错误的{{validation}}格式", + "startsWith": "文本必须以 \"{{startsWith}}\" 开头", + "endsWith": "文本必须以 \"{{endsWith}}\" 结尾" }, "too_small": { "array": { - "inclusive": "至少需要包含 {{minimum}} 个元素", - "not_inclusive": "必须包含多于 {{minimum}} 个元素" + "exact": "数组元素必须为 {{minimum}} 个", + "inclusive": "数组元素不得少于 {{minimum}} 个", + "not_inclusive": "数组元素必须超过 {{minimum}} 个" }, "string": { - "inclusive": "至少需要包含 {{minimum}} 个字符", - "not_inclusive": "必须包含多于 {{minimum}} 个字符" + "exact": "文本长度必须为 {{minimum}} 个字符", + "inclusive": "文本长度不得少于 {{minimum}} 个字符", + "not_inclusive": "文本长度必须超过 {{minimum}} 个字符" }, "number": { - "inclusive": "必须大于或等于 {{minimum}}", - "not_inclusive": "必须大于 {{minimum}}" + "exact": "数值必须为 {{minimum}}", + "inclusive": "数值不得小于 {{minimum}}", + "not_inclusive": "数值必须大于 {{minimum}}" }, "set": { - "inclusive": "无效输入", - "not_inclusive": "无效输入" + "exact": "错误的输入格式", + "inclusive": "错误的输入格式", + "not_inclusive": "错误的输入格式" }, "date": { - "inclusive": "日期必须晚于或等于 {{- minimum, datetime}}", - "not_inclusive": "日期必须晚于 {{- minimum, datetime}}" + "exact": "日期必须为 {{- minimum, datetime}}", + "inclusive": "日期不得晚于 {{- minimum, datetime}}", + "not_inclusive": "日期必须早于 {{- minimum, datetime}}" } }, "too_big": { "array": { - "inclusive": "最多只能包含 {{maximum}} 个元素", - "not_inclusive": "必须少于 {{maximum}} 个元素" + "exact": "数组元素必须为 {{maximum}} 个", + "inclusive": "数组元素不得多于 {{maximum}} 个", + "not_inclusive": "数组元素必须少于 {{maximum}} 个" }, "string": { - "inclusive": "最多只能包含 {{maximum}} 个字符", - "not_inclusive": "必须少于 {{maximum}} 个字符" + "exact": "文本长度必须为 {{maximum}} 个字符", + "inclusive": "文本长度不得多于 {{maximum}} 个字符", + "not_inclusive": "文本长度必须少于 {{maximum}} 个字符" }, "number": { - "inclusive": "必须小于或等于 {{maximum}}", - "not_inclusive": "必须小于 {{maximum}}" + "exact": "数值必须为 {{maximum}}", + "inclusive": "数值不得大于 {{maximum}}", + "not_inclusive": "数值必须小于 {{maximum}}" }, "set": { - "inclusive": "无效输入", - "not_inclusive": "无效输入" + "exact": "错误的输入格式", + "inclusive": "错误的输入格式", + "not_inclusive": "错误的输入格式" }, "date": { - "inclusive": "日期必须早于或等于 {{- maximum, datetime}}", - "not_inclusive": "日期必须早于 {{- maximum, datetime}}" + "exact": "日期必须为 {{- maximum, datetime}}", + "inclusive": "日期不得早于 {{- maximum, datetime}}", + "not_inclusive": "日期必须晚于 {{- maximum, datetime}}" } } }, "validations": { "email": "邮件", - "url": "url", + "url": "链接", "uuid": "uuid", "cuid": "cuid", "regex": "正则表达式", - "datetime": "datetime" + "datetime": "日期时间" }, "types": { "function": "函数", "number": "数字", "string": "字符串", - "nan": "NaN", + "nan": "非数", "integer": "整数", "float": "浮点数", "boolean": "布尔值", "date": "日期", - "bigint": "Bigint", - "undefined": "undefined", - "symbol": "Symbol", - "null": "null", + "bigint": "大整数", + "undefined": "未定义", + "symbol": "符号", + "null": "空对象", "array": "数组", "object": "对象", - "unknown": "unknown", + "unknown": "未知", "promise": "Promise", - "void": "void", - "never": "never", - "map": "Map", - "set": "Set" + "void": "空", + "never": "不存在", + "map": "字典", + "set": "集合" } } diff --git a/packages/core/locales/zh-CN/zod.json b/packages/core/locales/zh-CN/zod.json index 6391c09..0006a4d 100644 --- a/packages/core/locales/zh-CN/zod.json +++ b/packages/core/locales/zh-CN/zod.json @@ -1,104 +1,112 @@ { "errors": { - "invalid_type": "期望输入的是{{expected}}, 而输入的是{{received}}", - "invalid_type_received_undefined": "必填项", - "invalid_literal": "无效的输入, 请输入 {{expected}}", - "unrecognized_keys": "无法识别对象的键: {{- keys}}", - "invalid_union": "输入格式错误", - "invalid_union_discriminator": "无效的标识符。请输入 {{- options}}", - "invalid_enum_value": "无效的 '{{received}}' 值。请输入 {{- options}}", - "invalid_arguments": "参数错误", - "invalid_return_type": "错误的返回值类型", - "invalid_date": "错误的日期", - "custom": "格式错误", - "invalid_intersection_types": "交集类型无法合并", - "not_multiple_of": "必须是 {{multipleOf}} 的倍数", - "not_finite": "不能为无限值", + "invalid_type": "预期输入为{{expected}},而输入为{{received}}", + "invalid_type_received_undefined": "必填", + "invalid_literal": "错误的字面量值,请输入 {{expected}}", + "unrecognized_keys": "对象中的键无法识别: {{- keys}}", + "invalid_union": "不满足联合类型中的选项", + "invalid_union_discriminator": "标识值无法被区分。请输入 {{- options}}", + "invalid_enum_value": "错误的枚举值 '{{received}}'。请输入 {{- options}}", + "invalid_arguments": "错误的函数参数格式", + "invalid_return_type": "错误的函数返回值格式", + "invalid_date": "错误的日期格式", + "custom": "错误的输入格式", + "invalid_intersection_types": "交叉类型结果无法被合并", + "not_multiple_of": "数值必须是 {{multipleOf}} 的倍数", + "not_finite": "数值必须有限", "invalid_string": { - "email": "{{validation}}格式错误", - "url": "{{validation}} 格式错误", - "uuid": "{{validation}} 格式错误", - "cuid": "{{validation}} 格式错误", - "regex": "格式错误", - "datetime": "{{validation}} 格式错误", - "startsWith": "必须以 \"{{startsWith}}\" 起始", - "endsWith": "必须以 \"{{endsWith}}\" 结尾" + "email": "错误的{{validation}}格式", + "url": "错误的{{validation}}格式", + "uuid": "错误的{{validation}}格式", + "cuid": "错误的{{validation}}格式", + "regex": "错误的格式", + "datetime": "错误的{{validation}}格式", + "startsWith": "文本必须以 \"{{startsWith}}\" 开头", + "endsWith": "文本必须以 \"{{endsWith}}\" 结尾" }, "too_small": { "array": { - "inclusive": "至少需要包含 {{minimum}} 个元素", - "not_inclusive": "必须包含多于 {{minimum}} 个元素" + "exact": "数组元素必须为 {{minimum}} 个", + "inclusive": "数组元素不得少于 {{minimum}} 个", + "not_inclusive": "数组元素必须超过 {{minimum}} 个" }, "string": { - "inclusive": "至少需要包含 {{minimum}} 个字符", - "not_inclusive": "必须包含多于 {{minimum}} 个字符" + "exact": "文本长度必须为 {{minimum}} 个字符", + "inclusive": "文本长度不得少于 {{minimum}} 个字符", + "not_inclusive": "文本长度必须超过 {{minimum}} 个字符" }, "number": { - "inclusive": "必须大于或等于 {{minimum}}", - "not_inclusive": "必须大于 {{minimum}}" + "exact": "数值必须为 {{minimum}}", + "inclusive": "数值不得小于 {{minimum}}", + "not_inclusive": "数值必须大于 {{minimum}}" }, "set": { - "exact": "无效输入", - "inclusive": "无效输入", - "not_inclusive": "无效输入" + "exact": "错误的输入格式", + "inclusive": "错误的输入格式", + "not_inclusive": "错误的输入格式" }, "date": { - "inclusive": "日期必须晚于或等于 {{- minimum, datetime}}", - "not_inclusive": "日期必须晚于 {{- minimum, datetime}}" + "exact": "日期必须为 {{- minimum, datetime}}", + "inclusive": "日期不得晚于 {{- minimum, datetime}}", + "not_inclusive": "日期必须早于 {{- minimum, datetime}}" } }, "too_big": { "array": { - "inclusive": "最多只能包含 {{maximum}} 个元素", - "not_inclusive": "必须少于 {{maximum}} 个元素" + "exact": "数组元素必须为 {{maximum}} 个", + "inclusive": "数组元素不得多于 {{maximum}} 个", + "not_inclusive": "数组元素必须少于 {{maximum}} 个" }, "string": { - "inclusive": "最多只能包含 {{maximum}} 个字符", - "not_inclusive": "必须少于 {{maximum}} 个字符" + "exact": "文本长度必须为 {{maximum}} 个字符", + "inclusive": "文本长度不得多于 {{maximum}} 个字符", + "not_inclusive": "文本长度必须少于 {{maximum}} 个字符" }, "number": { - "inclusive": "必须小于或等于 {{maximum}}", - "not_inclusive": "必须小于 {{maximum}}" + "exact": "数值必须为 {{maximum}}", + "inclusive": "数值不得大于 {{maximum}}", + "not_inclusive": "数值必须小于 {{maximum}}" }, "set": { - "exact": "无效输入", - "inclusive": "无效输入", - "not_inclusive": "无效输入" + "exact": "错误的输入格式", + "inclusive": "错误的输入格式", + "not_inclusive": "错误的输入格式" }, "date": { - "inclusive": "日期必须早于或等于 {{- maximum, datetime}}", - "not_inclusive": "日期必须早于 {{- maximum, datetime}}" + "exact": "日期必须为 {{- maximum, datetime}}", + "inclusive": "日期不得早于 {{- maximum, datetime}}", + "not_inclusive": "日期必须晚于 {{- maximum, datetime}}" } } }, "validations": { "email": "邮件", - "url": "url", + "url": "链接", "uuid": "uuid", "cuid": "cuid", "regex": "正则表达式", - "datetime": "datetime" + "datetime": "日期时间" }, "types": { "function": "函数", "number": "数字", "string": "字符串", - "nan": "NaN", + "nan": "非数", "integer": "整数", "float": "浮点数", "boolean": "布尔值", "date": "日期", - "bigint": "Bigint", - "undefined": "undefined", - "symbol": "Symbol", - "null": "null", + "bigint": "大整数", + "undefined": "未定义", + "symbol": "符号", + "null": "空对象", "array": "数组", "object": "对象", - "unknown": "unknown", + "unknown": "未知", "promise": "Promise", - "void": "void", - "never": "never", - "map": "Map", - "set": "Set" + "void": "空", + "never": "不存在", + "map": "字典", + "set": "集合" } } diff --git a/packages/core/tests/integrations/zh-CN.test.ts b/packages/core/tests/integrations/zh-CN.test.ts index 23fd977..006e078 100644 --- a/packages/core/tests/integrations/zh-CN.test.ts +++ b/packages/core/tests/integrations/zh-CN.test.ts @@ -11,86 +11,88 @@ beforeAll(async () => { test("string parser error messages", () => { const schema = z.string(); - expect(getErrorMessage(schema.safeParse(undefined))).toEqual("必填项"); + expect(getErrorMessage(schema.safeParse(undefined))).toEqual("必填"); expect(getErrorMessage(schema.safeParse(1))).toEqual( - "期望输入的是字符串, 而输入的是数字" + "预期输入为字符串,而输入为数字" ); expect(getErrorMessage(schema.safeParse(true))).toEqual( - "期望输入的是字符串, 而输入的是布尔值" + "预期输入为字符串,而输入为布尔值" ); expect(getErrorMessage(schema.safeParse(Date))).toEqual( - "期望输入的是字符串, 而输入的是函数" + "预期输入为字符串,而输入为函数" ); expect(getErrorMessage(schema.safeParse(new Date()))).toEqual( - "期望输入的是字符串, 而输入的是日期" + "预期输入为字符串,而输入为日期" ); - expect(getErrorMessage(schema.email().safeParse(""))).toEqual("邮件格式错误"); - expect(getErrorMessage(schema.url().safeParse(""))).toEqual("url 格式错误"); + expect(getErrorMessage(schema.email().safeParse(""))).toEqual( + "错误的邮件格式" + ); + expect(getErrorMessage(schema.url().safeParse(""))).toEqual("错误的链接格式"); expect(getErrorMessage(schema.regex(/aaa/).safeParse(""))).toEqual( - "格式错误" + "错误的格式" ); expect(getErrorMessage(schema.startsWith("foo").safeParse(""))).toEqual( - '必须以 "foo" 起始' + '文本必须以 "foo" 开头' ); expect(getErrorMessage(schema.endsWith("bar").safeParse(""))).toEqual( - '必须以 "bar" 结尾' + '文本必须以 "bar" 结尾' ); expect(getErrorMessage(schema.min(5).safeParse("a"))).toEqual( - "至少需要包含 5 个字符" + "文本长度不得少于 5 个字符" ); expect(getErrorMessage(schema.max(5).safeParse("abcdef"))).toEqual( - "最多只能包含 5 个字符" + "文本长度不得多于 5 个字符" ); - // TODO: add `zod:errors.(too_small|too_big).string.exact` expect(getErrorMessage(schema.length(5).safeParse("abcdef"))).toEqual( - "String must contain exactly 5 character(s)" + "文本长度必须为 5 个字符" ); - // TODO: translation `datetime` (zod:validations.datetime) expect( getErrorMessage(schema.datetime().safeParse("2020-01-01T00:00:00+02:00")) - ).toEqual("datetime 格式错误"); + ).toEqual("错误的日期时间格式"); }); test("number parser error messages", () => { const schema = z.number(); - expect(getErrorMessage(schema.safeParse(undefined))).toEqual("必填项"); + expect(getErrorMessage(schema.safeParse(undefined))).toEqual("必填"); expect(getErrorMessage(schema.safeParse(""))).toEqual( - "期望输入的是数字, 而输入的是字符串" + "预期输入为数字,而输入为字符串" ); expect(getErrorMessage(schema.safeParse(null))).toEqual( - "期望输入的是数字, 而输入的是null" + "预期输入为数字,而输入为空对象" ); expect(getErrorMessage(schema.safeParse(NaN))).toEqual( - "期望输入的是数字, 而输入的是NaN" + "预期输入为数字,而输入为非数" ); expect(getErrorMessage(schema.int().safeParse(0.1))).toEqual( - "期望输入的是整数, 而输入的是浮点数" + "预期输入为整数,而输入为浮点数" ); expect(getErrorMessage(schema.multipleOf(5).safeParse(2))).toEqual( - "必须是 5 的倍数" + "数值必须是 5 的倍数" ); expect(getErrorMessage(schema.step(0.1).safeParse(0.0001))).toEqual( - "必须是 0.1 的倍数" + "数值必须是 0.1 的倍数" ); - expect(getErrorMessage(schema.lt(5).safeParse(10))).toEqual("必须小于 5"); + expect(getErrorMessage(schema.lt(5).safeParse(10))).toEqual("数值必须小于 5"); expect(getErrorMessage(schema.lte(5).safeParse(10))).toEqual( - "必须小于或等于 5" - ); - expect(getErrorMessage(schema.gt(5).safeParse(1))).toEqual("必须大于 5"); - expect(getErrorMessage(schema.gte(5).safeParse(1))).toEqual( - "必须大于或等于 5" + "数值不得大于 5" ); + expect(getErrorMessage(schema.gt(5).safeParse(1))).toEqual("数值必须大于 5"); + expect(getErrorMessage(schema.gte(5).safeParse(1))).toEqual("数值不得小于 5"); expect(getErrorMessage(schema.nonnegative().safeParse(-1))).toEqual( - "必须大于或等于 0" + "数值不得小于 0" ); expect(getErrorMessage(schema.nonpositive().safeParse(1))).toEqual( - "必须小于或等于 0" + "数值不得大于 0" + ); + expect(getErrorMessage(schema.negative().safeParse(1))).toEqual( + "数值必须小于 0" + ); + expect(getErrorMessage(schema.positive().safeParse(0))).toEqual( + "数值必须大于 0" ); - expect(getErrorMessage(schema.negative().safeParse(1))).toEqual("必须小于 0"); - expect(getErrorMessage(schema.positive().safeParse(0))).toEqual("必须大于 0"); expect(getErrorMessage(schema.finite().safeParse(Infinity))).toEqual( - "不能为无限值" + "数值必须有限" ); }); @@ -99,22 +101,22 @@ test("date parser error messages", async () => { const schema = z.date(); expect(getErrorMessage(schema.safeParse("2022-12-01"))).toEqual( - "期望输入的是日期, 而输入的是字符串" + "预期输入为日期,而输入为字符串" ); expect( getErrorMessage( schema.min(new Date("2022-08-01")).safeParse(new Date("2022-07-29")) ) - ).toEqual(`日期必须晚于或等于 ${testDate.toLocaleDateString(LOCALE)}`); + ).toEqual(`日期不得晚于 ${testDate.toLocaleDateString(LOCALE)}`); expect( getErrorMessage( schema.max(new Date("2022-08-01")).safeParse(new Date("2022-08-02")) ) - ).toEqual(`日期必须早于或等于 ${testDate.toLocaleDateString(LOCALE)}`); + ).toEqual(`日期不得早于 ${testDate.toLocaleDateString(LOCALE)}`); try { await schema.parseAsync(new Date("invalid")); } catch (err) { - expect((err as z.ZodError).issues[0].message).toEqual("错误的日期"); + expect((err as z.ZodError).issues[0].message).toEqual("错误的日期格式"); } }); @@ -122,20 +124,19 @@ test("array parser error messages", () => { const schema = z.string().array(); expect(getErrorMessage(schema.safeParse(""))).toEqual( - "期望输入的是数组, 而输入的是字符串" + "预期输入为数组,而输入为字符串" ); expect(getErrorMessage(schema.min(5).safeParse([""]))).toEqual( - "至少需要包含 5 个元素" + "数组元素不得少于 5 个" ); expect(getErrorMessage(schema.max(2).safeParse(["", "", ""]))).toEqual( - "最多只能包含 2 个元素" + "数组元素不得多于 2 个" ); expect(getErrorMessage(schema.nonempty().safeParse([]))).toEqual( - "至少需要包含 1 个元素" + "数组元素不得少于 1 个" ); - // TODO: add `zod:errors.(too_small|too_big).array.exact` expect(getErrorMessage(schema.length(2).safeParse([]))).toEqual( - "Array must contain exactly 2 element(s)" + "数组元素必须为 2 个" ); }); @@ -144,10 +145,10 @@ test("function parser error messages", () => { .function(z.tuple([z.string()]), z.number()) .parse((a: any) => a); expect(getErrorMessageFromZodError(() => functionParse(""))).toEqual( - "错误的返回值类型" + "错误的函数返回值格式" ); expect(getErrorMessageFromZodError(() => functionParse(1 as any))).toEqual( - "参数错误" + "错误的函数参数格式" ); }); @@ -161,12 +162,12 @@ test("other parser error messages", () => { ) .safeParse(1234) ) - ).toEqual("交集类型无法合并"); + ).toEqual("交叉类型结果无法被合并"); expect(getErrorMessage(z.literal(12).safeParse(""))).toEqual( - "无效的输入, 请输入 12" + "错误的字面量值,请输入 12" ); expect(getErrorMessage(z.enum(["A", "B", "C"]).safeParse("D"))).toEqual( - "无效的 'D' 值。请输入 'A' | 'B' | 'C'" + "错误的枚举值 'D'。请输入 'A' | 'B' | 'C'" ); expect( getErrorMessage( @@ -175,7 +176,7 @@ test("other parser error messages", () => { .strict() .safeParse({ dog: "", cat: "", rat: "" }) ) - ).toEqual("无法识别对象的键: 'cat', 'rat'"); + ).toEqual("对象中的键无法识别: 'cat', 'rat'"); expect( getErrorMessage( z @@ -185,10 +186,10 @@ test("other parser error messages", () => { ]) .safeParse({ type: "c", c: "abc" }) ) - ).toEqual("无效的标识符。请输入 'a' | 'b'"); + ).toEqual("标识值无法被区分。请输入 'a' | 'b'"); expect( getErrorMessage(z.union([z.string(), z.number()]).safeParse([true])) - ).toEqual("输入格式错误"); + ).toEqual("不满足联合类型中的选项"); expect( getErrorMessage( z @@ -198,5 +199,5 @@ test("other parser error messages", () => { }) .safeParse("") ) - ).toEqual("格式错误"); + ).toEqual("错误的输入格式"); });