x4js 2.3.19 → 2.3.21

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.
@@ -85,7 +85,12 @@ export function addTranslation( name: string, ...parts: any[] ) {
85
85
  _patch( lang.src_translations, p );
86
86
  } );
87
87
 
88
- lang.translations = _proxyfy( lang.src_translations, lang.base, true );
88
+ lang.translations = _proxyfy( lang.src_translations, lang.base, [] );
89
+
90
+ // the current language: _tr sees the new translations at once
91
+ if( name==current.name ) {
92
+ current.translations = lang.translations;
93
+ }
89
94
  }
90
95
 
91
96
  /**
@@ -117,96 +122,71 @@ function _patch( obj: any, by: any ) {
117
122
  /**
118
123
  * create a proxy for all sub objects
119
124
  * (deep traverse)
125
+ * @param path where obj is, from _tr: [] for _tr itself, ["global"] for _tr.global
120
126
  */
121
127
 
122
- function _proxyfy( obj: any, base: any, root: any ) {
128
+ function _proxyfy( obj: any, base: string, path: string[] ) {
123
129
 
124
130
  const result: any = {}
125
131
 
126
132
  for( const n in obj ) {
127
133
  if( typeof obj[n]!=="string" && !Array.isArray(obj[n])) {
128
- result[n] = _proxyfy( obj[n], base, false );
134
+ result[n] = _proxyfy( obj[n], base, [...path, n] );
129
135
  }
130
136
  else {
131
137
  result[n] = obj[n];
132
138
  }
133
139
  }
134
140
 
135
- return _mk_proxy( result, base, root );
141
+ return _mk_proxy( result, base, path );
136
142
  }
137
143
 
138
144
 
139
145
  /**
140
146
  * create a proxy for the given object
147
+ * what is not found is asked to the base language, at the same path
141
148
  */
142
149
 
143
- function _mk_proxy( obj: any, base: string, root: boolean ) : any {
150
+ function _mk_proxy( obj: any, base: string, path: string[] ) : any {
144
151
  return new Proxy( obj, {
145
152
  get: (target, prop) => {
146
- if( root ) {
147
- req_path = [prop];
148
- }
149
- else {
150
- req_path.push( prop );
153
+ const value = target[prop];
154
+
155
+ // a symbol is not a translation
156
+ if( value!==undefined || typeof prop==="symbol" ) {
157
+ return value;
151
158
  }
152
159
 
153
- let value = target[prop];
154
- if( value===undefined ) {
155
- if( base ) {
156
- value = _findBaseTrans( base );
157
- }
160
+ const asked = [...path, prop];
158
161
 
159
- if( value===undefined ) {
160
- console.error( "I18N error: unable to find", '_tr.'+req_path.join('.') );
161
- }
162
+ // not in this language: in its base, which looks in its own base
163
+ if( base ) {
164
+ return _findBaseTrans( base, asked );
162
165
  }
163
166
 
164
- return value;
167
+ console.error( "I18N error: unable to find", '_tr.'+asked.join('.') );
168
+ return undefined;
165
169
  }
166
170
  });
167
171
  }
168
172
 
169
-
170
173
  /**
171
- * when we ask for _tr.xxx
172
- * reqpath is set to [xxx]
173
- *
174
- * then when we try to get _tr.xxx.yyy
175
- * reqpath is [xxx,yyy]
176
- * if yyy is not found, we try with base langage for the full reqpath
177
- * until no base found
174
+ * the translation at path in the language base
175
+ * its proxies look in their own base: the last language of the chain tells what is missing
178
176
  */
179
177
 
180
- let req_path: (string | symbol)[];
178
+ function _findBaseTrans( base: string, path: string[] ) {
181
179
 
182
- /**
183
- *
184
- */
185
-
186
- function _findBaseTrans( base: any ) {
187
-
188
- while( base ) {
189
- const lang = languages[base];
190
- let trans = lang.translations;
191
- let value;
192
-
193
- for( const p of req_path ) {
194
- value = trans[p];
195
- if( value===undefined ) {
196
- break;
197
- }
180
+ let trans = languages[base].translations;
198
181
 
199
- trans = value;
200
- }
201
-
202
- if( value!==undefined ) {
203
- return trans;
182
+ for( const p of path ) {
183
+ trans = trans[p];
184
+ if( trans===undefined ) {
185
+ return undefined;
204
186
  }
205
-
206
- base = lang.base;
207
187
  }
208
188
 
209
- return undefined;
189
+ return trans;
210
190
  }
211
191
 
212
192
  /**
@@ -320,7 +300,7 @@ let fr = {
320
300
 
321
301
  diff_date_seconds: '{0} secondes',
322
302
  diff_date_minutes: '{0} minutes',
323
- diff_date_hours: '{0} heures',
303
+ diff_date_hours: '{0} heures {1} minutes',
324
304
 
325
305
  invalid_date: 'Date non reconnue ({0})',
326
306
  empty_list: 'Liste vide',
@@ -385,7 +365,7 @@ let en = {
385
365
 
386
366
  diff_date_seconds: '{0} seconds',
387
367
  diff_date_minutes: '{0} minutes',
388
- diff_date_hours: '{0} hours',
368
+ diff_date_hours: '{0} hours {1} minutes',
389
369
 
390
370
  invalid_date: 'Unrecognized date({0})',
391
371
  empty_list: 'Empty list',