@@ -102,41 +102,38 @@ export class Faker {
102
102
}
103
103
104
104
/**
105
- * Load the definitions contained in the locales file for the given types
105
+ * Load the definitions contained in the locales file for the given types.
106
+ *
107
+ * Background: Certain localization sets contain less data then others.
108
+ * In the case of a missing definition, use the localeFallback's values
109
+ * to substitute the missing data.
106
110
*/
107
111
private loadDefinitions ( ) : void {
108
112
// TODO @Shinigami 92 2022-01-11: Find a way to load this even more dynamically
109
113
// In a way so that we don't accidentally miss a definition
110
- Object . entries ( DEFINITIONS ) . forEach ( ( [ t , v ] ) => {
111
- if ( this . definitions [ t ] == null ) {
112
- this . definitions [ t ] = { } ;
114
+ for ( const [ moduleName , entryNames ] of Object . entries ( DEFINITIONS ) ) {
115
+ if ( typeof entryNames === 'string' ) {
116
+ // For 'title' and 'separator'
117
+ Object . defineProperty ( this . definitions , moduleName , {
118
+ get : ( ) : unknown /* string */ =>
119
+ this . locales [ this . locale ] [ moduleName ] ??
120
+ this . locales [ this . localeFallback ] [ moduleName ] ,
121
+ } ) ;
122
+ continue ;
113
123
}
114
124
115
- if ( typeof v === 'string' ) {
116
- this . definitions [ t ] = v ;
117
- return ;
125
+ if ( this . definitions [ moduleName ] == null ) {
126
+ this . definitions [ moduleName ] = { } ;
118
127
}
119
128
120
- v . forEach ( ( p ) => {
121
- Object . defineProperty ( this . definitions [ t ] , p , {
122
- get : ( ) => {
123
- if (
124
- this . locales [ this . locale ] [ t ] == null ||
125
- this . locales [ this . locale ] [ t ] [ p ] == null
126
- ) {
127
- // certain localization sets contain less data then others.
128
- // in the case of a missing definition, use the default localeFallback
129
- // to substitute the missing set data
130
- // throw new Error('unknown property ' + d + p)
131
- return this . locales [ this . localeFallback ] [ t ] [ p ] ;
132
- } else {
133
- // return localized data
134
- return this . locales [ this . locale ] [ t ] [ p ] ;
135
- }
136
- } ,
129
+ for ( const entryName of entryNames ) {
130
+ Object . defineProperty ( this . definitions [ moduleName ] , entryName , {
131
+ get : ( ) : unknown =>
132
+ this . locales [ this . locale ] [ moduleName ] ?. [ entryName ] ??
133
+ this . locales [ this . localeFallback ] [ moduleName ] ?. [ entryName ] ,
137
134
} ) ;
138
- } ) ;
139
- } ) ;
135
+ }
136
+ }
140
137
}
141
138
142
139
seed ( seed ?: number | number [ ] ) : void {
0 commit comments