@tolgee/core 4.0.0 → 4.2.0

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.
@@ -128,40 +128,37 @@ var Properties = /** @class */ (function () {
128
128
  }
129
129
  Object.defineProperty(Properties.prototype, "currentLanguage", {
130
130
  get: function () {
131
- var result;
132
- if (this.config.forceLanguage) {
131
+ var _a;
132
+ if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.forceLanguage) {
133
133
  return this.config.forceLanguage;
134
134
  }
135
- if (typeof localStorage !== 'undefined') {
136
- var storedLanguage = localStorage.getItem(CURRENT_LANGUAGE_LOCAL_STORAGE_KEY);
135
+ if (this._currentLanguage) {
136
+ return this._currentLanguage;
137
+ }
138
+ if (this.config.enableLanguageStore) {
139
+ var storedLanguage = this.getStoredLanguage();
137
140
  if (storedLanguage) {
138
- result = storedLanguage;
139
- }
140
- if (this.config.availableLanguages) {
141
- var isSavedLanguageAvailable = this.config.availableLanguages.indexOf(result) > -1;
142
- if (!isSavedLanguageAvailable) {
143
- result = undefined;
144
- }
141
+ return storedLanguage;
145
142
  }
146
143
  }
147
- else {
148
- result = this._currentLanguage;
149
- }
150
- if (!result) {
151
- result = this.getLanguageByNavigator();
152
- this.currentLanguage = result;
144
+ if (this.config.enableLanguageDetection) {
145
+ var detectedLanguage = this.getLanguageByNavigator();
146
+ if (detectedLanguage) {
147
+ return detectedLanguage;
148
+ }
153
149
  }
154
- return result;
150
+ return this.config.defaultLanguage;
155
151
  },
156
152
  set: function (language) {
153
+ var _a;
157
154
  if (!language) {
158
155
  throw new Error("Setting invalid language value ".concat(language));
159
156
  }
160
- if (typeof localStorage === 'undefined') {
161
- this._currentLanguage = language;
162
- return;
157
+ this._currentLanguage = language;
158
+ if (((_a = this.config) === null || _a === void 0 ? void 0 : _a.enableLanguageStore) &&
159
+ typeof localStorage !== 'undefined') {
160
+ localStorage.setItem(CURRENT_LANGUAGE_LOCAL_STORAGE_KEY, language);
163
161
  }
164
- localStorage.setItem(CURRENT_LANGUAGE_LOCAL_STORAGE_KEY, language);
165
162
  },
166
163
  enumerable: false,
167
164
  configurable: true
@@ -176,6 +173,18 @@ var Properties = /** @class */ (function () {
176
173
  enumerable: false,
177
174
  configurable: true
178
175
  });
176
+ Properties.prototype.getStoredLanguage = function () {
177
+ if (typeof localStorage !== 'undefined') {
178
+ var storedLanguage = localStorage.getItem(CURRENT_LANGUAGE_LOCAL_STORAGE_KEY);
179
+ if (!this.config.availableLanguages) {
180
+ return storedLanguage;
181
+ }
182
+ var isSavedLanguageAvailable = this.config.availableLanguages.indexOf(storedLanguage) > -1;
183
+ if (isSavedLanguageAvailable) {
184
+ return storedLanguage;
185
+ }
186
+ }
187
+ };
179
188
  Properties.prototype.getLanguageByNavigator = function () {
180
189
  if (typeof window !== 'undefined' && this.config.availableLanguages) {
181
190
  var preferred_1 = window.navigator.language;
@@ -190,7 +199,6 @@ var Properties = /** @class */ (function () {
190
199
  return twoLetterMatch;
191
200
  }
192
201
  }
193
- return this.config.defaultLanguage;
194
202
  };
195
203
  return Properties;
196
204
  }());
@@ -1634,7 +1642,18 @@ var TolgeeConfig = /** @class */ (function () {
1634
1642
  function TolgeeConfig(config) {
1635
1643
  this.inputPrefix = '%-%tolgee:';
1636
1644
  this.inputSuffix = '%-%';
1645
+ /**
1646
+ * Used when auto detection is not available or is turned off
1647
+ */
1637
1648
  this.defaultLanguage = 'en';
1649
+ /**
1650
+ * Store user language in localStorage (default: true)
1651
+ */
1652
+ this.enableLanguageStore = true;
1653
+ /**
1654
+ * Use auto language detection by browser locale (default: true)
1655
+ */
1656
+ this.enableLanguageDetection = true;
1638
1657
  this.filesUrlPrefix = 'i18n/';
1639
1658
  this.tagAttributes = {
1640
1659
  textarea: ['placeholder'],
@@ -1747,7 +1766,7 @@ var PluginManager = /** @class */ (function () {
1747
1766
  this.handshake = function () {
1748
1767
  var sharedConfiguration = __assign(__assign({}, _this.properties), { config: __assign(__assign({}, _this.properties.config), {
1749
1768
  //remove properties, which cannot be sent by window.postMessage
1750
- staticData: undefined, targetElement: undefined, _targetElement: undefined, ui: undefined }), uiPresent: Boolean(_this.properties.config.ui), uiVersion: "4.0.0" });
1769
+ staticData: undefined, targetElement: undefined, _targetElement: undefined, ui: undefined }), uiPresent: Boolean(_this.properties.config.ui), uiVersion: "4.2.0" });
1751
1770
  var timer = null;
1752
1771
  var ping = function () {
1753
1772
  _this.messages.send('TOLGEE_READY', sharedConfiguration);