@esfaenza/localizations 15.2.1 → 15.2.3

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.
Files changed (43) hide show
  1. package/esm2020/lib/core/localization.base.mjs +80 -160
  2. package/esm2020/lib/core/localization.persistor.mjs +103 -0
  3. package/esm2020/lib/locale-persistor/LocalStorageCachePersistor.mjs +25 -0
  4. package/esm2020/lib/locale-persistor/LocalePersistor.mjs +8 -0
  5. package/esm2020/lib/localizations.module.mjs +12 -4
  6. package/esm2020/lib/localizations.service.mjs +52 -38
  7. package/esm2020/lib/models/BaseLocalizationTokens.mjs +14 -0
  8. package/esm2020/lib/models/ItemLocalization.mjs +18 -0
  9. package/esm2020/lib/models/LocaleDefinitions.mjs +32 -0
  10. package/esm2020/lib/models/LocaleObject.mjs +18 -0
  11. package/esm2020/lib/models/LocalizationMissingPolicy.mjs +11 -0
  12. package/esm2020/lib/models/LocalizationModuleConfig.mjs +6 -0
  13. package/esm2020/lib/models/SpecializedLocalization.mjs +16 -0
  14. package/esm2020/lib/tokens.mjs +4 -4
  15. package/esm2020/public-api.mjs +5 -2
  16. package/fesm2015/esfaenza-localizations.mjs +269 -182
  17. package/fesm2015/esfaenza-localizations.mjs.map +1 -1
  18. package/fesm2020/esfaenza-localizations.mjs +262 -178
  19. package/fesm2020/esfaenza-localizations.mjs.map +1 -1
  20. package/lib/core/localization.base.d.ts +12 -59
  21. package/lib/core/localization.persistor.d.ts +57 -0
  22. package/lib/locale-persistor/LocalStorageCachePersistor.d.ts +11 -0
  23. package/lib/locale-persistor/LocalePersistor.d.ts +18 -0
  24. package/lib/localizations.module.d.ts +1 -1
  25. package/lib/localizations.service.d.ts +11 -17
  26. package/lib/models/BaseLocalizationTokens.d.ts +12 -0
  27. package/lib/models/LocalizationMissingPolicy.d.ts +9 -0
  28. package/lib/models/LocalizationModuleConfig.d.ts +17 -0
  29. package/lib/tokens.d.ts +4 -3
  30. package/package.json +1 -1
  31. package/public-api.d.ts +4 -1
  32. package/esm2020/lib/models/classes/ItemLocalization.mjs +0 -18
  33. package/esm2020/lib/models/classes/LocaleDefinitions.mjs +0 -32
  34. package/esm2020/lib/models/classes/LocaleObject.mjs +0 -18
  35. package/esm2020/lib/models/classes/LocalizationModuleConfig.mjs +0 -6
  36. package/esm2020/lib/models/classes/SpecializedLocalization.mjs +0 -16
  37. package/esm2020/lib/models/index.mjs +0 -6
  38. package/lib/models/classes/LocalizationModuleConfig.d.ts +0 -9
  39. package/lib/models/index.d.ts +0 -5
  40. /package/lib/models/{classes/ItemLocalization.d.ts → ItemLocalization.d.ts} +0 -0
  41. /package/lib/models/{classes/LocaleDefinitions.d.ts → LocaleDefinitions.d.ts} +0 -0
  42. /package/lib/models/{classes/LocaleObject.d.ts → LocaleObject.d.ts} +0 -0
  43. /package/lib/models/{classes/SpecializedLocalization.d.ts → SpecializedLocalization.d.ts} +0 -0
@@ -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
  */
@@ -74,12 +71,6 @@ LocaleDefinitions.AvailableLocales = [
74
71
  new LocaleObject('en-US', 'English', ['en', 'en-US'])
75
72
  ];
76
73
 
77
- /**
78
- * Classe di configurazione per il modulo di localizzazione
79
- */
80
- class LocalizationModuleConfig {
81
- }
82
-
83
74
  /**
84
75
  * Classe che rappresenta la localizzazione di un test base per una certa lingua
85
76
  */
@@ -96,77 +87,13 @@ class SpecializedLocalization {
96
87
  }
97
88
  }
98
89
 
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"]);
90
+ class LocalizationPersistor {
91
+ constructor(LocCache = null) {
92
+ this.LocCache = LocCache;
165
93
  }
166
94
  /**
167
95
  * Imposta la localizzazione di un token all'interno della cache scelta
168
96
  *
169
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno della quale inserire l'informazione del token con le localizzazioni
170
97
  * @param {string} direction Direzione della traduzione, rappresenta la lingua base in cui aspettarsi il testo e le varie lingue in cui viene tradotta.
171
98
  * Ad esempio, "en->it" indica che il parametro **left** è valido per le localizzazioni in inglese, mentre nell'elemento 0 del parametro **right**
172
99
  * è 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 +101,54 @@ class BaseLocalization {
174
101
  * @param {string} left Valore del token per la lingua specificata nel lato immediatamente sinistro alla freccia del parametro **direction**
175
102
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
176
103
  */
177
- static setToken(locCache, direction, token, left, right) {
104
+ setToken(direction, token, left, right) {
178
105
  let parsingRegex = /([a-zA-Z]+)[\s]*->[\s]*([a-zA-Z,\s]+)+/g;
179
106
  var parsingRes = parsingRegex.exec(direction);
180
107
  let from = parsingRes[1];
181
108
  let to = parsingRes[2].replace(/\s/g, '').split(',');
182
109
  let localeFrom = LocaleDefinitions.getByAlias(from);
183
- BaseLocalization._set(locCache, token, localeFrom.id, left, true);
110
+ this._set(token, localeFrom.id, left, true);
184
111
  to.forEach((t, index) => {
185
112
  let localeTo = LocaleDefinitions.getByAlias(t);
186
- BaseLocalization._set(locCache, token, localeTo.id, right[index], true);
113
+ this._set(token, localeTo.id, right[index], true);
187
114
  });
188
115
  }
189
116
  /**
190
117
  * Imposta la localizzazione di una frase all'interno della cache scelta
191
118
  *
192
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno della quale inserire l'informazione della frase con le localizzazioni
193
119
  * @param {string} direction Direzione della traduzione, rappresenta la lingua base in cui aspettarsi il testo e le varie lingue in cui viene tradotta.
194
120
  * Ad esempio, "en->it" indica che il parametro **left** è valido per le localizzazioni in inglese, mentre nell'elemento 0 del parametro **right**
195
121
  * è 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
122
  * @param {string} left Valore della frase per la lingua specificata nel lato immediatamente sinistro alla freccia del parametro **direction**
197
123
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
198
124
  */
199
- static set(locCache, direction, left, right) {
125
+ set(direction, left, right) {
200
126
  let parsingRegex = /([a-zA-Z]+)[\s]*->[\s]*([a-zA-Z,\s]+)+/g;
201
127
  var parsingRes = parsingRegex.exec(direction);
202
128
  let from = parsingRes[1];
203
129
  let to = parsingRes[2].replace(/\s/g, '').split(',');
204
130
  let localeFrom = LocaleDefinitions.getByAlias(from);
205
- BaseLocalization._set(locCache, left, localeFrom.id, left);
131
+ this._set(left, localeFrom.id, left);
206
132
  to.forEach((t, index) => {
207
133
  let localeTo = LocaleDefinitions.getByAlias(t);
208
- BaseLocalization._set(locCache, left, localeTo.id, right[index]);
134
+ this._set(left, localeTo.id, right[index]);
209
135
  });
210
136
  }
211
137
  /**
212
138
  * Imposta all'interno della cache specificata un'unica traduzione partendo da un testo di base
213
139
  *
214
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno della quale inserire l'informazione del testo con le localizzazioni
215
140
  * @param {string} text Testo da tradurre
216
141
  * @param {string} culture Lingua per cui inserire la traduzione
217
142
  * @param {string} translation Traduzione del **text** nella lingua specificata da **culture**
218
143
  * @param {boolean} token Indica se il **text** è un token o è una frase normale
219
144
  */
220
- static _set(locCache, text, culture, translation, token) {
221
- let item = BaseLocalization.findByText(locCache, text, token);
145
+ _set(text, culture, translation, token) {
146
+ let item = this.get(text, token);
222
147
  if (!item) {
223
- locCache[text] = new ItemLocalization(token, text, [new SpecializedLocalization(culture, translation)]);
148
+ this.LocCache[text] = new ItemLocalization(token, text, [new SpecializedLocalization(culture, translation)]);
224
149
  return;
225
150
  }
226
- var trs = BaseLocalization.translationByCulture(item, culture);
151
+ var trs = this.translationByCulture(item, culture);
227
152
  if (trs) {
228
153
  console.warn("Localization already loaded for sentence '" + text + "' and culture " + culture);
229
154
  return;
@@ -233,18 +158,15 @@ class BaseLocalization {
233
158
  /**
234
159
  * Partendo da una cache di localizzazione, dato un testo che può essere o meno un token, cerca quell'elemento all'interno della cache
235
160
  *
236
- * @param {{ [id: string]: ItemLocalization }} locCache Cache all'interno del quale cercare il **text**
237
- * @param {string} text Testo da cercare nella **locCache**
161
+ * @param {string} text Testo da cercare nella **LocCache**
238
162
  * @param {boolean} allowtokens Indica se l'elemento cercato può essere un token o se dev'essere per forza una frase (per evitare rari
239
163
  * casi di collisione in cui magari un token è uguale a una frase tradotta)
240
164
  *
241
165
  * @returns {ItemLocalization} Informazioni di localizzazione per il **text** cercato
242
166
  */
243
- static findByText(locCache, text, allowtokens) {
244
- let possibleItem = locCache[text];
245
- if (!possibleItem)
246
- return null;
247
- var item = allowtokens || !possibleItem.token ? possibleItem : null;
167
+ get(text, allowtokens) {
168
+ let possibleItem = this.LocCache[text];
169
+ var item = possibleItem && (allowtokens || !possibleItem.token) ? possibleItem : null;
248
170
  return item;
249
171
  }
250
172
  /**
@@ -255,7 +177,7 @@ class BaseLocalization {
255
177
  *
256
178
  * @returns {SpecializedLocalization} Informazioni di localizzazione specifiche della lingua richiesta
257
179
  */
258
- static translationByCulture(item, culture) {
180
+ translationByCulture(item, culture) {
259
181
  for (let i = 0; i < item.translations.length; i++) {
260
182
  let trl = item.translations[i];
261
183
  if (trl.culture == culture)
@@ -263,12 +185,174 @@ class BaseLocalization {
263
185
  }
264
186
  return null;
265
187
  }
188
+ }
189
+
190
+ var BaseLocalizationTokens;
191
+ (function (BaseLocalizationTokens) {
192
+ BaseLocalizationTokens["getDateMonthPosition"] = "getDateMonthPosition";
193
+ BaseLocalizationTokens["getDisplayDateMonthPosition"] = "getDisplayDateMonthPosition";
194
+ BaseLocalizationTokens["getLocale"] = "getLocale";
195
+ BaseLocalizationTokens["getSmallDateFormat"] = "getSmallDateFormat";
196
+ BaseLocalizationTokens["getDefaultDateFormat"] = "getDefaultDateFormat";
197
+ BaseLocalizationTokens["getSmallDateDisplayFormat"] = "getSmallDateDisplayFormat";
198
+ BaseLocalizationTokens["getFullDateFormat"] = "getFullDateFormat";
199
+ BaseLocalizationTokens["getFullDateDisplayFormat"] = "getFullDateDisplayFormat";
200
+ BaseLocalizationTokens["getFullDateFormatNoSeconds"] = "getFullDateFormatNoSeconds";
201
+ BaseLocalizationTokens["getFullDateDisplayFormatNoSeconds"] = "getFullDateDisplayFormatNoSeconds";
202
+ })(BaseLocalizationTokens || (BaseLocalizationTokens = {}));
203
+
204
+ /** Locale da utilizzare per le traduzioni */
205
+ const LOC_LOCALE = new InjectionToken('LOC_LOCALE');
206
+ /** Policy da utilizzare per traduzioni mancanti */
207
+ const LOC_MISSING_POLICY = new InjectionToken('LOC_MISSING_POLICY');
208
+
209
+ /** Classe astratta che rappresenta lo stato di persistenza fra la libreria e uno storage */
210
+ class LocalePersistor {
211
+ constructor() {
212
+ /** Chiave su cui salvare il locale */
213
+ this.LOCALE = "LOCALE";
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Classe singleton che contiene le traduzioni di base (per evitare di ripeterle in oggi classe di traduzione) e le funzioni base per
219
+ * inserire e leggere traduzioni in/da una cache
220
+ */
221
+ class BaseLocalization {
222
+ /**
223
+ * Costruttore. Da qui utilizzando il metodo statico **BaseLocalization.set** si va a popolare la cache delle traduzioni **BaseTranslations**
224
+ */
225
+ constructor(LocalePersistor, defaultLocale) {
226
+ this.LocalePersistor = LocalePersistor;
227
+ /**
228
+ * Cache di tutte le localizzazioni di base
229
+ */
230
+ this.LocCache = {};
231
+ /**
232
+ * Locale di riferimento per tutte le traduzioni
233
+ */
234
+ this.Locale = new ReplaySubject(1);
235
+ // Recupero il locale alla partenza del BaseLocalization in modo, spero di dare tempo all'applicazione di tirarlo su
236
+ this.LocalePersistor.recover().pipe(take(1)).subscribe(async (l) => {
237
+ if (!l) {
238
+ l = defaultLocale;
239
+ this.LocalePersistor.persist(defaultLocale);
240
+ }
241
+ this.Locale.next(l);
242
+ });
243
+ this.Persistor = new LocalizationPersistor(this.LocCache);
244
+ this.Persistor.set("en->it", "Remove", ["Elimina"]);
245
+ this.Persistor.set("en->it", "Paging", ["Paginazione"]);
246
+ this.Persistor.set("en->it", "Yes", ["Si"]);
247
+ this.Persistor.set("en->it", "No", ["No"]);
248
+ this.Persistor.set("en->it", "YES", ["SI'"]);
249
+ this.Persistor.set("en->it", "NO", ["NO"]);
250
+ this.Persistor.set("en->it", "Found objects", ["Risultati trovati"]);
251
+ this.Persistor.set("en->it", "Loading data...", ["Carico i dati..."]);
252
+ this.Persistor.set("en->it", "Error during save operations", ["Errori durante il salvataggio"]);
253
+ this.Persistor.set("en->it", "Error", ["Errore"]);
254
+ this.Persistor.set("en->it", "Warning", ["Avviso"]);
255
+ this.Persistor.set("en->it", "Information", ["Informazione"]);
256
+ this.Persistor.set("en->it", "Save", ["Salva"]);
257
+ this.Persistor.set("en->it", "Back", ["Indietro"]);
258
+ this.Persistor.set("en->it", "From", ["Da"]);
259
+ this.Persistor.set("en->it", "To", ["A"]);
260
+ this.Persistor.set("en->it", "View Chart", ["Vedi Grafico"]);
261
+ this.Persistor.set("en->it", "View Table", ["Vedi Tabella"]);
262
+ this.Persistor.set("en->it", "Notes", ["Note"]);
263
+ this.Persistor.set("en->it", "Edit Notes", ["Modifica Note"]);
264
+ this.Persistor.set("en->it", "Cancel", ["Annulla"]);
265
+ this.Persistor.set("en->it", "Confirm", ["Conferma"]);
266
+ this.Persistor.set("en->it", "View", ["Visualizza"]);
267
+ this.Persistor.set("en->it", "Close", ["Chiudi"]);
268
+ this.Persistor.set("en->it", "Select", ["Seleziona"]);
269
+ this.Persistor.set("en->it", "Meter Reading registered successfully", ["Lettura registrata correttamente"]);
270
+ this.Persistor.set("en->it", "Validation errors during save operations", ["Errori di validazione durante il salvataggio"]);
271
+ this.Persistor.set("en->it", "Can't register reading", ["Impossibile registrare la lettura"]);
272
+ this.Persistor.set("en->it", "Electricity", ["Elettricita"]);
273
+ this.Persistor.set("en->it", "Gas", ["Gas"]);
274
+ this.Persistor.set("en->it", "Water", ["Acqua"]);
275
+ this.Persistor.set("en->it", "Delete", ["Elimina"]);
276
+ this.Persistor.set("en->it", "This operation cannot be reverted", ["Questa operazione non puo' essere annullata"]);
277
+ this.Persistor.set("en->it", "Hide", ["Nascondi"]);
278
+ // Helper validazione
279
+ this.Persistor.set("en->it", "Required field", ["Campo richiesto"]);
280
+ this.Persistor.set("en->it", "Wrong field format", ["Formato campo errato"]);
281
+ this.Persistor.set("en->it", "Wrong field format or field missing", ["Formato campo errato o campo mancante"]);
282
+ 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"]);
283
+ // Informazioni sulle date
284
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getDateMonthPosition, "0", ["1"]);
285
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getDisplayDateMonthPosition, "1", ["1"]);
286
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getLocale, "en-US", ["it-IT"]);
287
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getSmallDateFormat, "MM/DD/YYYY", ["DD/MM/YYYY"]);
288
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getDefaultDateFormat, "MM/DD/YYYY", ["MM/DD/YYYY"]);
289
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getSmallDateDisplayFormat, "DD/MM/YYYY", ["DD/MM/YYYY"]);
290
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateFormat, "MM/DD/YYYY HH:mm:ss", ["DD/MM/YYYY HH:mm:ss"]);
291
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateDisplayFormat, "DD/MM/YYYY HH:mm:ss", ["DD/MM/YYYY HH:mm:ss"]);
292
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateFormatNoSeconds, "MM/DD/YYYY HH:mm", ["DD/MM/YYYY HH:mm"]);
293
+ this.Persistor.setToken("en->it", BaseLocalizationTokens.getFullDateDisplayFormatNoSeconds, "DD/MM/YYYY HH:mm", ["DD/MM/YYYY HH:mm"]);
294
+ }
266
295
  }
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 });
296
+ 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 });
297
+ BaseLocalization.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: BaseLocalization, providedIn: 'root' });
269
298
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: BaseLocalization, decorators: [{
270
- type: Injectable
271
- }], ctorParameters: function () { return []; } });
299
+ type: Injectable,
300
+ args: [{ providedIn: 'root' }]
301
+ }], ctorParameters: function () { return [{ type: LocalePersistor }, { type: undefined, decorators: [{
302
+ type: Inject,
303
+ args: [LOC_LOCALE]
304
+ }] }]; } });
305
+
306
+ // Angular
307
+ /**
308
+ * Pipe che permette di formattare una data in base al formato specificato. Utilizza il **DateService** di **@esfaenza/extensions** per ricondurre l'input a una data valida
309
+ */
310
+ class LocDatePipe {
311
+ /**
312
+ * @ignore
313
+ */
314
+ constructor(dts) {
315
+ this.dts = dts;
316
+ }
317
+ /**
318
+ * Trasformazione della data in input in base al formato richiesto
319
+ *
320
+ * @param {any} value Valore da ricondurre ad una data e formattare
321
+ * @param {string} format Formato in output della data da visualizzare
322
+ *
323
+ * @returns {string} Data formattata in base al formato
324
+ */
325
+ transform(value, format = "DD/MM/YYYY") {
326
+ if (value == null || value === "")
327
+ return null;
328
+ if (!this.dts) {
329
+ console.error("Tentativo di usare loc_date senza DateService. È possibile importare DateService installando e configurando il pacchetto @esfaenza/extensions");
330
+ return null;
331
+ }
332
+ var detatchedValue = JSON.parse(JSON.stringify(value));
333
+ var dt = this.dts.getDateConvertion(detatchedValue);
334
+ return dt.format(format);
335
+ }
336
+ }
337
+ LocDatePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocDatePipe, deps: [{ token: i1.DateService, optional: true }], target: i0.ɵɵFactoryTarget.Pipe });
338
+ LocDatePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "15.2.9", ngImport: i0, type: LocDatePipe, name: "loc_date" });
339
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocDatePipe, decorators: [{
340
+ type: Pipe,
341
+ args: [{ name: "loc_date", pure: true }]
342
+ }], ctorParameters: function () { return [{ type: i1.DateService, decorators: [{
343
+ type: Optional
344
+ }] }]; } });
345
+
346
+ /**
347
+ * Enum che rappresenta le policy di comportamento qualore il sistema di localizzazione non trovi la localizzazione cercata,
348
+ * usare **ThrowException** solo se ci si sente infallibili o si vuole debuggare in maniera approfondita l'applicazione
349
+ */
350
+ var LocalizationMissingPolicy;
351
+ (function (LocalizationMissingPolicy) {
352
+ LocalizationMissingPolicy[LocalizationMissingPolicy["PrintEmpty"] = 0] = "PrintEmpty";
353
+ LocalizationMissingPolicy[LocalizationMissingPolicy["PrintSource"] = 1] = "PrintSource";
354
+ LocalizationMissingPolicy[LocalizationMissingPolicy["ThrowException"] = 2] = "ThrowException";
355
+ })(LocalizationMissingPolicy || (LocalizationMissingPolicy = {}));
272
356
 
273
357
  /**
274
358
  * Classe di localizzazione estendibile che permette di specificare le proprie localizzazioni e comunque rendere disponibili le localizzazioni globali
@@ -277,17 +361,23 @@ class LocalizationService {
277
361
  /**
278
362
  * Costruttore
279
363
  *
280
- * @param {string} baseLoc Locale da utilizzare nel momento che questo servizio richiede le traduzioni
364
+ * @param {string} Locale Locale da utilizzare nel momento che questo servizio richiede le traduzioni
281
365
  * @param {baseLocalization} baseLocalization Classe base di localizzazione già fornita a livello di root dalla libreria stessa.
282
366
  * È sufficiente iniettarla nell'eventuale costruttore che estende questa classe e passarla as-is
283
367
  */
284
- constructor(baseLoc, baseLocalization) {
285
- this.baseLoc = baseLoc;
286
- this.baseLocalization = baseLocalization;
368
+ constructor(injector) {
369
+ this.injector = injector;
287
370
  /**
288
371
  * Cache di tutte le localizzazioni disonibili all'interno di questa classe
289
372
  */
290
373
  this.TranslationCache = {};
374
+ this.LocalePersistor = this.injector.get(LocalePersistor);
375
+ this.baseLocalization = this.injector.get(BaseLocalization);
376
+ this.onMissing = this.injector.get(LOC_MISSING_POLICY);
377
+ this.Persistor = new LocalizationPersistor(this.TranslationCache);
378
+ this.baseLocalization.Locale.subscribe(lr => {
379
+ this.Locale = lr;
380
+ });
291
381
  }
292
382
  /**
293
383
  * Imposta la localizzazione di un token all'interno della **TranslationCache**
@@ -300,7 +390,7 @@ class LocalizationService {
300
390
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
301
391
  */
302
392
  setToken(direction, token, left, right) {
303
- BaseLocalization.setToken(this.TranslationCache, direction, token, left, right);
393
+ this.Persistor.setToken(direction, token, left, right);
304
394
  }
305
395
  /**
306
396
  * Imposta la localizzazione di una frase all'interno della **TranslationCache**
@@ -312,7 +402,13 @@ class LocalizationService {
312
402
  * @param {string} right Array con i valori facenti riferimento all'N-esima lingua del lato destro del parametro **direction**
313
403
  */
314
404
  set(direction, left, right) {
315
- BaseLocalization.set(this.TranslationCache, direction, left, right);
405
+ // Controlla di non reinserire la stessa localizzazione presente nel BaseLocalization
406
+ // i controlli per il non reinserimento della stessa localizzazione all'interno della stessa istanza è fatto lato Persistor
407
+ if (this.baseLocalization.Persistor.get(left, false)) {
408
+ console.warn(`La localizzazione per ${left} è già presente fra le localizzazioni base, ignoro l'operazione`);
409
+ return;
410
+ }
411
+ this.Persistor.set(direction, left, right);
316
412
  }
317
413
  /**
318
414
  * Localizza un token
@@ -333,89 +429,49 @@ class LocalizationService {
333
429
  * @returns {string} Frase localizzata
334
430
  */
335
431
  loc(text, allowtokens) {
432
+ if (!this.Locale)
433
+ throw "Il sistema di localizzazione non ha ancora caricato il Locale, assicurarsi di caricare l'APP solo al retrieve del locale'";
336
434
  if (!text || text == "undefined")
337
435
  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
436
  // 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);
437
+ var item = this.Persistor.get(text, allowtokens) || this.baseLocalization.Persistor.get(text, allowtokens);
343
438
  if (!item) {
344
- console.error("Couldn't find localization for '" + text + "'");
345
- return "";
439
+ let message = `Impossibile trovare la localizzazione per '${text}'`;
440
+ switch (this.onMissing) {
441
+ case LocalizationMissingPolicy.PrintEmpty:
442
+ console.error(message);
443
+ return "";
444
+ case LocalizationMissingPolicy.PrintSource:
445
+ console.warn(message);
446
+ return text;
447
+ case LocalizationMissingPolicy.ThrowException:
448
+ throw message;
449
+ }
346
450
  }
347
- var loc = BaseLocalization.translationByCulture(item, this.baseLoc);
451
+ var loc = this.Persistor.translationByCulture(item, this.Locale);
348
452
  if (!loc) {
349
- console.error("Couldn't find localization for '" + text + "', culture " + this.baseLoc);
453
+ console.error("Impossibile trovare la localizzazione per '" + text + "', culture " + this.Locale);
350
454
  return "";
351
455
  }
352
456
  return loc.translation;
353
457
  }
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 "";
458
+ async changeLocale(newLocale) {
459
+ let locale = await this.LocalePersistor.recover().toPromise();
460
+ if (newLocale == locale) {
461
+ console.info("Il locale richiesto è lo stesso del locale attuale. Ignoro l'operazione");
462
+ return;
463
+ }
464
+ else {
465
+ await this.LocalePersistor.persist(newLocale).toPromise();
466
+ location.reload();
467
+ }
369
468
  }
370
469
  }
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 });
470
+ 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
471
  LocalizationService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocalizationService });
373
472
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocalizationService, decorators: [{
374
473
  type: Injectable
375
- }], ctorParameters: function () { return [{ type: undefined, decorators: [{
376
- type: Inject,
377
- args: [LOC_LOCALE]
378
- }] }, { type: BaseLocalization }]; } });
379
-
380
- // Angular
381
- /**
382
- * Pipe che permette di formattare una data in base al formato specificato. Utilizza il **DateService** di **@esfaenza/extensions** per ricondurre l'input a una data valida
383
- */
384
- class LocDatePipe {
385
- /**
386
- * @ignore
387
- */
388
- constructor(dts) {
389
- this.dts = dts;
390
- }
391
- /**
392
- * Trasformazione della data in input in base al formato richiesto
393
- *
394
- * @param {any} value Valore da ricondurre ad una data e formattare
395
- * @param {string} format Formato in output della data da visualizzare
396
- *
397
- * @returns {string} Data formattata in base al formato
398
- */
399
- transform(value, format = "DD/MM/YYYY") {
400
- if (value == null || value === "")
401
- return null;
402
- if (!this.dts) {
403
- console.error("Tentativo di usare loc_date senza DateService. È possibile importare DateService installando e configurando il pacchetto @esfaenza/extensions");
404
- return null;
405
- }
406
- var detatchedValue = JSON.parse(JSON.stringify(value));
407
- var dt = this.dts.getDateConvertion(detatchedValue);
408
- return dt.format(format);
409
- }
410
- }
411
- LocDatePipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocDatePipe, deps: [{ token: i1.DateService, optional: true }], target: i0.ɵɵFactoryTarget.Pipe });
412
- LocDatePipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "15.2.9", ngImport: i0, type: LocDatePipe, name: "loc_date" });
413
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocDatePipe, decorators: [{
414
- type: Pipe,
415
- args: [{ name: "loc_date", pure: true }]
416
- }], ctorParameters: function () { return [{ type: i1.DateService, decorators: [{
417
- type: Optional
418
- }] }]; } });
474
+ }], ctorParameters: function () { return [{ type: i0.Injector }]; } });
419
475
 
420
476
  // Angular
421
477
  /**
@@ -453,6 +509,26 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImpor
453
509
  args: [{ name: "localize", pure: true }]
454
510
  }], ctorParameters: function () { return [{ type: LocalizationService }]; } });
455
511
 
512
+ // Angular
513
+ class LocalStorageLocalePersistor extends LocalePersistor {
514
+ /** @ignore Vedi classe base */
515
+ persist(locale) {
516
+ localStorage.setItem(this.LOCALE, locale);
517
+ return of(true).pipe(first());
518
+ }
519
+ /** @ignore Vedi classe base */
520
+ recover() {
521
+ var ret = localStorage.getItem(this.LOCALE);
522
+ return of(ret).pipe(first());
523
+ }
524
+ }
525
+ LocalStorageLocalePersistor.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocalStorageLocalePersistor, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
526
+ LocalStorageLocalePersistor.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocalStorageLocalePersistor });
527
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: LocalStorageLocalePersistor, decorators: [{
528
+ type: Injectable
529
+ }] });
530
+
531
+ // Angular
456
532
  class LocalizationModule {
457
533
  static forRoot(config) {
458
534
  return {
@@ -460,7 +536,9 @@ class LocalizationModule {
460
536
  providers: [
461
537
  LocalizationService,
462
538
  BaseLocalization,
463
- { provide: LOC_LOCALE, useValue: config?.locale || 'it-IT' }
539
+ { provide: LOC_LOCALE, useValue: config?.defaultLocale || 'it-IT' },
540
+ { provide: LOC_MISSING_POLICY, useValue: config?.localizationMissingPolicy || LocalizationMissingPolicy.PrintEmpty },
541
+ { provide: LocalePersistor, useClass: config?.localePersistor || LocalStorageLocalePersistor },
464
542
  ]
465
543
  };
466
544
  }
@@ -476,6 +554,12 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImpor
476
554
  }]
477
555
  }] });
478
556
 
557
+ /**
558
+ * Classe di configurazione per il modulo di localizzazione
559
+ */
560
+ class LocalizationModuleConfig {
561
+ }
562
+
479
563
  /*
480
564
  * Public API Surface of localizations
481
565
  */
@@ -484,5 +568,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImpor
484
568
  * Generated bundle index. Do not edit.
485
569
  */
486
570
 
487
- export { LOC_LOCALE, LocDatePipe, LocalizationModule, LocalizationModuleConfig, LocalizationService, LocalizePipe };
571
+ export { BaseLocalization, BaseLocalizationTokens, LOC_LOCALE, LOC_MISSING_POLICY, LocDatePipe, LocalizationMissingPolicy, LocalizationModule, LocalizationModuleConfig, LocalizationService, LocalizePipe };
488
572
  //# sourceMappingURL=esfaenza-localizations.mjs.map