@stemy/ngx-utils 19.5.2 → 19.5.4

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.
@@ -1949,7 +1949,10 @@ class LoaderUtils {
1949
1949
  }
1950
1950
  static loadElement(src, parent, setup) {
1951
1951
  const promises = LoaderUtils.promises;
1952
- parent = parent || document.body;
1952
+ parent = parent || document;
1953
+ if (parent == document) {
1954
+ parent = document.body;
1955
+ }
1953
1956
  let { elem, promise } = promises[src] || {};
1954
1957
  if (elem) {
1955
1958
  if (parent === elem.parentElement)
@@ -3472,6 +3475,7 @@ class EventsService {
3472
3475
  this.eventForwarded = new EventEmitter();
3473
3476
  this.stickyUpdated = new EventEmitter();
3474
3477
  this.languageChanged = new EventEmitter();
3478
+ this.translationsEnabled = new EventEmitter();
3475
3479
  this.sticky = false;
3476
3480
  }
3477
3481
  event(e) {
@@ -3598,7 +3602,7 @@ class StaticLanguageService {
3598
3602
  return this.translations[this.currentLanguage] || {};
3599
3603
  }
3600
3604
  set dictionary(value) {
3601
- this.translations[this.currentLanguage] = value;
3605
+ this.setDictionary(this.currentLang, value);
3602
3606
  }
3603
3607
  get languages() {
3604
3608
  return this.languageList;
@@ -3616,12 +3620,18 @@ class StaticLanguageService {
3616
3620
  set editLanguage(lang) {
3617
3621
  this.editLang = lang || this.currentLanguage;
3618
3622
  }
3623
+ get enableTranslations() {
3624
+ return this.enableTrans;
3625
+ }
3626
+ set enableTranslations(value) {
3627
+ this.enableTrans = value;
3628
+ this.events.translationsEnabled.emit(value);
3629
+ }
3619
3630
  get disableTranslations() {
3620
- return this.disableTrans;
3631
+ return !this.enableTranslations;
3621
3632
  }
3622
3633
  set disableTranslations(value) {
3623
- this.disableTrans = value;
3624
- this.events.languageChanged.emit(this.currentLang);
3634
+ this.enableTranslations = !value;
3625
3635
  }
3626
3636
  get httpClient() {
3627
3637
  return this.client;
@@ -3640,7 +3650,7 @@ class StaticLanguageService {
3640
3650
  this.client = client;
3641
3651
  this.editLang = null;
3642
3652
  this.currentLang = null;
3643
- this.disableTrans = false;
3653
+ this.enableTrans = true;
3644
3654
  this.languageList = [];
3645
3655
  this.translations = {
3646
3656
  none: {}
@@ -3662,16 +3672,24 @@ class StaticLanguageService {
3662
3672
  this.replaceLanguages(this.languageList.concat(languages));
3663
3673
  }
3664
3674
  getTranslationSync(key, params = null) {
3665
- const lowerKey = (key || "").toLocaleLowerCase();
3666
- const translation = this.dictionary[lowerKey] || lowerKey;
3667
- return this.interpolate(translation == lowerKey ? key : translation, params);
3668
- }
3669
- getTranslation(key, params) {
3670
- if (!ObjectUtils.isString(key) || !key.length) {
3671
- throw new Error(`Parameter "key" required`);
3675
+ if (!key)
3676
+ return "";
3677
+ try {
3678
+ const lowerKey = key.toLocaleLowerCase();
3679
+ const dict = this.dictionary;
3680
+ if (lowerKey in dict && this.enableTranslations) {
3681
+ return this.interpolate(dict[lowerKey], params);
3682
+ }
3683
+ return this.interpolate(key, params);
3672
3684
  }
3673
- const translation = ObjectUtils.getValue(this.dictionary, key, key) || key;
3674
- return this.promises.resolve(this.interpolate(translation, params));
3685
+ catch (reason) {
3686
+ console.warn("ERROR IN TRANSLATIONS", reason);
3687
+ return key;
3688
+ }
3689
+ }
3690
+ async getTranslation(key, params = null) {
3691
+ await this.loadDictionary();
3692
+ return this.getTranslationSync(key, params);
3675
3693
  }
3676
3694
  getTranslations(...keys) {
3677
3695
  return this.promises.create(resolve => {
@@ -3692,6 +3710,16 @@ class StaticLanguageService {
3692
3710
  const translation = translations ? translations.find(t => t.lang == lang) : null;
3693
3711
  return this.interpolate(translation ? translation.translation : "", params);
3694
3712
  }
3713
+ async loadDictionary() {
3714
+ return this.dictionary;
3715
+ }
3716
+ setDictionary(lang, dictionary) {
3717
+ this.translations[lang] = Object.keys(dictionary || {}).reduce((res, key) => {
3718
+ res[key.toLocaleLowerCase()] = dictionary[key];
3719
+ return res;
3720
+ }, {});
3721
+ return this.translations[lang];
3722
+ }
3695
3723
  interpolate(expr, params) {
3696
3724
  if (typeof expr === "string") {
3697
3725
  return this.interpolateString(expr, params);
@@ -3766,6 +3794,7 @@ class LanguageService extends StaticLanguageService {
3766
3794
  }));
3767
3795
  }
3768
3796
  initService() {
3797
+ super.initService();
3769
3798
  this.client.setExtraRequestParam("language", "de");
3770
3799
  this.translationRequests = {};
3771
3800
  this.languageSettings = new BehaviorSubject(null);
@@ -3798,22 +3827,6 @@ class LanguageService extends StaticLanguageService {
3798
3827
  await this.useLanguage(lang);
3799
3828
  this.events.languageChanged.emit(lang);
3800
3829
  }
3801
- async getTranslation(key, params = null) {
3802
- if (!key)
3803
- return "";
3804
- try {
3805
- const lowerKey = key.toLocaleLowerCase();
3806
- const dict = await this.loadDictionary();
3807
- if (lowerKey in dict) {
3808
- return this.interpolate(dict[lowerKey], params);
3809
- }
3810
- return this.interpolate(key, params);
3811
- }
3812
- catch (reason) {
3813
- console.log("ERROR IN TRANSLATIONS", reason);
3814
- return key;
3815
- }
3816
- }
3817
3830
  async useLanguage(lang) {
3818
3831
  lang = this.languages.indexOf(lang) < 0 ? this.languages[0] : lang;
3819
3832
  this.client.setExtraRequestParam("language", lang);
@@ -3821,22 +3834,18 @@ class LanguageService extends StaticLanguageService {
3821
3834
  return this.dictionary;
3822
3835
  this.storage.set("language", lang);
3823
3836
  this.currentLang = lang;
3824
- const dict = await this.loadDictionary();
3825
- this.translations[lang] = dict;
3826
- return dict;
3837
+ return this.loadDictionary();
3827
3838
  }
3828
3839
  loadDictionary() {
3829
3840
  const lang = this.currentLanguage;
3830
3841
  this.translationRequests[lang] = this.translationRequests[lang] || new Promise(resolve => {
3831
3842
  const ext = this.config.translationExt || ``;
3832
- this.httpClient.get(`${this.config.translationUrl}${lang}${ext}`).toPromise().then(response => {
3833
- response = response || {};
3834
- resolve(Object.keys(response).reduce((result, key) => {
3835
- result[key.toLocaleLowerCase()] = response[key];
3836
- return result;
3837
- }, {}));
3838
- }, () => {
3839
- resolve({});
3843
+ const request = this.httpClient.get(`${this.config.translationUrl}${lang}${ext}`);
3844
+ firstValueFrom(request).then(response => {
3845
+ resolve(this.setDictionary(lang, response));
3846
+ }, reason => {
3847
+ console.warn("ERROR IN TRANSLATIONS", reason);
3848
+ resolve(this.setDictionary(lang, {}));
3840
3849
  });
3841
3850
  });
3842
3851
  return this.translationRequests[lang];
@@ -5172,9 +5181,9 @@ class TranslatePipe {
5172
5181
  this.lang = lang;
5173
5182
  dirty = true;
5174
5183
  }
5175
- const disabled = this.language.disableTranslations;
5176
- if (this.disabled !== disabled) {
5177
- this.disabled = disabled;
5184
+ const enabled = this.language.enableTranslations;
5185
+ if (this.enabled !== enabled) {
5186
+ this.enabled = enabled;
5178
5187
  dirty = true;
5179
5188
  }
5180
5189
  if (!ObjectUtils.equals(this.query, query)) {
@@ -5207,10 +5216,6 @@ class TranslatePipe {
5207
5216
  this.lastValue = Array.isArray(query) ? this.language.getTranslationFromArray(query, this.params, lang) : this.language.getTranslationFromObject(query, this.params, lang);
5208
5217
  return this.lastValue;
5209
5218
  }
5210
- if (this.disabled) {
5211
- this.lastValue = query;
5212
- return this.lastValue;
5213
- }
5214
5219
  this.lastValue = this.language.getTranslationSync(query, this.params);
5215
5220
  this.language.getTranslation(query, this.params).then(value => {
5216
5221
  this.lastValue = value;