@esfaenza/localizations 11.2.9 → 11.2.10

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.
@@ -1,11 +1,10 @@
1
- import { InjectionToken, Injectable, Inject, Pipe, Optional, NgModule } from '@angular/core';
1
+ import { __awaiter } from 'tslib';
2
+ import * as i0 from '@angular/core';
3
+ import { InjectionToken, Injectable, Inject, Injector, Pipe, Optional, NgModule } from '@angular/core';
4
+ import { take, first } from 'rxjs/operators';
5
+ import { ReplaySubject, of } from 'rxjs';
2
6
  import { DateService } from '@esfaenza/extensions';
3
7
 
4
- /**
5
- * Locale da utilizzare per le traduzioni
6
- */
7
- const LOC_LOCALE = new InjectionToken('LOC_LOCALE');
8
-
9
8
  /**
10
9
  * Contenitore delle localizzazioni collegate ad un testo "base", una per ogni linguaggio supportato
11
10
  */
@@ -95,77 +94,13 @@ class SpecializedLocalization {
95
94
  }
96
95
  }
97
96
 
98
- /**
99
- * Classe singleton che contiene le traduzioni di base (per evitare di ripeterle in oggi classe di traduzione) e le funzioni base per
100
- * inserire e leggere traduzioni in/da una cache
101
- */
102
- class BaseLocalization {
103
- /**
104
- * Costruttore. Da qui utilizzando il metodo statico **BaseLocalization.set** si va a popolare la cache delle traduzioni **BaseTranslations**
105
- */
106
- constructor() {
107
- /**
108
- * Cache di tutte le localizzazioni di base
109
- */
110
- this.BaseTranslations = {};
111
- // Odio sprecare caratteri... soprattutto quando rischio che non vengano minificati
112
- let bt = this.BaseTranslations;
113
- let bl = BaseLocalization;
114
- bl.set(bt, "en->it", "Remove", ["Elimina"]);
115
- bl.set(bt, "en->it", "Paging", ["Paginazione"]);
116
- bl.set(bt, "en->it", "Yes", ["Si"]);
117
- bl.set(bt, "en->it", "No", ["No"]);
118
- bl.set(bt, "en->it", "YES", ["SI'"]);
119
- bl.set(bt, "en->it", "NO", ["NO"]);
120
- bl.set(bt, "en->it", "Found objects", ["Risultati trovati"]);
121
- bl.set(bt, "en->it", "Loading data...", ["Carico i dati..."]);
122
- bl.set(bt, "en->it", "Error during save operations", ["Errori durante il salvataggio"]);
123
- bl.set(bt, "en->it", "Error", ["Errore"]);
124
- bl.set(bt, "en->it", "Warning", ["Avviso"]);
125
- bl.set(bt, "en->it", "Information", ["Informazione"]);
126
- bl.set(bt, "en->it", "Save", ["Salva"]);
127
- bl.set(bt, "en->it", "Back", ["Indietro"]);
128
- bl.set(bt, "en->it", "From", ["Da"]);
129
- bl.set(bt, "en->it", "To", ["A"]);
130
- bl.set(bt, "en->it", "View Chart", ["Vedi Grafico"]);
131
- bl.set(bt, "en->it", "View Table", ["Vedi Tabella"]);
132
- bl.set(bt, "en->it", "Notes", ["Note"]);
133
- bl.set(bt, "en->it", "Edit Notes", ["Modifica Note"]);
134
- bl.set(bt, "en->it", "Cancel", ["Annulla"]);
135
- bl.set(bt, "en->it", "Confirm", ["Conferma"]);
136
- bl.set(bt, "en->it", "View", ["Visualizza"]);
137
- bl.set(bt, "en->it", "Close", ["Chiudi"]);
138
- bl.set(bt, "en->it", "Select", ["Seleziona"]);
139
- bl.set(bt, "en->it", "Meter Reading registered successfully", ["Lettura registrata correttamente"]);
140
- bl.set(bt, "en->it", "Validation errors during save operations", ["Errori di validazione durante il salvataggio"]);
141
- bl.set(bt, "en->it", "Can't register reading", ["Impossibile registrare la lettura"]);
142
- bl.set(bt, "en->it", "Electricity", ["Elettricita"]);
143
- bl.set(bt, "en->it", "Gas", ["Gas"]);
144
- bl.set(bt, "en->it", "Water", ["Acqua"]);
145
- bl.set(bt, "en->it", "Delete", ["Elimina"]);
146
- bl.set(bt, "en->it", "This operation cannot be reverted", ["Questa operazione non puo' essere annullata"]);
147
- bl.set(bt, "en->it", "Hide", ["Nascondi"]);
148
- // Helper validazione
149
- bl.set(bt, "en->it", "Required field", ["Campo richiesto"]);
150
- bl.set(bt, "en->it", "Wrong field format", ["Formato campo errato"]);
151
- bl.set(bt, "en->it", "Wrong field format or field missing", ["Formato campo errato o campo mancante"]);
152
- bl.set(bt, "en->it", "You don't have the required permissions to view this page. The navigation has been blocked", ["Non si hanno privilegi a sufficienza per accedere a questa pagina. La navigazione e' stata bloccata"]);
153
- // Informazioni sulle date
154
- bl.setToken(bt, "en->it", "getDateMonthPosition", "0", ["1"]);
155
- bl.setToken(bt, "en->it", "getDisplayDateMonthPosition", "1", ["1"]);
156
- bl.setToken(bt, "en->it", "getLocale", "en-US", ["it-IT"]);
157
- bl.setToken(bt, "en->it", "getSmallDateFormat", "MM/DD/YYYY", ["DD/MM/YYYY"]);
158
- bl.setToken(bt, "en->it", "getDefaultDateFormat", "MM/DD/YYYY", ["MM/DD/YYYY"]);
159
- bl.setToken(bt, "en->it", "getSmallDateDisplayFormat", "DD/MM/YYYY", ["DD/MM/YYYY"]);
160
- bl.setToken(bt, "en->it", "getFullDateFormat", "MM/DD/YYYY HH:mm:ss", ["DD/MM/YYYY HH:mm:ss"]);
161
- bl.setToken(bt, "en->it", "getFullDateDisplayFormat", "DD/MM/YYYY HH:mm:ss", ["DD/MM/YYYY HH:mm:ss"]);
162
- bl.setToken(bt, "en->it", "getFullDateFormatNoSeconds", "MM/DD/YYYY HH:mm", ["DD/MM/YYYY HH:mm"]);
163
- bl.setToken(bt, "en->it", "getFullDateDisplayFormatNoSeconds", "DD/MM/YYYY HH:mm", ["DD/MM/YYYY HH:mm"]);
97
+ class LocalizationPersistor {
98
+ constructor(LocCache = null) {
99
+ this.LocCache = LocCache;
164
100
  }
165
101
  /**
166
102
  * Imposta la localizzazione di un token all'interno della cache scelta
167
103
  *
168
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno della quale inserire l'informazione del token con le localizzazioni
169
104
  * @param {string} direction Direzione della traduzione, rappresenta la lingua base in cui aspettarsi il testo e le varie lingue in cui viene tradotta.
170
105
  * Ad esempio, "en->it" indica che il parametro **left** è valido per le localizzazioni in inglese, mentre nell'elemento 0 del parametro **right**
171
106
  * è presente l'unica traduzione in italiano, per definire più traduzioni sul lato destro della freccia bisogna concatenarle con la virgola, ad esempio "en->it,jp,ecc"
@@ -173,56 +108,54 @@ class BaseLocalization {
173
108
  * @param {string} left Valore del token per la lingua specificata nel lato immediatamente sinistro alla freccia del parametro **direction**
174
109
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
175
110
  */
176
- static setToken(locCache, direction, token, left, right) {
111
+ setToken(direction, token, left, right) {
177
112
  let parsingRegex = /([a-zA-Z]+)[\s]*->[\s]*([a-zA-Z,\s]+)+/g;
178
113
  var parsingRes = parsingRegex.exec(direction);
179
114
  let from = parsingRes[1];
180
115
  let to = parsingRes[2].replace(/\s/g, '').split(',');
181
116
  let localeFrom = LocaleDefinitions.getByAlias(from);
182
- BaseLocalization._set(locCache, token, localeFrom.id, left, true);
117
+ this._set(token, localeFrom.id, left, true);
183
118
  to.forEach((t, index) => {
184
119
  let localeTo = LocaleDefinitions.getByAlias(t);
185
- BaseLocalization._set(locCache, token, localeTo.id, right[index], true);
120
+ this._set(token, localeTo.id, right[index], true);
186
121
  });
187
122
  }
188
123
  /**
189
124
  * Imposta la localizzazione di una frase all'interno della cache scelta
190
125
  *
191
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno della quale inserire l'informazione della frase con le localizzazioni
192
126
  * @param {string} direction Direzione della traduzione, rappresenta la lingua base in cui aspettarsi il testo e le varie lingue in cui viene tradotta.
193
127
  * Ad esempio, "en->it" indica che il parametro **left** è valido per le localizzazioni in inglese, mentre nell'elemento 0 del parametro **right**
194
128
  * è presente l'unica traduzione in italiano, per definire più traduzioni sul lato destro della freccia bisogna concatenarle con la virgola, ad esempio "en->it,jp,ecc"
195
129
  * @param {string} left Valore della frase per la lingua specificata nel lato immediatamente sinistro alla freccia del parametro **direction**
196
130
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
197
131
  */
198
- static set(locCache, direction, left, right) {
132
+ set(direction, left, right) {
199
133
  let parsingRegex = /([a-zA-Z]+)[\s]*->[\s]*([a-zA-Z,\s]+)+/g;
200
134
  var parsingRes = parsingRegex.exec(direction);
201
135
  let from = parsingRes[1];
202
136
  let to = parsingRes[2].replace(/\s/g, '').split(',');
203
137
  let localeFrom = LocaleDefinitions.getByAlias(from);
204
- BaseLocalization._set(locCache, left, localeFrom.id, left);
138
+ this._set(left, localeFrom.id, left);
205
139
  to.forEach((t, index) => {
206
140
  let localeTo = LocaleDefinitions.getByAlias(t);
207
- BaseLocalization._set(locCache, left, localeTo.id, right[index]);
141
+ this._set(left, localeTo.id, right[index]);
208
142
  });
209
143
  }
210
144
  /**
211
145
  * Imposta all'interno della cache specificata un'unica traduzione partendo da un testo di base
212
146
  *
213
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno della quale inserire l'informazione del testo con le localizzazioni
214
147
  * @param {string} text Testo da tradurre
215
148
  * @param {string} culture Lingua per cui inserire la traduzione
216
149
  * @param {string} translation Traduzione del **text** nella lingua specificata da **culture**
217
150
  * @param {boolean} token Indica se il **text** è un token o è una frase normale
218
151
  */
219
- static _set(locCache, text, culture, translation, token) {
220
- let item = BaseLocalization.findByText(locCache, text, token);
152
+ _set(text, culture, translation, token) {
153
+ let item = this.get(text, token);
221
154
  if (!item) {
222
- locCache[text] = new ItemLocalization(token, text, [new SpecializedLocalization(culture, translation)]);
155
+ this.LocCache[text] = new ItemLocalization(token, text, [new SpecializedLocalization(culture, translation)]);
223
156
  return;
224
157
  }
225
- var trs = BaseLocalization.translationByCulture(item, culture);
158
+ var trs = this.translationByCulture(item, culture);
226
159
  if (trs) {
227
160
  console.warn("Localization already loaded for sentence '" + text + "' and culture " + culture);
228
161
  return;
@@ -232,15 +165,14 @@ class BaseLocalization {
232
165
  /**
233
166
  * Partendo da una cache di localizzazione, dato un testo che può essere o meno un token, cerca quell'elemento all'interno della cache
234
167
  *
235
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno del quale cercare il **text**
236
- * @param {string} text Testo da cercare nella **locCache**
168
+ * @param {string} text Testo da cercare nella **LocCache**
237
169
  * @param {boolean} allowtokens Indica se l'elemento cercato può essere un token o se dev'essere per forza una frase (per evitare rari
238
170
  * casi di collisione in cui magari un token è uguale a una frase tradotta)
239
171
  *
240
172
  * @returns {ItemLocalization} Informazioni di localizzazione per il **text** cercato
241
173
  */
242
- static findByText(locCache, text, allowtokens) {
243
- let possibleItem = locCache[text];
174
+ get(text, allowtokens) {
175
+ let possibleItem = this.LocCache[text];
244
176
  if (!possibleItem)
245
177
  return null;
246
178
  var item = allowtokens || !possibleItem.token ? possibleItem : null;
@@ -254,7 +186,7 @@ class BaseLocalization {
254
186
  *
255
187
  * @returns {SpecializedLocalization} Informazioni di localizzazione specifiche della lingua richiesta
256
188
  */
257
- static translationByCulture(item, culture) {
189
+ translationByCulture(item, culture) {
258
190
  for (let i = 0; i < item.translations.length; i++) {
259
191
  let trl = item.translations[i];
260
192
  if (trl.culture == culture)
@@ -262,11 +194,122 @@ class BaseLocalization {
262
194
  }
263
195
  return null;
264
196
  }
197
+ }
198
+
199
+ var BaseLocalizationTokens;
200
+ (function (BaseLocalizationTokens) {
201
+ BaseLocalizationTokens["getDateMonthPosition"] = "getDateMonthPosition";
202
+ BaseLocalizationTokens["getDisplayDateMonthPosition"] = "getDisplayDateMonthPosition";
203
+ BaseLocalizationTokens["getLocale"] = "getLocale";
204
+ BaseLocalizationTokens["getSmallDateFormat"] = "getSmallDateFormat";
205
+ BaseLocalizationTokens["getDefaultDateFormat"] = "getDefaultDateFormat";
206
+ BaseLocalizationTokens["getSmallDateDisplayFormat"] = "getSmallDateDisplayFormat";
207
+ BaseLocalizationTokens["getFullDateFormat"] = "getFullDateFormat";
208
+ BaseLocalizationTokens["getFullDateDisplayFormat"] = "getFullDateDisplayFormat";
209
+ BaseLocalizationTokens["getFullDateFormatNoSeconds"] = "getFullDateFormatNoSeconds";
210
+ BaseLocalizationTokens["getFullDateDisplayFormatNoSeconds"] = "getFullDateDisplayFormatNoSeconds";
211
+ })(BaseLocalizationTokens || (BaseLocalizationTokens = {}));
212
+
213
+ /** Classe astratta che rappresenta lo stato di persistenza fra la libreria e uno storage */
214
+ class LocalePersistor {
215
+ constructor() {
216
+ /** Chiave su cui salvare il locale */
217
+ this.LOCALE = "LOCALE";
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Locale da utilizzare per le traduzioni
223
+ */
224
+ const LOC_LOCALE = new InjectionToken('LOC_LOCALE');
225
+
226
+ /**
227
+ * Classe singleton che contiene le traduzioni di base (per evitare di ripeterle in oggi classe di traduzione) e le funzioni base per
228
+ * inserire e leggere traduzioni in/da una cache
229
+ */
230
+ class BaseLocalization {
231
+ /**
232
+ * Costruttore. Da qui utilizzando il metodo statico **BaseLocalization.set** si va a popolare la cache delle traduzioni **BaseTranslations**
233
+ */
234
+ constructor(LocalePersistor, defaultLocale) {
235
+ this.LocalePersistor = LocalePersistor;
236
+ /**
237
+ * Cache di tutte le localizzazioni di base
238
+ */
239
+ this.LocCache = {};
240
+ /**
241
+ * Locale di riferimento per tutte le traduzioni
242
+ */
243
+ this.Locale = new ReplaySubject(1);
244
+ // Recupero il locale alla partenza del BaseLocalization in modo, spero di dare tempo all'applicazione di tirarlo su
245
+ this.LocalePersistor.recover().pipe(take(1)).subscribe((l) => __awaiter(this, void 0, void 0, function* () {
246
+ if (!l) {
247
+ l = defaultLocale;
248
+ this.LocalePersistor.persist(defaultLocale);
249
+ }
250
+ this.Locale.next(l);
251
+ }));
252
+ this.Persistor = new LocalizationPersistor(this.LocCache);
253
+ this.Persistor.set("en->it", "Remove", ["Elimina"]);
254
+ this.Persistor.set("en->it", "Paging", ["Paginazione"]);
255
+ this.Persistor.set("en->it", "Yes", ["Si"]);
256
+ this.Persistor.set("en->it", "No", ["No"]);
257
+ this.Persistor.set("en->it", "YES", ["SI'"]);
258
+ this.Persistor.set("en->it", "NO", ["NO"]);
259
+ this.Persistor.set("en->it", "Found objects", ["Risultati trovati"]);
260
+ this.Persistor.set("en->it", "Loading data...", ["Carico i dati..."]);
261
+ this.Persistor.set("en->it", "Error during save operations", ["Errori durante il salvataggio"]);
262
+ this.Persistor.set("en->it", "Error", ["Errore"]);
263
+ this.Persistor.set("en->it", "Warning", ["Avviso"]);
264
+ this.Persistor.set("en->it", "Information", ["Informazione"]);
265
+ this.Persistor.set("en->it", "Save", ["Salva"]);
266
+ this.Persistor.set("en->it", "Back", ["Indietro"]);
267
+ this.Persistor.set("en->it", "From", ["Da"]);
268
+ this.Persistor.set("en->it", "To", ["A"]);
269
+ this.Persistor.set("en->it", "View Chart", ["Vedi Grafico"]);
270
+ this.Persistor.set("en->it", "View Table", ["Vedi Tabella"]);
271
+ this.Persistor.set("en->it", "Notes", ["Note"]);
272
+ this.Persistor.set("en->it", "Edit Notes", ["Modifica Note"]);
273
+ this.Persistor.set("en->it", "Cancel", ["Annulla"]);
274
+ this.Persistor.set("en->it", "Confirm", ["Conferma"]);
275
+ this.Persistor.set("en->it", "View", ["Visualizza"]);
276
+ this.Persistor.set("en->it", "Close", ["Chiudi"]);
277
+ this.Persistor.set("en->it", "Select", ["Seleziona"]);
278
+ this.Persistor.set("en->it", "Meter Reading registered successfully", ["Lettura registrata correttamente"]);
279
+ this.Persistor.set("en->it", "Validation errors during save operations", ["Errori di validazione durante il salvataggio"]);
280
+ this.Persistor.set("en->it", "Can't register reading", ["Impossibile registrare la lettura"]);
281
+ this.Persistor.set("en->it", "Electricity", ["Elettricita"]);
282
+ this.Persistor.set("en->it", "Gas", ["Gas"]);
283
+ this.Persistor.set("en->it", "Water", ["Acqua"]);
284
+ this.Persistor.set("en->it", "Delete", ["Elimina"]);
285
+ this.Persistor.set("en->it", "This operation cannot be reverted", ["Questa operazione non puo' essere annullata"]);
286
+ this.Persistor.set("en->it", "Hide", ["Nascondi"]);
287
+ // Helper validazione
288
+ this.Persistor.set("en->it", "Required field", ["Campo richiesto"]);
289
+ this.Persistor.set("en->it", "Wrong field format", ["Formato campo errato"]);
290
+ this.Persistor.set("en->it", "Wrong field format or field missing", ["Formato campo errato o campo mancante"]);
291
+ this.Persistor.set("en->it", "You don't have the required permissions to view this page. The navigation has been blocked", ["Non si hanno privilegi a sufficienza per accedere a questa pagina. La navigazione e' stata bloccata"]);
292
+ // Informazioni sulle date
293
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getDateMonthPosition, "0", ["1"]);
294
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getDisplayDateMonthPosition, "1", ["1"]);
295
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getLocale, "en-US", ["it-IT"]);
296
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getSmallDateFormat, "MM/DD/YYYY", ["DD/MM/YYYY"]);
297
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getDefaultDateFormat, "MM/DD/YYYY", ["MM/DD/YYYY"]);
298
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getSmallDateDisplayFormat, "DD/MM/YYYY", ["DD/MM/YYYY"]);
299
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateFormat, "MM/DD/YYYY HH:mm:ss", ["DD/MM/YYYY HH:mm:ss"]);
300
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateDisplayFormat, "DD/MM/YYYY HH:mm:ss", ["DD/MM/YYYY HH:mm:ss"]);
301
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateFormatNoSeconds, "MM/DD/YYYY HH:mm", ["DD/MM/YYYY HH:mm"]);
302
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateDisplayFormatNoSeconds, "DD/MM/YYYY HH:mm", ["DD/MM/YYYY HH:mm"]);
303
+ }
265
304
  }
305
+ BaseLocalization.ɵprov = i0.ɵɵdefineInjectable({ factory: function BaseLocalization_Factory() { return new BaseLocalization(i0.ɵɵinject(LocalePersistor), i0.ɵɵinject(LOC_LOCALE)); }, token: BaseLocalization, providedIn: "root" });
266
306
  BaseLocalization.decorators = [
267
- { type: Injectable }
307
+ { type: Injectable, args: [{ providedIn: 'root' },] }
268
308
  ];
269
- BaseLocalization.ctorParameters = () => [];
309
+ BaseLocalization.ctorParameters = () => [
310
+ { type: LocalePersistor },
311
+ { type: String, decorators: [{ type: Inject, args: [LOC_LOCALE,] }] }
312
+ ];
270
313
 
271
314
  /**
272
315
  * Classe di localizzazione estendibile che permette di specificare le proprie localizzazioni e comunque rendere disponibili le localizzazioni globali
@@ -275,17 +318,22 @@ class LocalizationService {
275
318
  /**
276
319
  * Costruttore
277
320
  *
278
- * @param {string} baseLoc Locale da utilizzare nel momento che questo servizio richiede le traduzioni
321
+ * @param {string} Locale Locale da utilizzare nel momento che questo servizio richiede le traduzioni
279
322
  * @param {baseLocalization} baseLocalization Classe base di localizzazione già fornita a livello di root dalla libreria stessa.
280
323
  * È sufficiente iniettarla nell'eventuale costruttore che estende questa classe e passarla as-is
281
324
  */
282
- constructor(baseLoc, baseLocalization) {
283
- this.baseLoc = baseLoc;
284
- this.baseLocalization = baseLocalization;
325
+ constructor(injector) {
326
+ this.injector = injector;
285
327
  /**
286
328
  * Cache di tutte le localizzazioni disonibili all'interno di questa classe
287
329
  */
288
330
  this.TranslationCache = {};
331
+ this.LocalePersistor = this.injector.get(LocalePersistor);
332
+ this.baseLocalization = this.injector.get(BaseLocalization);
333
+ this.Persistor = new LocalizationPersistor(this.TranslationCache);
334
+ this.baseLocalization.Locale.subscribe(lr => {
335
+ this.Locale = lr;
336
+ });
289
337
  }
290
338
  /**
291
339
  * Imposta la localizzazione di un token all'interno della **TranslationCache**
@@ -298,7 +346,7 @@ class LocalizationService {
298
346
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
299
347
  */
300
348
  setToken(direction, token, left, right) {
301
- BaseLocalization.setToken(this.TranslationCache, direction, token, left, right);
349
+ this.Persistor.setToken(direction, token, left, right);
302
350
  }
303
351
  /**
304
352
  * Imposta la localizzazione di una frase all'interno della **TranslationCache**
@@ -310,7 +358,13 @@ class LocalizationService {
310
358
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
311
359
  */
312
360
  set(direction, left, right) {
313
- BaseLocalization.set(this.TranslationCache, direction, left, right);
361
+ // Controlla di non reinserire la stessa localizzazione presente nel BaseLocalization
362
+ // i controlli per il non reinserimento della stessa localizzazione all'interno della stessa istanza è fatto lato Persistor
363
+ if (this.baseLocalization.Persistor.get(left, false)) {
364
+ console.warn(`La localizzazione per ${left} è già presente fra le localizzazioni base, ignoro l'operazione`);
365
+ return;
366
+ }
367
+ this.Persistor.set(direction, left, right);
314
368
  }
315
369
  /**
316
370
  * Localizza un token
@@ -333,45 +387,42 @@ class LocalizationService {
333
387
  loc(text, allowtokens) {
334
388
  if (!text || text == "undefined")
335
389
  return "";
336
- // Questo può succedere solo se qualcuno crea questa service a manazza. In quel caso prendo la classe di default e basta
337
- if (this.baseLocalization == null)
338
- this.baseLocalization = new BaseLocalization();
339
390
  // Prendo la localizzazione o dalla cache attuale o dalla cache del servizio base
340
- var item = BaseLocalization.findByText(this.TranslationCache, text, allowtokens) || BaseLocalization.findByText(this.baseLocalization.BaseTranslations, text, allowtokens);
391
+ var item = this.Persistor.get(text, allowtokens) || this.baseLocalization.Persistor.get(text, allowtokens);
341
392
  if (!item) {
342
- console.error("Couldn't find localization for '" + text + "'");
393
+ console.error("Impossibile trovare la localizzazione per '" + text + "'");
394
+ return "";
395
+ }
396
+ if (!this.Locale) {
397
+ console.error("Il sistema di localizzazione non ha ancora caricato il Locale, assicurarsi di caricare l'APP solo al retrieve del locale'");
343
398
  return "";
344
399
  }
345
- var loc = BaseLocalization.translationByCulture(item, this.baseLoc);
400
+ var loc = this.Persistor.translationByCulture(item, this.Locale);
346
401
  if (!loc) {
347
- console.error("Couldn't find localization for '" + text + "', culture " + this.baseLoc);
402
+ console.error("Impossibile trovare la localizzazione per '" + text + "', culture " + this.Locale);
348
403
  return "";
349
404
  }
350
405
  return loc.translation;
351
406
  }
352
- /**
353
- * Localizzazione per Severity classiche (E per errore, W per warning e I per info)
354
- *
355
- * @param {string} text Severity da tradurre
356
- *
357
- * @returns {string} Severity tradotta
358
- */
359
- locSeverity(text) {
360
- if (text == "E")
361
- return this.loc("Error");
362
- if (text == "W")
363
- return this.loc("Warning");
364
- if (text == "I")
365
- return this.loc("Information");
366
- return "";
407
+ changeLocale(newLocale) {
408
+ return __awaiter(this, void 0, void 0, function* () {
409
+ let locale = yield this.LocalePersistor.recover().toPromise();
410
+ if (newLocale == locale) {
411
+ console.info("Il locale richiesto è lo stesso del locale attuale. Ignoro l'operazione");
412
+ return;
413
+ }
414
+ else {
415
+ yield this.LocalePersistor.persist(newLocale).toPromise();
416
+ location.reload();
417
+ }
418
+ });
367
419
  }
368
420
  }
369
421
  LocalizationService.decorators = [
370
422
  { type: Injectable }
371
423
  ];
372
424
  LocalizationService.ctorParameters = () => [
373
- { type: String, decorators: [{ type: Inject, args: [LOC_LOCALE,] }] },
374
- { type: BaseLocalization }
425
+ { type: Injector }
375
426
  ];
376
427
 
377
428
  // Angular
@@ -448,6 +499,23 @@ LocalizePipe.ctorParameters = () => [
448
499
  { type: LocalizationService }
449
500
  ];
450
501
 
502
+ // Angular
503
+ class LocalStorageLocalePersistor extends LocalePersistor {
504
+ /** @ignore Vedi classe base */
505
+ persist(locale) {
506
+ localStorage.setItem(this.LOCALE, locale);
507
+ return of(true).pipe(first());
508
+ }
509
+ /** @ignore Vedi classe base */
510
+ recover() {
511
+ var ret = localStorage.getItem(this.LOCALE);
512
+ return of(ret).pipe(first());
513
+ }
514
+ }
515
+ LocalStorageLocalePersistor.decorators = [
516
+ { type: Injectable }
517
+ ];
518
+
451
519
  class LocalizationModule {
452
520
  static forRoot(config) {
453
521
  return {
@@ -455,7 +523,8 @@ class LocalizationModule {
455
523
  providers: [
456
524
  LocalizationService,
457
525
  BaseLocalization,
458
- { provide: LOC_LOCALE, useValue: (config === null || config === void 0 ? void 0 : config.locale) || 'it-IT' }
526
+ { provide: LOC_LOCALE, useValue: (config === null || config === void 0 ? void 0 : config.defaultLocale) || 'it-IT' },
527
+ { provide: LocalePersistor, useClass: (config === null || config === void 0 ? void 0 : config.localePersistor) || LocalStorageLocalePersistor },
459
528
  ]
460
529
  };
461
530
  }
@@ -475,5 +544,5 @@ LocalizationModule.decorators = [
475
544
  * Generated bundle index. Do not edit.
476
545
  */
477
546
 
478
- export { LOC_LOCALE, LocDatePipe, LocalizationModule, LocalizationModuleConfig, LocalizationService, LocalizePipe, BaseLocalization as ɵa };
547
+ export { BaseLocalization, BaseLocalizationTokens, LOC_LOCALE, LocDatePipe, LocalizationModule, LocalizationModuleConfig, LocalizationService, LocalizePipe, LocalePersistor as ɵa, LocalStorageLocalePersistor as ɵb };
479
548
  //# sourceMappingURL=esfaenza-localizations.js.map