Created
May 9, 2024 01:23
-
-
Save lemonlatte/7ef4a6c4344262f460c9561ccb17f028 to your computer and use it in GitHub Desktop.
pydantic error 翻譯
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import inspect | |
# from pydantic import errors | |
# gpt_translate( | |
# { | |
# k: v.msg_template | |
# for k, v in dict(inspect.getmembers(errors, inspect.isclass)).items() | |
# if issubclass(v, errors.PydanticValueError) and hasattr(v, "msg_template") | |
# } | |
# ) | |
PYDANTIC_ERROR_MSG_TEMPLATE_ZH: dict[str, str] = { | |
"AnyStrMaxLengthError": "確保此值最多有 {limit_value} 個字符", | |
"AnyStrMinLengthError": "確保此值至少有 {limit_value} 個字符", | |
"ColorError": "值不是有效的顏色:{reason}", | |
"DateError": "日期格式無效", | |
"DateNotInTheFutureError": "日期不在未來", | |
"DateNotInThePastError": "日期不在過去", | |
"DateTimeError": "日期時間格式無效", | |
"DecimalIsNotFiniteError": "值不是有效的十進制數", | |
"DecimalMaxDigitsError": "確保總位數不超過 {max_digits}", | |
"DecimalMaxPlacesError": "確保小數位數不超過 {decimal_places}", | |
"DecimalWholeDigitsError": "確保小數點前的位數不超過 {whole_digits}", | |
"DurationError": "持續時間格式無效", | |
"EmailError": "值不是有效的電子郵件地址", | |
"ExtraError": "不允許額外的字段", | |
"FrozenSetMaxLengthError": "確保此值最多有 {limit_value} 個元素", | |
"FrozenSetMinLengthError": "確保此值至少有 {limit_value} 個元素", | |
"IPv4AddressError": "值不是有效的 IPv4 地址", | |
"IPv4InterfaceError": "值不是有效的 IPv4 介面", | |
"IPv4NetworkError": "值不是有效的 IPv4 網路", | |
"IPv6AddressError": "值不是有效的 IPv6 地址", | |
"IPv6InterfaceError": "值不是有效的 IPv6 介面", | |
"IPv6NetworkError": "值不是有效的 IPv6 網路", | |
"IPvAnyAddressError": "值不是有效的 IPv4 或 IPv6 地址", | |
"IPvAnyInterfaceError": "值不是有效的 IPv4 或 IPv6 介面", | |
"IPvAnyNetworkError": "值不是有效的 IPv4 或 IPv6 網路", | |
"InvalidByteSize": "無法從位元組字符串解析值和單位", | |
"InvalidByteSizeUnit": "無法解釋位元組單位:{unit}", | |
"InvalidDiscriminator": "對於鑑別器 {discriminator_key!r} 和值 {discriminator_value!r} 沒有匹配(允許的值:{allowed_values})", | |
"InvalidLengthForBrand": "{brand} 卡的長度必須是 {required_length}", | |
"JsonError": "JSON 無效", | |
"ListMaxLengthError": "確保此值最多有 {limit_value} 個元素", | |
"ListMinLengthError": "確保此值至少有 {limit_value} 個元素", | |
"ListUniqueItemsError": "列表中有重複的項目", | |
"LuhnValidationError": "卡號未通過 Luhn 驗證", | |
"MissingDiscriminator": "值中缺少鑑別器 {discriminator_key!r}", | |
"MissingError": "缺少必填字段", | |
"NotDigitError": "卡號不全是數字", | |
"NumberNotFiniteError": "確保此值是有限數", | |
"NumberNotGeError": "確保此值大於等於 {limit_value}", | |
"NumberNotGtError": "確保此值大於 {limit_value}", | |
"NumberNotLeError": "確保此值小於等於 {limit_value}", | |
"NumberNotLtError": "確保此值小於 {limit_value}", | |
"NumberNotMultipleError": "確保此值是 {multiple_of} 的倍數", | |
"PathNotADirectoryError": '路徑 "{path}" 不指向目錄', | |
"PathNotAFileError": '路徑 "{path}" 不指向文件', | |
"PathNotExistsError": '在路徑 "{path}" 上不存在文件或目錄', | |
"PatternError": "正則表達式無效", | |
"SetMaxLengthError": "確保此值最多有 {limit_value} 個元素", | |
"SetMinLengthError": "確保此值至少有 {limit_value} 個元素", | |
"StrRegexError": '字符串不符合正則表達式 "{pattern}"', | |
"StrictBoolError": "值不是有效的布爾值", | |
"TimeError": "時間格式無效", | |
"TupleLengthError": "元組長度 {actual_length} 錯誤,預期為 {expected_length}", | |
"UUIDVersionError": "期望的 uuid 版本為 {required_version}", | |
"UrlExtraError": "URL 無效,在有效的 URL 之後發現額外字符:{extra!r}", | |
"UrlHostError": "URL 主機無效", | |
"UrlHostTldError": "URL 主機無效,需要頂級域名", | |
"UrlPortError": "URL 端口無效,端口不能超過 65535", | |
"UrlSchemeError": "URL 方案無效或缺失", | |
"UrlSchemePermittedError": "不允許使用 URL 方案", | |
"UrlUserInfoError": "URL 中需要用戶信息但缺失", | |
} | |
def override_pydantic_error_msg_template(error: ValidationError) -> ValidationError: | |
raw_errors = error.raw_errors | |
for e in raw_errors: | |
error_class_name = e.exc.__class__.__name__ | |
if error_class_name in PYDANTIC_ERROR_MSG_TEMPLATE_ZH: | |
e.exc.msg_template = PYDANTIC_ERROR_MSG_TEMPLATE_ZH[error_class_name] | |
return error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment