schema-dsl 1.2.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -210
- package/README.md +391 -2249
- package/dist/DslBuilder-DQDN0ZxZ.d.cts +341 -0
- package/dist/DslBuilder-DkLaOo9Q.d.ts +341 -0
- package/dist/Validator-C7GsVQOH.d.cts +192 -0
- package/dist/Validator-hFWKGxir.d.ts +192 -0
- package/dist/index.cjs +6594 -0
- package/dist/index.d.cts +1145 -0
- package/dist/index.d.ts +1145 -0
- package/dist/index.js +6528 -0
- package/dist/plugin-CIKtTMtS.d.cts +246 -0
- package/dist/plugin-CIKtTMtS.d.ts +246 -0
- package/dist/plugins/custom-format.cjs +3802 -0
- package/dist/plugins/custom-format.d.cts +12 -0
- package/dist/plugins/custom-format.d.ts +12 -0
- package/dist/plugins/custom-format.js +3772 -0
- package/dist/plugins/custom-type-example.cjs +3795 -0
- package/dist/plugins/custom-type-example.d.cts +8 -0
- package/dist/plugins/custom-type-example.d.ts +8 -0
- package/dist/plugins/custom-type-example.js +3765 -0
- package/dist/plugins/custom-validator.cjs +146 -0
- package/dist/plugins/custom-validator.d.cts +10 -0
- package/dist/plugins/custom-validator.d.ts +10 -0
- package/dist/plugins/custom-validator.js +121 -0
- package/docs/FEATURE-INDEX.md +102 -68
- package/docs/add-custom-locale.md +48 -35
- package/docs/add-keyword.md +24 -0
- package/docs/api-reference.md +396 -154
- package/docs/api.md +13 -0
- package/docs/best-practices-project-structure.md +19 -10
- package/docs/best-practices.md +93 -53
- package/docs/cache-manager.md +23 -15
- package/docs/compile.md +45 -0
- package/docs/conditional-api.md +40 -11
- package/docs/custom-extensions-guide.md +80 -152
- package/docs/design-philosophy.md +76 -71
- package/docs/doc-index.md +324 -0
- package/docs/dsl-syntax.md +69 -19
- package/docs/dynamic-locale.md +24 -14
- package/docs/enum.md +12 -5
- package/docs/error-handling.md +53 -44
- package/docs/export-guide.md +47 -8
- package/docs/export-limitations.md +27 -11
- package/docs/faq.md +86 -67
- package/docs/frontend-i18n-guide.md +26 -12
- package/docs/i18n-user-guide.md +60 -47
- package/docs/i18n.md +51 -32
- package/docs/index.md +48 -0
- package/docs/json-schema-basics.md +40 -0
- package/docs/label-vs-description.md +12 -3
- package/docs/markdown-exporter.md +15 -6
- package/docs/mongodb-exporter.md +11 -4
- package/docs/multi-language.md +26 -0
- package/docs/multi-type-support.md +26 -33
- package/docs/mysql-exporter.md +9 -2
- package/docs/number-operators.md +12 -5
- package/docs/optional-marker-guide.md +28 -23
- package/docs/performance-guide.md +49 -0
- package/docs/plugin-system.md +205 -366
- package/docs/plugin-type-registration.md +34 -0
- package/docs/postgresql-exporter.md +9 -2
- package/docs/public/favicon.svg +5 -0
- package/docs/quick-start.md +37 -363
- package/docs/runtime-locale-support.md +20 -9
- package/docs/schema-helper.md +10 -5
- package/docs/schema-utils-advanced-issues.md +23 -0
- package/docs/schema-utils-best-practices.md +20 -0
- package/docs/schema-utils-chaining.md +7 -0
- package/docs/schema-utils.md +76 -42
- package/docs/security-checklist.md +20 -0
- package/docs/string-extensions.md +17 -9
- package/docs/troubleshooting.md +36 -21
- package/docs/type-converter.md +41 -50
- package/docs/type-reference.md +38 -15
- package/docs/typescript-guide.md +53 -42
- package/docs/union-type-guide.md +11 -1
- package/docs/union-types.md +10 -3
- package/docs/validate-async.md +36 -25
- package/docs/validate-batch.md +49 -0
- package/docs/validate-dsl-object-support.md +33 -28
- package/docs/validate.md +36 -16
- package/docs/validation-guide.md +25 -7
- package/docs/validator.md +39 -0
- package/package.json +85 -27
- package/plugins/custom-format.cjs +8 -0
- package/plugins/custom-type-example.cjs +8 -0
- package/plugins/custom-validator.cjs +8 -0
- package/src/adapters/DslAdapter.ts +111 -0
- package/src/adapters/index.ts +1 -0
- package/src/config/constants.ts +83 -0
- package/src/config/index.ts +2 -0
- package/src/config/patterns.ts +77 -0
- package/src/core/CacheManager.ts +159 -0
- package/src/core/ConditionalBuilder.ts +382 -0
- package/src/core/ConditionalRuntime.ts +28 -0
- package/src/core/ConditionalValidator.ts +255 -0
- package/src/core/DslBuilder.ts +677 -0
- package/src/core/ErrorCodes.ts +38 -0
- package/src/core/ErrorFormatter.ts +271 -0
- package/src/core/JSONSchemaCore.ts +65 -0
- package/src/core/Locale.ts +187 -0
- package/src/core/MessageTemplate.ts +42 -0
- package/src/core/ObjectDslBuilder.ts +64 -0
- package/src/core/PluginManager.ts +326 -0
- package/src/core/StringExtensions.ts +140 -0
- package/src/core/TemplateEngine.ts +44 -0
- package/src/core/Validator.ts +448 -0
- package/src/errors/I18nError.ts +159 -0
- package/src/errors/ValidationError.ts +105 -0
- package/src/exporters/BaseExporter.ts +60 -0
- package/src/exporters/MarkdownExporter.ts +305 -0
- package/src/exporters/MongoDBExporter.ts +126 -0
- package/src/exporters/MySQLExporter.ts +155 -0
- package/src/exporters/PostgreSQLExporter.ts +222 -0
- package/src/exporters/index.ts +18 -0
- package/src/index.ts +633 -0
- package/{lib/locales/en-US.js → src/locales/en-US.ts} +21 -37
- package/{lib/locales/es-ES.js → src/locales/es-ES.ts} +63 -16
- package/{lib/locales/fr-FR.js → src/locales/fr-FR.ts} +74 -27
- package/src/locales/index.ts +103 -0
- package/{lib/locales/ja-JP.js → src/locales/ja-JP.ts} +59 -17
- package/src/locales/types.ts +156 -0
- package/{lib/locales/zh-CN.js → src/locales/zh-CN.ts} +21 -38
- package/src/parser/ConstraintParser.ts +101 -0
- package/src/parser/DslParser.ts +470 -0
- package/src/parser/SchemaCompiler.ts +66 -0
- package/src/parser/TypeRegistry.ts +250 -0
- package/src/parser/index.ts +6 -0
- package/src/plugins/custom-format.ts +126 -0
- package/src/plugins/custom-type-example.ts +108 -0
- package/src/plugins/custom-validator.ts +140 -0
- package/src/types/conditional.ts +28 -0
- package/src/types/config.ts +59 -0
- package/src/types/dsl.ts +131 -0
- package/src/types/error.ts +60 -0
- package/src/types/index.ts +17 -0
- package/src/types/infer.ts +128 -0
- package/src/types/plugin.ts +58 -0
- package/src/types/safe-regex.d.ts +9 -0
- package/src/types/schema.ts +66 -0
- package/src/types/validate.ts +71 -0
- package/src/utils/SchemaHelper.ts +196 -0
- package/src/utils/SchemaUtils.ts +346 -0
- package/src/utils/TypeConverter.ts +215 -0
- package/src/utils/index.ts +10 -0
- package/src/validators/CustomKeywords.ts +477 -0
- package/.eslintignore +0 -11
- package/.eslintrc.json +0 -27
- package/CONTRIBUTING.md +0 -368
- package/STATUS.md +0 -491
- package/changelogs/v1.0.0.md +0 -328
- package/changelogs/v1.0.9.md +0 -367
- package/changelogs/v1.1.0.md +0 -389
- package/changelogs/v1.1.1.md +0 -308
- package/changelogs/v1.1.2.md +0 -183
- package/changelogs/v1.1.3.md +0 -161
- package/changelogs/v1.1.4.md +0 -432
- package/changelogs/v1.1.5.md +0 -493
- package/changelogs/v1.1.6.md +0 -211
- package/changelogs/v1.1.8.md +0 -376
- package/changelogs/v1.2.3.md +0 -124
- package/docs/INDEX.md +0 -252
- package/docs/issues-resolved-summary.md +0 -196
- package/docs/performance-benchmark-report.md +0 -179
- package/docs/performance-quick-reference.md +0 -123
- package/docs/user-questions-answered.md +0 -353
- package/docs/validation-rules-v1.0.2.md +0 -1608
- package/examples/README.md +0 -81
- package/examples/array-dsl-example.js +0 -227
- package/examples/conditional-example.js +0 -288
- package/examples/conditional-non-object.js +0 -129
- package/examples/conditional-validate-example.js +0 -321
- package/examples/custom-extension.js +0 -85
- package/examples/dsl-match-example.js +0 -74
- package/examples/dsl-style.js +0 -118
- package/examples/dynamic-locale-configuration.js +0 -348
- package/examples/dynamic-locale-example.js +0 -287
- package/examples/enum.examples.js +0 -324
- package/examples/export-demo.js +0 -130
- package/examples/express-integration.js +0 -376
- package/examples/i18n-error-handling-complete.js +0 -381
- package/examples/i18n-error-handling-quickstart.md +0 -0
- package/examples/i18n-error.examples.js +0 -181
- package/examples/i18n-full-demo.js +0 -301
- package/examples/i18n-memory-safety.examples.js +0 -268
- package/examples/markdown-export.js +0 -71
- package/examples/middleware-usage.js +0 -93
- package/examples/new-features-comparison.js +0 -315
- package/examples/password-reset/README.md +0 -153
- package/examples/password-reset/schema.js +0 -26
- package/examples/password-reset/test.js +0 -101
- package/examples/plugin-system.examples.js +0 -205
- package/examples/schema-utils-chaining.examples.js +0 -250
- package/examples/simple-example.js +0 -122
- package/examples/slug.examples.js +0 -179
- package/examples/string-extensions.js +0 -297
- package/examples/union-type-example.js +0 -127
- package/examples/union-types-example.js +0 -77
- package/examples/user-registration/README.md +0 -156
- package/examples/user-registration/routes.js +0 -92
- package/examples/user-registration/schema.js +0 -150
- package/examples/user-registration/server.js +0 -74
- package/index.d.ts +0 -3540
- package/index.js +0 -457
- package/index.mjs +0 -60
- package/lib/adapters/DslAdapter.js +0 -871
- package/lib/adapters/index.js +0 -20
- package/lib/config/constants.js +0 -286
- package/lib/config/patterns/common.js +0 -47
- package/lib/config/patterns/creditCard.js +0 -9
- package/lib/config/patterns/idCard.js +0 -9
- package/lib/config/patterns/index.js +0 -9
- package/lib/config/patterns/licensePlate.js +0 -4
- package/lib/config/patterns/passport.js +0 -4
- package/lib/config/patterns/phone.js +0 -9
- package/lib/config/patterns/postalCode.js +0 -5
- package/lib/core/CacheManager.js +0 -376
- package/lib/core/ConditionalBuilder.js +0 -503
- package/lib/core/DslBuilder.js +0 -1400
- package/lib/core/ErrorCodes.js +0 -233
- package/lib/core/ErrorFormatter.js +0 -445
- package/lib/core/JSONSchemaCore.js +0 -347
- package/lib/core/Locale.js +0 -130
- package/lib/core/MessageTemplate.js +0 -98
- package/lib/core/PluginManager.js +0 -448
- package/lib/core/StringExtensions.js +0 -240
- package/lib/core/Validator.js +0 -654
- package/lib/errors/I18nError.js +0 -328
- package/lib/errors/ValidationError.js +0 -191
- package/lib/exporters/MarkdownExporter.js +0 -420
- package/lib/exporters/MongoDBExporter.js +0 -162
- package/lib/exporters/MySQLExporter.js +0 -212
- package/lib/exporters/PostgreSQLExporter.js +0 -289
- package/lib/exporters/index.js +0 -24
- package/lib/locales/index.js +0 -8
- package/lib/utils/LRUCache.js +0 -174
- package/lib/utils/SchemaHelper.js +0 -240
- package/lib/utils/SchemaUtils.js +0 -445
- package/lib/utils/TypeConverter.js +0 -245
- package/lib/utils/index.js +0 -13
- package/lib/validators/CustomKeywords.js +0 -616
- package/lib/validators/index.js +0 -11
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import type { LocaleMessages } from './types.js'
|
|
2
|
+
|
|
3
|
+
const enUS: LocaleMessages = {
|
|
2
4
|
// Generic
|
|
3
5
|
required: '{{#label}} is required',
|
|
4
6
|
type: '{{#label}} should be {{#expected}}, got {{#actual}}',
|
|
@@ -12,12 +14,12 @@ module.exports = {
|
|
|
12
14
|
'max-depth': 'Maximum recursion depth ({{#depth}}) exceeded at {{#label}}',
|
|
13
15
|
exception: '{{#label}} validation exception: {{#message}}',
|
|
14
16
|
|
|
15
|
-
// Conditional
|
|
17
|
+
// Conditional
|
|
16
18
|
'conditional.underAge': 'Minors cannot register',
|
|
17
19
|
'conditional.blocked': 'Account has been blocked',
|
|
18
20
|
'conditional.notAllowed': 'Registration not allowed',
|
|
19
21
|
|
|
20
|
-
// I18nError
|
|
22
|
+
// I18nError — generic
|
|
21
23
|
'error.notFound': '{{#resource}} not found',
|
|
22
24
|
'error.forbidden': 'Access to {{#resource}} is forbidden',
|
|
23
25
|
'error.unauthorized': 'Unauthorized, please log in',
|
|
@@ -25,34 +27,27 @@ module.exports = {
|
|
|
25
27
|
'error.duplicate': '{{#resource}} already exists',
|
|
26
28
|
'error.conflict': 'Operation conflict: {{#reason}}',
|
|
27
29
|
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
'account.notFound': {
|
|
31
|
-
code: 'ACCOUNT_NOT_FOUND',
|
|
32
|
-
message: 'Account not found'
|
|
33
|
-
},
|
|
30
|
+
// Account
|
|
31
|
+
'account.notFound': { code: 'ACCOUNT_NOT_FOUND', message: 'Account not found' },
|
|
34
32
|
'account.inactive': 'Account is inactive',
|
|
35
33
|
'account.banned': 'Account has been banned',
|
|
36
34
|
'account.insufficientBalance': {
|
|
37
35
|
code: 'INSUFFICIENT_BALANCE',
|
|
38
|
-
message: 'Insufficient balance, current: {{#balance}}, required: {{#required}}'
|
|
36
|
+
message: 'Insufficient balance, current: {{#balance}}, required: {{#required}}',
|
|
39
37
|
},
|
|
40
38
|
'account.insufficientCredits': 'Insufficient credits, current: {{#credits}}, required: {{#required}}',
|
|
41
39
|
|
|
42
|
-
//
|
|
40
|
+
// User
|
|
43
41
|
'user.notFound': 'User not found',
|
|
44
42
|
'user.notVerified': 'User is not verified',
|
|
45
43
|
'user.noPermission': 'No admin permission',
|
|
46
44
|
|
|
47
|
-
//
|
|
48
|
-
'order.notPaid': {
|
|
49
|
-
code: 'ORDER_NOT_PAID',
|
|
50
|
-
message: 'Order not paid'
|
|
51
|
-
},
|
|
45
|
+
// Order
|
|
46
|
+
'order.notPaid': { code: 'ORDER_NOT_PAID', message: 'Order not paid' },
|
|
52
47
|
'order.paymentMissing': 'Payment information missing',
|
|
53
48
|
'order.addressMissing': 'Shipping address missing',
|
|
54
49
|
|
|
55
|
-
//
|
|
50
|
+
// Format
|
|
56
51
|
'format.email': '{{#label}} must be a valid email address',
|
|
57
52
|
'format.url': '{{#label}} must be a valid URL',
|
|
58
53
|
'format.uuid': '{{#label}} must be a valid UUID',
|
|
@@ -67,7 +62,6 @@ module.exports = {
|
|
|
67
62
|
'string.hostname': '{{#label}} must be a valid hostname',
|
|
68
63
|
'string.pattern': '{{#label}} format does not match required pattern',
|
|
69
64
|
'string.enum': '{{#label}} must be one of: {{#valids}}',
|
|
70
|
-
// v1.0.2
|
|
71
65
|
'string.length': '{{#label}} length must be exactly {{#limit}} characters',
|
|
72
66
|
'string.alphanum': '{{#label}} must only contain alphanumeric characters',
|
|
73
67
|
'string.trim': '{{#label}} must not have leading or trailing whitespace',
|
|
@@ -81,7 +75,6 @@ module.exports = {
|
|
|
81
75
|
'number.integer': '{{#label}} must be an integer',
|
|
82
76
|
'number.positive': '{{#label}} must be a positive number',
|
|
83
77
|
'number.negative': '{{#label}} must be a negative number',
|
|
84
|
-
// v1.0.2
|
|
85
78
|
'number.precision': '{{#label}} must have at most {{#limit}} decimal places',
|
|
86
79
|
'number.port': '{{#label}} must be a valid port number (1-65535)',
|
|
87
80
|
|
|
@@ -93,10 +86,8 @@ module.exports = {
|
|
|
93
86
|
'object.min': '{{#label}} must have at least {{#limit}} properties',
|
|
94
87
|
'object.max': '{{#label}} must have at most {{#limit}} properties',
|
|
95
88
|
'object.unknown': '{{#label}} contains unknown property: {{#key}}',
|
|
96
|
-
// v1.0.2
|
|
97
89
|
'object.missing': '{{#label}} is missing required properties',
|
|
98
90
|
'object.schema': '{{#label}} contains additional properties',
|
|
99
|
-
// v1.1.6 - additionalProperties
|
|
100
91
|
'additionalProperties': '{{#label}} must NOT have additional properties: {{#key}}',
|
|
101
92
|
|
|
102
93
|
// Array
|
|
@@ -105,7 +96,6 @@ module.exports = {
|
|
|
105
96
|
'array.max': '{{#label}} must have at most {{#limit}} items',
|
|
106
97
|
'array.length': '{{#label}} must have exactly {{#limit}} items',
|
|
107
98
|
'array.unique': '{{#label}} must not contain duplicate items',
|
|
108
|
-
// v1.0.2
|
|
109
99
|
'array.sparse': '{{#label}} must not be a sparse array',
|
|
110
100
|
'array.includesRequired': '{{#label}} must include required items',
|
|
111
101
|
|
|
@@ -113,7 +103,6 @@ module.exports = {
|
|
|
113
103
|
'date.base': '{{#label}} must be a valid date',
|
|
114
104
|
'date.min': '{{#label}} must be no earlier than {{#limit}}',
|
|
115
105
|
'date.max': '{{#label}} must be no later than {{#limit}}',
|
|
116
|
-
// v1.0.2
|
|
117
106
|
'date.format': '{{#label}} date format is invalid',
|
|
118
107
|
'date.greater': '{{#label}} must be after {{#limit}}',
|
|
119
108
|
'date.less': '{{#label}} must be before {{#limit}}',
|
|
@@ -124,7 +113,7 @@ module.exports = {
|
|
|
124
113
|
'any.only': '{{#label}} must match {{#valids}}',
|
|
125
114
|
'any.unknown': 'Field {{#key}} is not allowed',
|
|
126
115
|
|
|
127
|
-
// Patterns
|
|
116
|
+
// Patterns
|
|
128
117
|
'pattern.phone': 'Invalid phone number',
|
|
129
118
|
'pattern.phone.international': 'Invalid international phone number',
|
|
130
119
|
'pattern.idCard': 'Invalid ID card number',
|
|
@@ -143,34 +132,29 @@ module.exports = {
|
|
|
143
132
|
'pattern.macAddress': 'Invalid MAC Address',
|
|
144
133
|
'pattern.cron': 'Invalid Cron Expression',
|
|
145
134
|
'pattern.slug': 'URL slug can only contain lowercase letters, numbers, and hyphens',
|
|
146
|
-
// v1.0.2 新增
|
|
147
135
|
'pattern.domain': '{{#label}} must be a valid domain name',
|
|
148
136
|
'pattern.ip': '{{#label}} must be a valid IP address',
|
|
149
137
|
'pattern.base64': '{{#label}} must be a valid Base64 string',
|
|
150
138
|
'pattern.jwt': '{{#label}} must be a valid JWT token',
|
|
151
139
|
'pattern.json': '{{#label}} must be a valid JSON string',
|
|
152
|
-
|
|
153
|
-
// Username & Password
|
|
154
140
|
'pattern.username': 'Username must start with a letter and contain only letters, numbers, and underscores',
|
|
155
141
|
'pattern.password.weak': 'Password must be at least 6 characters',
|
|
156
142
|
'pattern.password.medium': 'Password must be at least 8 characters and contain letters and numbers',
|
|
157
143
|
'pattern.password.strong': 'Password must be at least 8 characters and contain uppercase, lowercase letters and numbers',
|
|
158
144
|
'pattern.password.veryStrong': 'Password must be at least 10 characters and contain uppercase, lowercase letters, numbers and special characters',
|
|
159
|
-
|
|
160
|
-
// Union Type
|
|
161
145
|
'pattern.emailOrPhone': 'Must be an email or phone number',
|
|
162
146
|
'pattern.usernameOrEmail': 'Must be a username or email',
|
|
163
147
|
'pattern.httpOrHttps': 'Must be a URL starting with http or https',
|
|
164
148
|
|
|
165
|
-
// oneOf
|
|
149
|
+
// oneOf
|
|
166
150
|
oneOf: '{{#label}} must match one of the following types',
|
|
167
151
|
'oneOf.invalid': '{{#label}} value does not match any allowed type',
|
|
168
152
|
|
|
169
|
-
//
|
|
170
|
-
|
|
153
|
+
// Error fallback
|
|
154
|
+
UNKNOWN_ERROR: 'Unknown validation error',
|
|
155
|
+
CUSTOM_VALIDATION_FAILED: 'Validation failed',
|
|
156
|
+
ASYNC_VALIDATION_NOT_SUPPORTED: 'Async validation not supported in synchronous validate()',
|
|
157
|
+
VALIDATE_MUST_BE_FUNCTION: 'validate must be a function',
|
|
158
|
+
}
|
|
171
159
|
|
|
172
|
-
|
|
173
|
-
'CUSTOM_VALIDATION_FAILED': 'Validation failed',
|
|
174
|
-
'ASYNC_VALIDATION_NOT_SUPPORTED': 'Async validation not supported in synchronous validate()',
|
|
175
|
-
'VALIDATE_MUST_BE_FUNCTION': 'validate must be a function'
|
|
176
|
-
};
|
|
160
|
+
export default enUS
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import type { LocaleMessages } from './types.js'
|
|
2
|
+
|
|
3
|
+
const esES: LocaleMessages = {
|
|
2
4
|
// Generic
|
|
3
5
|
required: '{{#label}} es obligatorio',
|
|
4
6
|
type: '{{#label}} debe ser de tipo {{#expected}}, pero se obtuvo {{#actual}}',
|
|
@@ -12,7 +14,40 @@ module.exports = {
|
|
|
12
14
|
'max-depth': 'Profundidad máxima de recursión ({{#depth}}) excedida en {{#label}}',
|
|
13
15
|
exception: 'Excepción de validación en {{#label}}: {{#message}}',
|
|
14
16
|
|
|
15
|
-
//
|
|
17
|
+
// Conditional (v2 additions)
|
|
18
|
+
'conditional.underAge': 'Los menores de edad no pueden registrarse',
|
|
19
|
+
'conditional.blocked': 'La cuenta ha sido bloqueada',
|
|
20
|
+
'conditional.notAllowed': 'El registro no está permitido',
|
|
21
|
+
|
|
22
|
+
// I18nError — generic (v2 additions)
|
|
23
|
+
'error.notFound': '{{#resource}} no encontrado',
|
|
24
|
+
'error.forbidden': 'Acceso a {{#resource}} está prohibido',
|
|
25
|
+
'error.unauthorized': 'No autorizado, por favor inicie sesión',
|
|
26
|
+
'error.invalid': '{{#field}} no es válido',
|
|
27
|
+
'error.duplicate': '{{#resource}} ya existe',
|
|
28
|
+
'error.conflict': 'Conflicto de operación: {{#reason}}',
|
|
29
|
+
|
|
30
|
+
// Account (v2 additions)
|
|
31
|
+
'account.notFound': { code: 'ACCOUNT_NOT_FOUND', message: 'Cuenta no encontrada' },
|
|
32
|
+
'account.inactive': 'La cuenta está inactiva',
|
|
33
|
+
'account.banned': 'La cuenta ha sido suspendida',
|
|
34
|
+
'account.insufficientBalance': {
|
|
35
|
+
code: 'INSUFFICIENT_BALANCE',
|
|
36
|
+
message: 'Saldo insuficiente, actual: {{#balance}}, requerido: {{#required}}',
|
|
37
|
+
},
|
|
38
|
+
'account.insufficientCredits': 'Créditos insuficientes, actual: {{#credits}}, requerido: {{#required}}',
|
|
39
|
+
|
|
40
|
+
// User (v2 additions)
|
|
41
|
+
'user.notFound': 'Usuario no encontrado',
|
|
42
|
+
'user.notVerified': 'El usuario no está verificado',
|
|
43
|
+
'user.noPermission': 'Sin permisos de administrador',
|
|
44
|
+
|
|
45
|
+
// Order (v2 additions)
|
|
46
|
+
'order.notPaid': { code: 'ORDER_NOT_PAID', message: 'Pedido no pagado' },
|
|
47
|
+
'order.paymentMissing': 'Falta información de pago',
|
|
48
|
+
'order.addressMissing': 'Falta la dirección de envío',
|
|
49
|
+
|
|
50
|
+
// Format
|
|
16
51
|
'format.email': '{{#label}} debe ser una dirección de correo electrónico válida',
|
|
17
52
|
'format.url': '{{#label}} debe ser una URL válida',
|
|
18
53
|
'format.uuid': '{{#label}} debe ser un UUID válido',
|
|
@@ -27,6 +62,11 @@ module.exports = {
|
|
|
27
62
|
'string.hostname': '{{#label}} debe ser un nombre de host válido',
|
|
28
63
|
'string.pattern': 'El formato de {{#label}} no coincide con el patrón requerido',
|
|
29
64
|
'string.enum': '{{#label}} debe ser uno de: {{#valids}}',
|
|
65
|
+
'string.length': 'La longitud de {{#label}} debe ser exactamente {{#limit}} caracteres',
|
|
66
|
+
'string.alphanum': '{{#label}} solo puede contener caracteres alfanuméricos',
|
|
67
|
+
'string.trim': '{{#label}} no debe tener espacios al principio ni al final',
|
|
68
|
+
'string.lowercase': '{{#label}} debe estar en minúsculas',
|
|
69
|
+
'string.uppercase': '{{#label}} debe estar en mayúsculas',
|
|
30
70
|
|
|
31
71
|
// Number
|
|
32
72
|
'number.base': '{{#label}} debe ser un número',
|
|
@@ -35,6 +75,8 @@ module.exports = {
|
|
|
35
75
|
'number.integer': '{{#label}} debe ser un número entero',
|
|
36
76
|
'number.positive': '{{#label}} debe ser un número positivo',
|
|
37
77
|
'number.negative': '{{#label}} debe ser un número negativo',
|
|
78
|
+
'number.precision': '{{#label}} debe tener como máximo {{#limit}} decimales',
|
|
79
|
+
'number.port': '{{#label}} debe ser un número de puerto válido (1-65535)',
|
|
38
80
|
|
|
39
81
|
// Boolean
|
|
40
82
|
'boolean.base': '{{#label}} debe ser un booleano',
|
|
@@ -44,7 +86,8 @@ module.exports = {
|
|
|
44
86
|
'object.min': '{{#label}} debe tener al menos {{#limit}} propiedades',
|
|
45
87
|
'object.max': '{{#label}} debe tener como máximo {{#limit}} propiedades',
|
|
46
88
|
'object.unknown': '{{#label}} contiene una propiedad desconocida: {{#key}}',
|
|
47
|
-
|
|
89
|
+
'object.missing': 'A {{#label}} le faltan propiedades requeridas',
|
|
90
|
+
'object.schema': '{{#label}} contiene propiedades adicionales',
|
|
48
91
|
'additionalProperties': '{{#label}} NO debe tener propiedades adicionales: {{#key}}',
|
|
49
92
|
|
|
50
93
|
// Array
|
|
@@ -53,11 +96,16 @@ module.exports = {
|
|
|
53
96
|
'array.max': '{{#label}} debe tener como máximo {{#limit}} elementos',
|
|
54
97
|
'array.length': '{{#label}} debe tener exactamente {{#limit}} elementos',
|
|
55
98
|
'array.unique': '{{#label}} no debe contener elementos duplicados',
|
|
99
|
+
'array.sparse': '{{#label}} no debe ser un array disperso',
|
|
100
|
+
'array.includesRequired': '{{#label}} debe incluir los elementos requeridos',
|
|
56
101
|
|
|
57
102
|
// Date
|
|
58
103
|
'date.base': '{{#label}} debe ser una fecha válida',
|
|
59
104
|
'date.min': '{{#label}} no debe ser anterior a {{#limit}}',
|
|
60
105
|
'date.max': '{{#label}} no debe ser posterior a {{#limit}}',
|
|
106
|
+
'date.format': 'El formato de fecha de {{#label}} no es válido',
|
|
107
|
+
'date.greater': '{{#label}} debe ser posterior a {{#limit}}',
|
|
108
|
+
'date.less': '{{#label}} debe ser anterior a {{#limit}}',
|
|
61
109
|
|
|
62
110
|
// Any
|
|
63
111
|
'any.required': '{{#label}} es obligatorio',
|
|
@@ -65,7 +113,7 @@ module.exports = {
|
|
|
65
113
|
'any.only': '{{#label}} debe coincidir con {{#valids}}',
|
|
66
114
|
'any.unknown': 'El campo {{#key}} no está permitido',
|
|
67
115
|
|
|
68
|
-
// Patterns
|
|
116
|
+
// Patterns
|
|
69
117
|
'pattern.phone': 'Número de teléfono no válido',
|
|
70
118
|
'pattern.phone.international': 'Número de teléfono internacional no válido',
|
|
71
119
|
'pattern.idCard': 'Número de tarjeta de identificación no válido',
|
|
@@ -84,30 +132,29 @@ module.exports = {
|
|
|
84
132
|
'pattern.macAddress': 'Dirección MAC no válida',
|
|
85
133
|
'pattern.cron': 'Expresión Cron no válida',
|
|
86
134
|
'pattern.slug': 'El slug de URL solo puede contener letras minúsculas, números y guiones',
|
|
87
|
-
// v1.0.2 新增
|
|
88
135
|
'pattern.domain': '{{#label}} debe ser un nombre de dominio válido',
|
|
89
136
|
'pattern.ip': '{{#label}} debe ser una dirección IP válida',
|
|
90
137
|
'pattern.base64': '{{#label}} debe ser una cadena Base64 válida',
|
|
91
138
|
'pattern.jwt': '{{#label}} debe ser un token JWT válido',
|
|
92
139
|
'pattern.json': '{{#label}} debe ser una cadena JSON válida',
|
|
93
|
-
|
|
94
|
-
// Username & Password
|
|
95
140
|
'pattern.username': 'El nombre de usuario debe comenzar con una letra y contener solo letras, números y guiones bajos',
|
|
96
141
|
'pattern.password.weak': 'La contraseña debe tener al menos 6 caracteres',
|
|
97
142
|
'pattern.password.medium': 'La contraseña debe tener al menos 8 caracteres y contener letras y números',
|
|
98
143
|
'pattern.password.strong': 'La contraseña debe tener al menos 8 caracteres y contener letras mayúsculas, minúsculas y números',
|
|
99
144
|
'pattern.password.veryStrong': 'La contraseña debe tener al menos 10 caracteres y contener letras mayúsculas, minúsculas, números y caracteres especiales',
|
|
145
|
+
'pattern.emailOrPhone': 'Debe ser un correo electrónico o número de teléfono',
|
|
146
|
+
'pattern.usernameOrEmail': 'Debe ser un nombre de usuario o correo electrónico',
|
|
147
|
+
'pattern.httpOrHttps': 'Debe ser una URL que comience con http o https',
|
|
100
148
|
|
|
101
|
-
//
|
|
102
|
-
'UNKNOWN_ERROR': 'Error de validación desconocido',
|
|
103
|
-
|
|
104
|
-
// oneOf (unión entre tipos) - v1.1.0
|
|
149
|
+
// oneOf
|
|
105
150
|
oneOf: '{{#label}} debe coincidir con uno de los siguientes tipos',
|
|
106
151
|
'oneOf.invalid': 'El valor de {{#label}} no coincide con ningún tipo permitido',
|
|
107
152
|
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
153
|
+
// Error fallback
|
|
154
|
+
UNKNOWN_ERROR: 'Error de validación desconocido',
|
|
155
|
+
CUSTOM_VALIDATION_FAILED: 'Validación fallida',
|
|
156
|
+
ASYNC_VALIDATION_NOT_SUPPORTED: 'La validación asíncrona no es compatible en validate() síncrono',
|
|
157
|
+
VALIDATE_MUST_BE_FUNCTION: 'validate debe ser una función',
|
|
158
|
+
}
|
|
113
159
|
|
|
160
|
+
export default esES
|
|
@@ -1,18 +1,53 @@
|
|
|
1
|
-
|
|
1
|
+
import type { LocaleMessages } from './types.js'
|
|
2
|
+
|
|
3
|
+
const frFR: LocaleMessages = {
|
|
2
4
|
// Generic
|
|
3
5
|
required: '{{#label}} est requis',
|
|
4
|
-
type:
|
|
5
|
-
min:
|
|
6
|
-
max:
|
|
6
|
+
type: "{{#label}} doit être de type {{#expected}}, mais {{#actual}} a été reçu",
|
|
7
|
+
min: "La longueur de {{#label}} doit être d'au moins {{#limit}}",
|
|
8
|
+
max: "La longueur de {{#label}} doit être d'au plus {{#limit}}",
|
|
7
9
|
length: 'La longueur de {{#label}} doit être exactement {{#expected}}',
|
|
8
|
-
pattern:
|
|
10
|
+
pattern: "Le format de {{#label}} n'est pas valide",
|
|
9
11
|
enum: '{{#label}} doit être l\'un de : {{#allowed}}',
|
|
10
12
|
custom: 'Validation échouée pour {{#label}} : {{#message}}',
|
|
11
13
|
circular: 'Référence circulaire détectée dans {{#label}}',
|
|
12
14
|
'max-depth': 'Profondeur maximale de récursion ({{#depth}}) dépassée dans {{#label}}',
|
|
13
15
|
exception: 'Exception de validation dans {{#label}} : {{#message}}',
|
|
14
16
|
|
|
15
|
-
//
|
|
17
|
+
// Conditional (v2 additions)
|
|
18
|
+
'conditional.underAge': 'Les mineurs ne peuvent pas s\'inscrire',
|
|
19
|
+
'conditional.blocked': 'Le compte a été bloqué',
|
|
20
|
+
'conditional.notAllowed': 'L\'inscription n\'est pas autorisée',
|
|
21
|
+
|
|
22
|
+
// I18nError — generic (v2 additions)
|
|
23
|
+
'error.notFound': '{{#resource}} introuvable',
|
|
24
|
+
'error.forbidden': 'Accès à {{#resource}} interdit',
|
|
25
|
+
'error.unauthorized': 'Non autorisé, veuillez vous connecter',
|
|
26
|
+
'error.invalid': '{{#field}} n\'est pas valide',
|
|
27
|
+
'error.duplicate': '{{#resource}} existe déjà',
|
|
28
|
+
'error.conflict': 'Conflit d\'opération : {{#reason}}',
|
|
29
|
+
|
|
30
|
+
// Account (v2 additions)
|
|
31
|
+
'account.notFound': { code: 'ACCOUNT_NOT_FOUND', message: 'Compte introuvable' },
|
|
32
|
+
'account.inactive': 'Le compte est inactif',
|
|
33
|
+
'account.banned': 'Le compte a été banni',
|
|
34
|
+
'account.insufficientBalance': {
|
|
35
|
+
code: 'INSUFFICIENT_BALANCE',
|
|
36
|
+
message: 'Solde insuffisant, actuel : {{#balance}}, requis : {{#required}}',
|
|
37
|
+
},
|
|
38
|
+
'account.insufficientCredits': 'Crédits insuffisants, actuels : {{#credits}}, requis : {{#required}}',
|
|
39
|
+
|
|
40
|
+
// User (v2 additions)
|
|
41
|
+
'user.notFound': 'Utilisateur introuvable',
|
|
42
|
+
'user.notVerified': "L'utilisateur n'est pas vérifié",
|
|
43
|
+
'user.noPermission': 'Pas de permission d\'administrateur',
|
|
44
|
+
|
|
45
|
+
// Order (v2 additions)
|
|
46
|
+
'order.notPaid': { code: 'ORDER_NOT_PAID', message: 'Commande non payée' },
|
|
47
|
+
'order.paymentMissing': 'Informations de paiement manquantes',
|
|
48
|
+
'order.addressMissing': 'Adresse de livraison manquante',
|
|
49
|
+
|
|
50
|
+
// Format
|
|
16
51
|
'format.email': '{{#label}} doit être une adresse e-mail valide',
|
|
17
52
|
'format.url': '{{#label}} doit être une URL valide',
|
|
18
53
|
'format.uuid': '{{#label}} doit être un UUID valide',
|
|
@@ -24,9 +59,14 @@ module.exports = {
|
|
|
24
59
|
'format.binary': '{{#label}} doit être une chaîne base64 valide',
|
|
25
60
|
|
|
26
61
|
// String
|
|
27
|
-
'string.hostname':
|
|
62
|
+
'string.hostname': "{{#label}} doit être un nom d'hôte valide",
|
|
28
63
|
'string.pattern': 'Le format de {{#label}} ne correspond pas au modèle requis',
|
|
29
64
|
'string.enum': '{{#label}} doit être l\'un de : {{#valids}}',
|
|
65
|
+
'string.length': 'La longueur de {{#label}} doit être exactement {{#limit}} caractères',
|
|
66
|
+
'string.alphanum': '{{#label}} ne doit contenir que des caractères alphanumériques',
|
|
67
|
+
'string.trim': '{{#label}} ne doit pas avoir d\'espaces en début ou en fin',
|
|
68
|
+
'string.lowercase': '{{#label}} doit être en minuscules',
|
|
69
|
+
'string.uppercase': '{{#label}} doit être en majuscules',
|
|
30
70
|
|
|
31
71
|
// Number
|
|
32
72
|
'number.base': '{{#label}} doit être un nombre',
|
|
@@ -35,6 +75,8 @@ module.exports = {
|
|
|
35
75
|
'number.integer': '{{#label}} doit être un nombre entier',
|
|
36
76
|
'number.positive': '{{#label}} doit être un nombre positif',
|
|
37
77
|
'number.negative': '{{#label}} doit être un nombre négatif',
|
|
78
|
+
'number.precision': '{{#label}} doit avoir au plus {{#limit}} décimales',
|
|
79
|
+
'number.port': '{{#label}} doit être un numéro de port valide (1-65535)',
|
|
38
80
|
|
|
39
81
|
// Boolean
|
|
40
82
|
'boolean.base': '{{#label}} doit être un booléen',
|
|
@@ -44,7 +86,8 @@ module.exports = {
|
|
|
44
86
|
'object.min': '{{#label}} doit avoir au moins {{#limit}} propriétés',
|
|
45
87
|
'object.max': '{{#label}} doit avoir au plus {{#limit}} propriétés',
|
|
46
88
|
'object.unknown': '{{#label}} contient une propriété inconnue : {{#key}}',
|
|
47
|
-
|
|
89
|
+
'object.missing': '{{#label}} est manquant les propriétés requises',
|
|
90
|
+
'object.schema': '{{#label}} contient des propriétés supplémentaires',
|
|
48
91
|
'additionalProperties': '{{#label}} NE doit PAS avoir de propriétés supplémentaires : {{#key}}',
|
|
49
92
|
|
|
50
93
|
// Array
|
|
@@ -53,11 +96,16 @@ module.exports = {
|
|
|
53
96
|
'array.max': '{{#label}} doit avoir au plus {{#limit}} éléments',
|
|
54
97
|
'array.length': '{{#label}} doit avoir exactement {{#limit}} éléments',
|
|
55
98
|
'array.unique': '{{#label}} ne doit pas contenir d\'éléments en double',
|
|
99
|
+
'array.sparse': '{{#label}} ne doit pas être un tableau clairsemé',
|
|
100
|
+
'array.includesRequired': '{{#label}} doit inclure les éléments requis',
|
|
56
101
|
|
|
57
102
|
// Date
|
|
58
103
|
'date.base': '{{#label}} doit être une date valide',
|
|
59
104
|
'date.min': '{{#label}} ne doit pas être antérieur à {{#limit}}',
|
|
60
105
|
'date.max': '{{#label}} ne doit pas être postérieur à {{#limit}}',
|
|
106
|
+
'date.format': "Le format de date de {{#label}} n'est pas valide",
|
|
107
|
+
'date.greater': '{{#label}} doit être après {{#limit}}',
|
|
108
|
+
'date.less': '{{#label}} doit être avant {{#limit}}',
|
|
61
109
|
|
|
62
110
|
// Any
|
|
63
111
|
'any.required': '{{#label}} est requis',
|
|
@@ -65,10 +113,10 @@ module.exports = {
|
|
|
65
113
|
'any.only': '{{#label}} doit correspondre à {{#valids}}',
|
|
66
114
|
'any.unknown': 'Le champ {{#key}} n\'est pas autorisé',
|
|
67
115
|
|
|
68
|
-
// Patterns
|
|
116
|
+
// Patterns
|
|
69
117
|
'pattern.phone': 'Numéro de téléphone non valide',
|
|
70
118
|
'pattern.phone.international': 'Numéro de téléphone international non valide',
|
|
71
|
-
'pattern.idCard':
|
|
119
|
+
'pattern.idCard': "Numéro de carte d'identité non valide",
|
|
72
120
|
'pattern.creditCard': 'Numéro de carte de crédit non valide',
|
|
73
121
|
'pattern.creditCard.visa': 'Numéro de carte Visa non valide',
|
|
74
122
|
'pattern.creditCard.mastercard': 'Numéro Mastercard non valide',
|
|
@@ -76,38 +124,37 @@ module.exports = {
|
|
|
76
124
|
'pattern.creditCard.discover': 'Numéro de carte Discover non valide',
|
|
77
125
|
'pattern.creditCard.jcb': 'Numéro de carte JCB non valide',
|
|
78
126
|
'pattern.creditCard.unionpay': 'Numéro de carte UnionPay non valide',
|
|
79
|
-
|
|
127
|
+
"pattern.licensePlate": "Numéro de plaque d'immatriculation non valide",
|
|
80
128
|
'pattern.postalCode': 'Code postal non valide',
|
|
81
129
|
'pattern.passport': 'Numéro de passeport non valide',
|
|
82
130
|
'pattern.objectId': 'ObjectId non valide',
|
|
83
131
|
'pattern.hexColor': 'Couleur hexadécimale non valide',
|
|
84
132
|
'pattern.macAddress': 'Adresse MAC non valide',
|
|
85
133
|
'pattern.cron': 'Expression Cron non valide',
|
|
86
|
-
|
|
87
|
-
// v1.0.2 新增
|
|
134
|
+
"pattern.slug": "Le slug d'URL ne peut contenir que des lettres minuscules, des chiffres et des tirets",
|
|
88
135
|
'pattern.domain': '{{#label}} doit être un nom de domaine valide',
|
|
89
136
|
'pattern.ip': '{{#label}} doit être une adresse IP valide',
|
|
90
137
|
'pattern.base64': '{{#label}} doit être une chaîne Base64 valide',
|
|
91
138
|
'pattern.jwt': '{{#label}} doit être un token JWT valide',
|
|
92
139
|
'pattern.json': '{{#label}} doit être une chaîne JSON valide',
|
|
93
|
-
|
|
94
|
-
// Username & Password
|
|
95
|
-
'pattern.username': 'Le nom d\'utilisateur doit commencer par une lettre et contenir uniquement des lettres, des chiffres et des tirets bas',
|
|
140
|
+
"pattern.username": "Le nom d'utilisateur doit commencer par une lettre et contenir uniquement des lettres, des chiffres et des tirets bas",
|
|
96
141
|
'pattern.password.weak': 'Le mot de passe doit contenir au moins 6 caractères',
|
|
97
142
|
'pattern.password.medium': 'Le mot de passe doit contenir au moins 8 caractères et inclure des lettres et des chiffres',
|
|
98
143
|
'pattern.password.strong': 'Le mot de passe doit contenir au moins 8 caractères et inclure des lettres majuscules, minuscules et des chiffres',
|
|
99
144
|
'pattern.password.veryStrong': 'Le mot de passe doit contenir au moins 10 caractères et inclure des lettres majuscules, minuscules, des chiffres et des caractères spéciaux',
|
|
145
|
+
'pattern.emailOrPhone': 'Doit être un e-mail ou un numéro de téléphone',
|
|
146
|
+
'pattern.usernameOrEmail': "Doit être un nom d'utilisateur ou un e-mail",
|
|
147
|
+
'pattern.httpOrHttps': 'Doit être une URL commençant par http ou https',
|
|
100
148
|
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
// oneOf (union de types) - v1.1.0
|
|
105
|
-
oneOf: '{{#label}} doit correspondre à l\'un des types suivants',
|
|
106
|
-
'oneOf.invalid': 'La valeur de {{#label}} ne correspond à aucun type autorisé',
|
|
149
|
+
// oneOf
|
|
150
|
+
oneOf: "{{#label}} doit correspondre à l'un des types suivants",
|
|
151
|
+
'oneOf.invalid': '{{#label}} ne correspond à aucun type autorisé',
|
|
107
152
|
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
153
|
+
// Error fallback
|
|
154
|
+
UNKNOWN_ERROR: 'Erreur de validation inconnue',
|
|
155
|
+
CUSTOM_VALIDATION_FAILED: 'Validation échouée',
|
|
156
|
+
ASYNC_VALIDATION_NOT_SUPPORTED: 'La validation asynchrone n\'est pas prise en charge dans validate() synchrone',
|
|
157
|
+
VALIDATE_MUST_BE_FUNCTION: 'validate doit être une fonction',
|
|
158
|
+
}
|
|
113
159
|
|
|
160
|
+
export default frFR
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { LocaleKey, LocaleMessage, LocaleMessages } from './types.js'
|
|
2
|
+
import zhCN from './zh-CN.js'
|
|
3
|
+
import enUS from './en-US.js'
|
|
4
|
+
import jaJP from './ja-JP.js'
|
|
5
|
+
import esES from './es-ES.js'
|
|
6
|
+
import frFR from './fr-FR.js'
|
|
7
|
+
|
|
8
|
+
export type { LocaleKey, LocaleMessage, LocaleMessages }
|
|
9
|
+
|
|
10
|
+
// ─── Supported locale registry ─────────────────────────────────────────────────────────
|
|
11
|
+
const LOCALES: Readonly<Record<string, LocaleMessages>> = {
|
|
12
|
+
'zh-CN': zhCN,
|
|
13
|
+
'en-US': enUS,
|
|
14
|
+
'ja-JP': jaJP,
|
|
15
|
+
'es-ES': esES,
|
|
16
|
+
'fr-FR': frFR,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Default locale (global fallback base) */
|
|
20
|
+
const DEFAULT_LOCALE = 'en-US'
|
|
21
|
+
|
|
22
|
+
/** en-US acts as secondary fallback (used when any locale is missing a key) */
|
|
23
|
+
const FALLBACK_LOCALE = 'en-US'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Look up a locale message entry (with fallback chain).
|
|
27
|
+
*
|
|
28
|
+
* Fallback chain: locale → en-US (if locale ≠ en-US) → key itself
|
|
29
|
+
*/
|
|
30
|
+
export function getMessage(
|
|
31
|
+
key: LocaleKey,
|
|
32
|
+
locale: string = DEFAULT_LOCALE
|
|
33
|
+
): LocaleMessage {
|
|
34
|
+
const messages = LOCALES[locale]
|
|
35
|
+
if (messages && key in messages) {
|
|
36
|
+
return messages[key]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Primary fallback: en-US
|
|
40
|
+
if (locale !== DEFAULT_LOCALE) {
|
|
41
|
+
const defaultMessages = LOCALES[DEFAULT_LOCALE]
|
|
42
|
+
if (defaultMessages && key in defaultMessages) {
|
|
43
|
+
return defaultMessages[key]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Secondary fallback: en-US
|
|
48
|
+
const fallbackMessages = LOCALES[FALLBACK_LOCALE]
|
|
49
|
+
if (fallbackMessages && key in fallbackMessages) {
|
|
50
|
+
return fallbackMessages[key]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Final fallback: return the key itself (never returns undefined)
|
|
54
|
+
return key
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Get the resolved string for a message (unwraps {code, message} object format).
|
|
59
|
+
*/
|
|
60
|
+
export function getMessageString(
|
|
61
|
+
key: LocaleKey,
|
|
62
|
+
locale: string = DEFAULT_LOCALE
|
|
63
|
+
): string {
|
|
64
|
+
const msg = getMessage(key, locale)
|
|
65
|
+
if (typeof msg === 'string') return msg
|
|
66
|
+
return msg.message
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get the error code of a message (if the value is a {code, message} object).
|
|
71
|
+
*/
|
|
72
|
+
export function getMessageCode(
|
|
73
|
+
key: LocaleKey,
|
|
74
|
+
locale: string = DEFAULT_LOCALE
|
|
75
|
+
): string | number | undefined {
|
|
76
|
+
const msg = getMessage(key, locale)
|
|
77
|
+
if (typeof msg === 'object') return msg.code
|
|
78
|
+
return undefined
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get the complete message map for the given locale.
|
|
83
|
+
*/
|
|
84
|
+
export function getMessages(locale: string = DEFAULT_LOCALE): LocaleMessages {
|
|
85
|
+
return LOCALES[locale] ?? LOCALES[DEFAULT_LOCALE]!
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check whether a locale is supported.
|
|
90
|
+
*/
|
|
91
|
+
export function isSupportedLocale(locale: string): boolean {
|
|
92
|
+
return locale in LOCALES
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get all supported locale codes.
|
|
97
|
+
*/
|
|
98
|
+
export function getSupportedLocales(): string[] {
|
|
99
|
+
return Object.keys(LOCALES)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { zhCN, enUS, jaJP, esES, frFR }
|
|
103
|
+
export default LOCALES
|