@@ -17,6 +17,7 @@ export type RootI18nDictionary = Record<string, PartialI18nDictionary>;
17
17
18
18
class Dictionary {
19
19
public locale : string ;
20
+ public fallbackLocale ?: string ;
20
21
21
22
private container : RootI18nDictionary ;
22
23
private interpolateOptions : InterpolateOptions ;
@@ -33,7 +34,19 @@ class Dictionary {
33
34
}
34
35
35
36
public resolve ( ctx : FieldValidationMetaInfo , interpolateOptions ?: InterpolateOptions ) {
36
- return this . format ( this . locale , ctx , interpolateOptions ) ;
37
+ let result = this . format ( this . locale , ctx , interpolateOptions ) ;
38
+ if ( ! result && this . fallbackLocale && this . fallbackLocale !== this . locale ) {
39
+ result = this . format ( this . fallbackLocale , ctx , interpolateOptions ) ;
40
+ }
41
+
42
+ return result || this . getDefaultMessage ( this . locale , ctx ) ;
43
+ }
44
+
45
+ public getDefaultMessage ( locale : string , ctx : FieldValidationMetaInfo ) {
46
+ const { label, name } = ctx ;
47
+ const fieldName = this . resolveLabel ( locale , name , label ) ;
48
+
49
+ return `${ fieldName } is not valid` ;
37
50
}
38
51
39
52
public getLocaleDefault ( locale : string , field : string ) : string | ValidationMessageGenerator | undefined {
@@ -54,7 +67,7 @@ class Dictionary {
54
67
const fieldName = this . resolveLabel ( locale , name , label ) ;
55
68
56
69
if ( ! rule ) {
57
- message = this . getLocaleDefault ( locale , name ) || ` ${ fieldName } is not valid` ;
70
+ message = this . getLocaleDefault ( locale , name ) || '' ;
58
71
return isCallable ( message )
59
72
? message ( ctx )
60
73
: interpolate ( message , { ...form , field : fieldName } , interpolateOptions ?? this . interpolateOptions ) ;
@@ -63,7 +76,7 @@ class Dictionary {
63
76
// find if specific message for that field was specified.
64
77
message = this . container [ locale ] ?. fields ?. [ name ] ?. [ rule . name ] || this . container [ locale ] ?. messages ?. [ rule . name ] ;
65
78
if ( ! message ) {
66
- message = this . getLocaleDefault ( locale , name ) || ` ${ fieldName } is not valid` ;
79
+ message = this . getLocaleDefault ( locale , name ) || '' ;
67
80
}
68
81
69
82
return isCallable ( message )
@@ -121,6 +134,13 @@ function setLocale(locale: string) {
121
134
DICTIONARY . locale = locale ;
122
135
}
123
136
137
+ /**
138
+ * Sets the fallback locale.
139
+ */
140
+ function setFallbackLocale ( locale : string ) {
141
+ DICTIONARY . fallbackLocale = locale ;
142
+ }
143
+
124
144
/**
125
145
* Loads a locale file from URL and merges it with the current dictionary
126
146
*/
@@ -143,4 +163,4 @@ async function loadLocaleFromURL(url: string) {
143
163
}
144
164
}
145
165
146
- export { localize , setLocale , loadLocaleFromURL } ;
166
+ export { localize , setLocale , loadLocaleFromURL , setFallbackLocale } ;
0 commit comments