@@ -29,146 +29,136 @@ export class Localization {
29
29
return new Localization ( locale , bundle ) ;
30
30
}
31
31
32
+ /**
33
+ * @private
34
+ * @param {string } messageId
35
+ * @param {Record<string, FluentVariable> } [args]
36
+ * @returns {string }
37
+ */
38
+ _formatMessage ( messageId , args ) {
39
+ const message = this . bundle . getMessage ( messageId ) ;
40
+ if ( ! message ?. value ) {
41
+ return `Localization error: message '${ messageId } ' not found.` ;
42
+ }
43
+ return this . bundle . formatPattern ( message . value , args ) ;
44
+ }
45
+
32
46
/** @type (expectedTypes: string | string[], actualType: string) => string */
33
47
getTypeErrorMessage ( expectedTypes , actualType ) {
34
- const message = /** @type Message */ ( this . bundle . getMessage ( "type-error" ) ) ;
35
-
36
- if ( typeof expectedTypes === "string" ) {
37
- expectedTypes = [ expectedTypes ] ;
38
- }
48
+ const types = Array . isArray ( expectedTypes ) ? expectedTypes : [ expectedTypes ] ;
49
+ const expected = new Intl . ListFormat ( this . locale , { type : "disjunction" } ) . format (
50
+ types . map ( ( type ) => JSON . stringify ( type ) )
51
+ ) ;
39
52
40
- const expected = expectedTypes . map ( ( type ) => JSON . stringify ( type ) ) ;
41
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , {
42
- expected : new Intl . ListFormat ( this . locale , { type : "disjunction" } ) . format ( expected ) ,
53
+ return this . _formatMessage ( "type-error" , {
54
+ expected,
43
55
actual : JSON . stringify ( actualType )
44
56
} ) ;
45
57
}
46
58
47
59
/** @type (limit: number) => string */
48
60
getMinLengthErrorMessage ( limit ) {
49
- const message = /** @type Message */ ( this . bundle . getMessage ( "min-length-error" ) ) ;
50
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
61
+ return this . _formatMessage ( "min-length-error" , { limit } ) ;
51
62
}
52
63
53
64
/** @type (limit: number) => string */
54
65
getMaxLengthErrorMessage ( limit ) {
55
- const message = /** @type Message */ ( this . bundle . getMessage ( "max-length-error" ) ) ;
56
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
66
+ return this . _formatMessage ( "max-length-error" , { limit } ) ;
57
67
}
58
68
59
69
/** @type (limit: number) => string */
60
70
getMaximumErrorMessage ( limit ) {
61
- const message = /** @type Message */ ( this . bundle . getMessage ( "maximum-error" ) ) ;
62
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
71
+ return this . _formatMessage ( "maximum-error" , { limit } ) ;
63
72
}
64
73
65
74
/** @type (limit: number) => string */
66
75
getMinimumErrorMessage ( limit ) {
67
- const message = /** @type Message */ ( this . bundle . getMessage ( "minimum-error" ) ) ;
68
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
76
+ return this . _formatMessage ( "minimum-error" , { limit } ) ;
69
77
}
70
78
71
79
/** @type (limit: number) => string */
72
80
getExclusiveMinimumErrorMessage ( limit ) {
73
- const message = /** @type Message */ ( this . bundle . getMessage ( "exclusive-minimum-error" ) ) ;
74
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
81
+ return this . _formatMessage ( "exclusive-minimum-error" , { limit } ) ;
75
82
}
76
83
77
84
/** @type (limit: number) => string */
78
85
getExclusiveMaximumErrorMessage ( limit ) {
79
- const message = /** @type Message */ ( this . bundle . getMessage ( "exclusive-maximum-error" ) ) ;
80
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
86
+ return this . _formatMessage ( "exclusive-maximum-error" , { limit } ) ;
81
87
}
82
88
83
- /** @type (instanceLocation: string, missingProperties: string | string []) => string */
89
+ /** @type (instanceLocation: string, missingProperties: string[]) => string */
84
90
getRequiredErrorMessage ( instanceLocation , missingProperties ) {
85
- const requiredList = new Intl . ListFormat ( this . locale , { type : "conjunction" } ) . format ( missingProperties ) ;
86
- const message = /** @type Message */ ( this . bundle . getMessage ( "required-error" ) ) ;
87
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , {
91
+ return this . _formatMessage ( "required-error" , {
88
92
instanceLocation,
89
- missingProperties : requiredList
93
+ missingProperties : new Intl . ListFormat ( this . locale , { type : "conjunction" } ) . format ( missingProperties )
90
94
} ) ;
91
95
}
92
96
93
97
/** @type (divisor: number) => string */
94
98
getMultipleOfErrorMessage ( divisor ) {
95
- const message = /** @type Message */ ( this . bundle . getMessage ( "multiple-of-error" ) ) ;
96
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { divisor } ) ;
99
+ return this . _formatMessage ( "multiple-of-error" , { divisor } ) ;
97
100
}
98
101
99
102
/** @type (limit: number) => string */
100
103
getMaxPropertiesErrorMessage ( limit ) {
101
- const message = /** @type Message */ ( this . bundle . getMessage ( "max-properties-error" ) ) ;
102
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
104
+ return this . _formatMessage ( "max-properties-error" , { limit } ) ;
103
105
}
104
106
105
107
/** @type (limit: number) => string */
106
108
getMinPropertiesErrorMessage ( limit ) {
107
- const message = /** @type Message */ ( this . bundle . getMessage ( "min-properties-error" ) ) ;
108
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
109
+ return this . _formatMessage ( "min-properties-error" , { limit } ) ;
109
110
}
110
111
111
112
/** @type (expectedValue: FluentVariable) => string */
112
113
getConstErrorMessage ( expectedValue ) {
113
- const message = /** @type Message */ ( this . bundle . getMessage ( "const-error" ) ) ; // type doubt here
114
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { expectedValue } ) ;
114
+ return this . _formatMessage ( "const-error" , { expectedValue } ) ;
115
115
}
116
116
117
117
/** @type (limit: number) => string */
118
118
getMaxItemsErrorMessage ( limit ) {
119
- const message = /** @type Message */ ( this . bundle . getMessage ( "max-items-error" ) ) ;
120
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
119
+ return this . _formatMessage ( "max-items-error" , { limit } ) ;
121
120
}
122
121
123
122
/** @type (limit: number) => string */
124
123
getMinItemsErrorMessage ( limit ) {
125
- const message = /** @type Message */ ( this . bundle . getMessage ( "min-items-error" ) ) ;
126
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { limit } ) ;
124
+ return this . _formatMessage ( "min-items-error" , { limit } ) ;
127
125
}
128
126
129
127
/** @type () => string */
130
128
getUniqueItemsErrorMessage ( ) {
131
- const message = /** @type Message */ ( this . bundle . getMessage ( "unique-items-error" ) ) ;
132
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) ) ;
129
+ return this . _formatMessage ( "unique-items-error" ) ;
133
130
}
134
131
135
132
/** @type (format: string) => string */
136
133
getFormatErrorMessage ( format ) {
137
- const message = /** @type Message */ ( this . bundle . getMessage ( "format-error" ) ) ;
138
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { format } ) ;
134
+ return this . _formatMessage ( "format-error" , { format } ) ;
139
135
}
140
136
141
137
/** @type (pattern: string) => string */
142
138
getPatternErrorMessage ( pattern ) {
143
- const message = /** @type Message */ ( this . bundle . getMessage ( "pattern-error" ) ) ;
144
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { pattern } ) ;
139
+ return this . _formatMessage ( "pattern-error" , { pattern } ) ;
145
140
}
146
141
147
142
/** @type () => string */
148
143
getContainsErrorMessage ( ) {
149
- const message = /** @type Message */ ( this . bundle . getMessage ( "contains-error" ) ) ;
150
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) ) ;
144
+ return this . _formatMessage ( "contains-error" ) ;
151
145
}
152
146
153
147
/** @type () => string */
154
148
getNotErrorMessage ( ) {
155
- const message = /** @type Message */ ( this . bundle . getMessage ( "not-error" ) ) ;
156
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) ) ;
149
+ return this . _formatMessage ( "not-error" ) ;
157
150
}
158
151
159
152
/** @type (propertyName: string) => string */
160
153
getAdditionalPropertiesErrorMessage ( propertyName ) {
161
- const message = /** @type Message */ ( this . bundle . getMessage ( "additional-properties-error" ) ) ;
162
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , { propertyName } ) ;
154
+ return this . _formatMessage ( "additional-properties-error" , { propertyName } ) ;
163
155
}
164
156
165
- /** @type (property: string, missingDependents: string | string []) => string */
157
+ /** @type (property: string, missingDependents: string[]) => string */
166
158
getDependentRequiredErrorMessage ( property , missingDependents ) {
167
- const dependentsList = new Intl . ListFormat ( this . locale , { type : "conjunction" } ) . format ( missingDependents ) ;
168
- const message = /** @type Message */ ( this . bundle . getMessage ( "dependent-required-error" ) ) ;
169
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , {
159
+ return this . _formatMessage ( "dependent-required-error" , {
170
160
property,
171
- missingDependents : dependentsList
161
+ missingDependents : new Intl . ListFormat ( this . locale , { type : "conjunction" } ) . format ( missingDependents )
172
162
} ) ;
173
163
}
174
164
@@ -191,21 +181,20 @@ export class Localization {
191
181
* @returns {string }
192
182
*/
193
183
getEnumErrorMessage ( args ) {
194
- const message = /** @type Message */ ( this . bundle . getMessage ( "enum-error" ) ) ;
184
+ const formattedArgs = {
185
+ variant : args . variant ,
186
+ instanceValue : `"${ args . instanceValue } "` ,
187
+ suggestion : "" ,
188
+ allowedValues : ""
189
+ } ;
190
+
195
191
if ( args . variant === "fallback" ) {
196
192
const quotedValues = args . allowedValues . map ( ( value ) => JSON . stringify ( value ) ) ;
197
- const formattedList = new Intl . ListFormat ( this . locale , { type : "disjunction" } ) . format ( quotedValues ) ;
198
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , {
199
- variant : "fallback" ,
200
- instanceValue : `"${ args . instanceValue } "` ,
201
- allowedValues : formattedList
202
- } ) ;
193
+ formattedArgs . allowedValues = new Intl . ListFormat ( this . locale , { type : "disjunction" } ) . format ( quotedValues ) ;
203
194
} else {
204
- return this . bundle . formatPattern ( /** @type Pattern */ ( message . value ) , {
205
- variant : "suggestion" ,
206
- instanceValue : `"${ args . instanceValue } "` ,
207
- suggestion : args . suggestion
208
- } ) ;
195
+ formattedArgs . suggestion = args . suggestion ;
209
196
}
197
+
198
+ return this . _formatMessage ( "enum-error" , formattedArgs ) ;
210
199
}
211
200
}
0 commit comments