@esfaenza/localizations 15.2.0 → 15.2.2

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