@@ -17,6 +17,7 @@ export type RootI18nDictionary = Record<string, PartialI18nDictionary>;
1717
1818class Dictionary {
1919 public locale : string ;
20+ public fallbackLocale ?: string ;
2021
2122 private container : RootI18nDictionary ;
2223 private interpolateOptions : InterpolateOptions ;
@@ -33,7 +34,19 @@ class Dictionary {
3334 }
3435
3536 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` ;
3750 }
3851
3952 public getLocaleDefault ( locale : string , field : string ) : string | ValidationMessageGenerator | undefined {
@@ -54,7 +67,7 @@ class Dictionary {
5467 const fieldName = this . resolveLabel ( locale , name , label ) ;
5568
5669 if ( ! rule ) {
57- message = this . getLocaleDefault ( locale , name ) || ` ${ fieldName } is not valid` ;
70+ message = this . getLocaleDefault ( locale , name ) || '' ;
5871 return isCallable ( message )
5972 ? message ( ctx )
6073 : interpolate ( message , { ...form , field : fieldName } , interpolateOptions ?? this . interpolateOptions ) ;
@@ -63,7 +76,7 @@ class Dictionary {
6376 // find if specific message for that field was specified.
6477 message = this . container [ locale ] ?. fields ?. [ name ] ?. [ rule . name ] || this . container [ locale ] ?. messages ?. [ rule . name ] ;
6578 if ( ! message ) {
66- message = this . getLocaleDefault ( locale , name ) || ` ${ fieldName } is not valid` ;
79+ message = this . getLocaleDefault ( locale , name ) || '' ;
6780 }
6881
6982 return isCallable ( message )
@@ -121,6 +134,13 @@ function setLocale(locale: string) {
121134 DICTIONARY . locale = locale ;
122135}
123136
137+ /**
138+ * Sets the fallback locale.
139+ */
140+ function setFallbackLocale ( locale : string ) {
141+ DICTIONARY . fallbackLocale = locale ;
142+ }
143+
124144/**
125145 * Loads a locale file from URL and merges it with the current dictionary
126146 */
@@ -143,4 +163,4 @@ async function loadLocaleFromURL(url: string) {
143163 }
144164}
145165
146- export { localize , setLocale , loadLocaleFromURL } ;
166+ export { localize , setLocale , loadLocaleFromURL , setFallbackLocale } ;
0 commit comments