@stemy/ngx-utils 19.5.2 → 19.5.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.
@@ -3472,6 +3472,7 @@ class EventsService {
3472
3472
  this.eventForwarded = new EventEmitter();
3473
3473
  this.stickyUpdated = new EventEmitter();
3474
3474
  this.languageChanged = new EventEmitter();
3475
+ this.translationsEnabled = new EventEmitter();
3475
3476
  this.sticky = false;
3476
3477
  }
3477
3478
  event(e) {
@@ -3598,7 +3599,7 @@ class StaticLanguageService {
3598
3599
  return this.translations[this.currentLanguage] || {};
3599
3600
  }
3600
3601
  set dictionary(value) {
3601
- this.translations[this.currentLanguage] = value;
3602
+ this.setDictionary(this.currentLang, value);
3602
3603
  }
3603
3604
  get languages() {
3604
3605
  return this.languageList;
@@ -3616,12 +3617,18 @@ class StaticLanguageService {
3616
3617
  set editLanguage(lang) {
3617
3618
  this.editLang = lang || this.currentLanguage;
3618
3619
  }
3620
+ get enableTranslations() {
3621
+ return this.enableTrans;
3622
+ }
3623
+ set enableTranslations(value) {
3624
+ this.enableTrans = value;
3625
+ this.events.translationsEnabled.emit(value);
3626
+ }
3619
3627
  get disableTranslations() {
3620
- return this.disableTrans;
3628
+ return !this.enableTranslations;
3621
3629
  }
3622
3630
  set disableTranslations(value) {
3623
- this.disableTrans = value;
3624
- this.events.languageChanged.emit(this.currentLang);
3631
+ this.enableTranslations = !value;
3625
3632
  }
3626
3633
  get httpClient() {
3627
3634
  return this.client;
@@ -3640,7 +3647,7 @@ class StaticLanguageService {
3640
3647
  this.client = client;
3641
3648
  this.editLang = null;
3642
3649
  this.currentLang = null;
3643
- this.disableTrans = false;
3650
+ this.enableTrans = true;
3644
3651
  this.languageList = [];
3645
3652
  this.translations = {
3646
3653
  none: {}
@@ -3662,16 +3669,24 @@ class StaticLanguageService {
3662
3669
  this.replaceLanguages(this.languageList.concat(languages));
3663
3670
  }
3664
3671
  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`);
3672
+ if (!key)
3673
+ return "";
3674
+ try {
3675
+ const lowerKey = key.toLocaleLowerCase();
3676
+ const dict = this.dictionary;
3677
+ if (lowerKey in dict && this.enableTranslations) {
3678
+ return this.interpolate(dict[lowerKey], params);
3679
+ }
3680
+ return this.interpolate(key, params);
3672
3681
  }
3673
- const translation = ObjectUtils.getValue(this.dictionary, key, key) || key;
3674
- return this.promises.resolve(this.interpolate(translation, params));
3682
+ catch (reason) {
3683
+ console.warn("ERROR IN TRANSLATIONS", reason);
3684
+ return key;
3685
+ }
3686
+ }
3687
+ async getTranslation(key, params = null) {
3688
+ await this.loadDictionary();
3689
+ return this.getTranslationSync(key, params);
3675
3690
  }
3676
3691
  getTranslations(...keys) {
3677
3692
  return this.promises.create(resolve => {
@@ -3692,6 +3707,16 @@ class StaticLanguageService {
3692
3707
  const translation = translations ? translations.find(t => t.lang == lang) : null;
3693
3708
  return this.interpolate(translation ? translation.translation : "", params);
3694
3709
  }
3710
+ async loadDictionary() {
3711
+ return this.dictionary;
3712
+ }
3713
+ setDictionary(lang, dictionary) {
3714
+ this.translations[lang] = Object.keys(dictionary || {}).reduce((res, key) => {
3715
+ res[key.toLocaleLowerCase()] = dictionary[key];
3716
+ return res;
3717
+ }, {});
3718
+ return this.translations[lang];
3719
+ }
3695
3720
  interpolate(expr, params) {
3696
3721
  if (typeof expr === "string") {
3697
3722
  return this.interpolateString(expr, params);
@@ -3766,6 +3791,7 @@ class LanguageService extends StaticLanguageService {
3766
3791
  }));
3767
3792
  }
3768
3793
  initService() {
3794
+ super.initService();
3769
3795
  this.client.setExtraRequestParam("language", "de");
3770
3796
  this.translationRequests = {};
3771
3797
  this.languageSettings = new BehaviorSubject(null);
@@ -3798,22 +3824,6 @@ class LanguageService extends StaticLanguageService {
3798
3824
  await this.useLanguage(lang);
3799
3825
  this.events.languageChanged.emit(lang);
3800
3826
  }
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
3827
  async useLanguage(lang) {
3818
3828
  lang = this.languages.indexOf(lang) < 0 ? this.languages[0] : lang;
3819
3829
  this.client.setExtraRequestParam("language", lang);
@@ -3821,22 +3831,18 @@ class LanguageService extends StaticLanguageService {
3821
3831
  return this.dictionary;
3822
3832
  this.storage.set("language", lang);
3823
3833
  this.currentLang = lang;
3824
- const dict = await this.loadDictionary();
3825
- this.translations[lang] = dict;
3826
- return dict;
3834
+ return this.loadDictionary();
3827
3835
  }
3828
3836
  loadDictionary() {
3829
3837
  const lang = this.currentLanguage;
3830
3838
  this.translationRequests[lang] = this.translationRequests[lang] || new Promise(resolve => {
3831
3839
  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({});
3840
+ const request = this.httpClient.get(`${this.config.translationUrl}${lang}${ext}`);
3841
+ firstValueFrom(request).then(response => {
3842
+ resolve(this.setDictionary(lang, response));
3843
+ }, reason => {
3844
+ console.warn("ERROR IN TRANSLATIONS", reason);
3845
+ resolve(this.setDictionary(lang, {}));
3840
3846
  });
3841
3847
  });
3842
3848
  return this.translationRequests[lang];
@@ -5172,9 +5178,9 @@ class TranslatePipe {
5172
5178
  this.lang = lang;
5173
5179
  dirty = true;
5174
5180
  }
5175
- const disabled = this.language.disableTranslations;
5176
- if (this.disabled !== disabled) {
5177
- this.disabled = disabled;
5181
+ const enabled = this.language.enableTranslations;
5182
+ if (this.enabled !== enabled) {
5183
+ this.enabled = enabled;
5178
5184
  dirty = true;
5179
5185
  }
5180
5186
  if (!ObjectUtils.equals(this.query, query)) {
@@ -5207,10 +5213,6 @@ class TranslatePipe {
5207
5213
  this.lastValue = Array.isArray(query) ? this.language.getTranslationFromArray(query, this.params, lang) : this.language.getTranslationFromObject(query, this.params, lang);
5208
5214
  return this.lastValue;
5209
5215
  }
5210
- if (this.disabled) {
5211
- this.lastValue = query;
5212
- return this.lastValue;
5213
- }
5214
5216
  this.lastValue = this.language.getTranslationSync(query, this.params);
5215
5217
  this.language.getTranslation(query, this.params).then(value => {
5216
5218
  this.lastValue = value;