@tolgee/core 3.0.1 → 3.1.1

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.
@@ -764,23 +764,25 @@
764
764
  var data, e_4;
765
765
  return __generator(this, function (_a) {
766
766
  switch (_a.label) {
767
- case 0:
768
- this.coreService.checkScope('translations.view');
769
- _a.label = 1;
767
+ case 0: return [4 /*yield*/, this.coreService.loadApiKeyDetails()];
770
768
  case 1:
771
- _a.trys.push([1, 3, , 4]);
772
- return [4 /*yield*/, this.apiHttpService.fetchJson("v2/projects/translations/".concat(language))];
769
+ _a.sent();
770
+ this.coreService.checkScope('translations.view');
771
+ _a.label = 2;
773
772
  case 2:
773
+ _a.trys.push([2, 4, , 5]);
774
+ return [4 /*yield*/, this.apiHttpService.fetchJson("v2/projects/translations/".concat(language))];
775
+ case 3:
774
776
  data = _a.sent();
775
777
  this.setLanguageData(language, data[language] || {});
776
- return [3 /*break*/, 4];
777
- case 3:
778
+ return [3 /*break*/, 5];
779
+ case 4:
778
780
  e_4 = _a.sent();
779
781
  // eslint-disable-next-line no-console
780
782
  console.error('Error while fetching localization data from API.', e_4);
781
783
  this.setEmptyLanguageData(language);
782
784
  return [2 /*return*/];
783
- case 4: return [2 /*return*/];
785
+ case 5: return [2 /*return*/];
784
786
  }
785
787
  });
786
788
  });
@@ -1577,6 +1579,24 @@
1577
1579
  throw new Error("Api key not permitted to do this, please add 'translations.view' scope.");
1578
1580
  }
1579
1581
  };
1582
+ CoreService.prototype.loadApiKeyDetails = function () {
1583
+ return __awaiter(this, void 0, void 0, function () {
1584
+ var details;
1585
+ return __generator(this, function (_a) {
1586
+ switch (_a.label) {
1587
+ case 0:
1588
+ if (!(this.properties.scopes === undefined)) return [3 /*break*/, 2];
1589
+ return [4 /*yield*/, this.getApiKeyDetails()];
1590
+ case 1:
1591
+ details = _a.sent();
1592
+ this.properties.scopes = details.scopes;
1593
+ this.properties.projectId = details.projectId;
1594
+ _a.label = 2;
1595
+ case 2: return [2 /*return*/];
1596
+ }
1597
+ });
1598
+ });
1599
+ };
1580
1600
  return CoreService;
1581
1601
  }());
1582
1602
 
@@ -2226,51 +2246,39 @@
2226
2246
  return result;
2227
2247
  };
2228
2248
  Coder.prototype.unwrap = function (text) {
2229
- return __awaiter(this, void 0, void 0, function () {
2230
- var matchRegexp, keysAndParams, matched, match, start, result, pre, _a, fullMatch, wrapped, unwrapped, index, input, translated;
2231
- return __generator(this, function (_b) {
2232
- switch (_b.label) {
2233
- case 0:
2234
- matchRegexp = new RegExp(this.rawUnWrapRegex, 'gs');
2235
- keysAndParams = [];
2236
- matched = false;
2237
- start = 0;
2238
- result = '';
2239
- _b.label = 1;
2240
- case 1:
2241
- if (!((match = matchRegexp.exec(text)) !== null)) return [3 /*break*/, 3];
2242
- pre = match[1];
2243
- _a = __read(match, 4), fullMatch = _a[0], wrapped = _a[2], unwrapped = _a[3];
2244
- index = match.index, input = match.input;
2245
- result += input.substr(start, index - start);
2246
- start = index + fullMatch.length;
2247
- if (pre === '\\') {
2248
- if (!TextHelper.isCharEscaped(index, text)) {
2249
- result += wrapped;
2250
- return [3 /*break*/, 1];
2251
- }
2252
- pre = '';
2253
- }
2254
- return [4 /*yield*/, this.getTranslatedWithMetadata(unwrapped)];
2255
- case 2:
2256
- translated = _b.sent();
2257
- keysAndParams.push({
2258
- key: translated.key,
2259
- params: translated.params,
2260
- defaultValue: translated.defaultValue,
2261
- });
2262
- matched = true;
2263
- result += pre + translated.translated;
2264
- return [3 /*break*/, 1];
2265
- case 3:
2266
- result += text.substring(start);
2267
- if (matched) {
2268
- return [2 /*return*/, { text: result, keys: keysAndParams }];
2269
- }
2270
- return [2 /*return*/, undefined];
2249
+ var matchRegexp = new RegExp(this.rawUnWrapRegex, 'gs');
2250
+ var keysAndParams = [];
2251
+ var matched = false;
2252
+ var match;
2253
+ var start = 0;
2254
+ var result = '';
2255
+ while ((match = matchRegexp.exec(text)) !== null) {
2256
+ var pre = match[1];
2257
+ var _a = __read(match, 4), fullMatch = _a[0]; _a[1]; var wrapped = _a[2], unwrapped = _a[3];
2258
+ var index = match.index, input = match.input;
2259
+ result += input.substr(start, index - start);
2260
+ start = index + fullMatch.length;
2261
+ if (pre === '\\') {
2262
+ if (!TextHelper.isCharEscaped(index, text)) {
2263
+ result += wrapped;
2264
+ continue;
2271
2265
  }
2266
+ pre = '';
2267
+ }
2268
+ var translated = this.getTranslatedWithMetadata(unwrapped);
2269
+ keysAndParams.push({
2270
+ key: translated.key,
2271
+ params: translated.params,
2272
+ defaultValue: translated.defaultValue,
2272
2273
  });
2273
- });
2274
+ matched = true;
2275
+ result += pre + translated.translated;
2276
+ }
2277
+ result += text.substring(start);
2278
+ if (matched) {
2279
+ return { text: result, keys: keysAndParams };
2280
+ }
2281
+ return undefined;
2274
2282
  };
2275
2283
  Coder.prototype.wrap = function (key, params, defaultValue) {
2276
2284
  var _this = this;
@@ -2287,19 +2295,9 @@
2287
2295
  return "".concat(this.properties.config.inputPrefix).concat(this.escapeParam(key)).concat(defaultString).concat(paramString).concat(this.properties.config.inputSuffix);
2288
2296
  };
2289
2297
  Coder.prototype.getTranslatedWithMetadata = function (text) {
2290
- return __awaiter(this, void 0, void 0, function () {
2291
- var _a, key, params, defaultValue, translated;
2292
- return __generator(this, function (_b) {
2293
- switch (_b.label) {
2294
- case 0:
2295
- _a = Coder.parseUnwrapped(text), key = _a.key, params = _a.params, defaultValue = _a.defaultValue;
2296
- return [4 /*yield*/, this.textService.translate(key, params, undefined, false, defaultValue)];
2297
- case 1:
2298
- translated = _b.sent();
2299
- return [2 /*return*/, { translated: translated, key: key, params: params, defaultValue: defaultValue }];
2300
- }
2301
- });
2302
- });
2298
+ var _a = Coder.parseUnwrapped(text), key = _a.key, params = _a.params, defaultValue = _a.defaultValue;
2299
+ var translated = this.textService.instant(key, params, undefined, false, defaultValue);
2300
+ return { translated: translated, key: key, params: params, defaultValue: defaultValue };
2303
2301
  };
2304
2302
  return Coder;
2305
2303
  }());
@@ -2734,31 +2732,26 @@
2734
2732
  this.defaultMemory = new ValueMemory();
2735
2733
  }
2736
2734
  Coder.prototype.unwrap = function (text) {
2737
- return __awaiter(this, void 0, void 0, function () {
2738
- var keysAndParams, messages, result;
2739
- var _this = this;
2740
- return __generator(this, function (_a) {
2741
- keysAndParams = [];
2742
- messages = decodeFromText(text);
2743
- messages.forEach(function (msg) {
2744
- var _a = __read(stringToCodePoints(msg), 2), keyCode = _a[0], defaultCode = _a[1];
2745
- var key = _this.keyMemory.numberToValue(keyCode);
2746
- var defaultValue = defaultCode !== undefined
2747
- ? _this.defaultMemory.numberToValue(defaultCode)
2748
- : undefined;
2749
- keysAndParams.push({
2750
- key: key,
2751
- params: undefined,
2752
- defaultValue: defaultValue,
2753
- });
2754
- });
2755
- result = removeSecrets(text);
2756
- if (keysAndParams.length) {
2757
- return [2 /*return*/, { text: result, keys: keysAndParams }];
2758
- }
2759
- return [2 /*return*/, undefined];
2735
+ var _this = this;
2736
+ var keysAndParams = [];
2737
+ var messages = decodeFromText(text);
2738
+ messages.forEach(function (msg) {
2739
+ var _a = __read(stringToCodePoints(msg), 2), keyCode = _a[0], defaultCode = _a[1];
2740
+ var key = _this.keyMemory.numberToValue(keyCode);
2741
+ var defaultValue = defaultCode !== undefined
2742
+ ? _this.defaultMemory.numberToValue(defaultCode)
2743
+ : undefined;
2744
+ keysAndParams.push({
2745
+ key: key,
2746
+ params: undefined,
2747
+ defaultValue: defaultValue,
2760
2748
  });
2761
2749
  });
2750
+ var result = removeSecrets(text);
2751
+ if (keysAndParams.length) {
2752
+ return { text: result, keys: keysAndParams };
2753
+ }
2754
+ return undefined;
2762
2755
  };
2763
2756
  Coder.prototype.wrap = function (key, _params, defaultValue, translation) {
2764
2757
  if (defaultValue === void 0) { defaultValue = undefined; }
@@ -2906,20 +2899,6 @@
2906
2899
  };
2907
2900
  this.dependencyService = new DependencyService();
2908
2901
  }
2909
- Tolgee.use = function (module) {
2910
- return new Tolgee().use(module);
2911
- };
2912
- Tolgee.init = function (config) {
2913
- return new Tolgee().init(config);
2914
- };
2915
- Tolgee.prototype.use = function (module) {
2916
- this.dependencyService.moduleService.addModule(module);
2917
- return this;
2918
- };
2919
- Tolgee.prototype.init = function (config) {
2920
- this.dependencyService.init(config);
2921
- return this;
2922
- };
2923
2902
  Object.defineProperty(Tolgee.prototype, "properties", {
2924
2903
  get: function () {
2925
2904
  return this.dependencyService.properties;
@@ -2934,21 +2913,24 @@
2934
2913
  enumerable: false,
2935
2914
  configurable: true
2936
2915
  });
2937
- Object.defineProperty(Tolgee.prototype, "coreService", {
2938
- get: function () {
2939
- return this.dependencyService.coreService;
2940
- },
2941
- enumerable: false,
2942
- configurable: true
2943
- });
2944
2916
  Object.defineProperty(Tolgee.prototype, "lang", {
2945
2917
  get: function () {
2946
2918
  return this.properties.currentLanguage;
2947
2919
  },
2948
- set: function (value) {
2949
- this.properties.currentLanguage = value;
2950
- this.dependencyService.eventService
2951
- .LANGUAGE_CHANGED.emit(value);
2920
+ /**
2921
+ * This sets a new language.
2922
+ *
2923
+ * Using this setter can behave buggy when you change languages
2924
+ * too fast, since it changes the language property before
2925
+ * translations are actually loaded.
2926
+ * @Deprecated use asynchronous changeLanguage method.
2927
+ */
2928
+ set: function (newLanguage) {
2929
+ var _this = this;
2930
+ this.properties.currentLanguage = newLanguage;
2931
+ this.translationService.loadTranslations(newLanguage).then(function () {
2932
+ _this.emitLangChangeEvent(newLanguage);
2933
+ });
2952
2934
  },
2953
2935
  enumerable: false,
2954
2936
  configurable: true
@@ -2975,6 +2957,10 @@
2975
2957
  configurable: true
2976
2958
  });
2977
2959
  Object.defineProperty(Tolgee.prototype, "onLangLoaded", {
2960
+ /**
2961
+ * Is emitted when language translations are loaded for the first time.
2962
+ * It is not emitted when language is changed and translations were loaded before.
2963
+ */
2978
2964
  get: function () {
2979
2965
  return this.dependencyService.eventService.LANGUAGE_LOADED;
2980
2966
  },
@@ -3006,6 +2992,49 @@
3006
2992
  enumerable: false,
3007
2993
  configurable: true
3008
2994
  });
2995
+ Object.defineProperty(Tolgee.prototype, "coreService", {
2996
+ get: function () {
2997
+ return this.dependencyService.coreService;
2998
+ },
2999
+ enumerable: false,
3000
+ configurable: true
3001
+ });
3002
+ Tolgee.use = function (module) {
3003
+ return new Tolgee().use(module);
3004
+ };
3005
+ Tolgee.init = function (config) {
3006
+ return new Tolgee().init(config);
3007
+ };
3008
+ /**
3009
+ * Sets the new language.
3010
+ *
3011
+ * Emits the onLangChange and onLangChangeAndLoad events after
3012
+ * the translations are loaded.
3013
+ *
3014
+ * @return Promise<void> Resolves when translations are loaded
3015
+ */
3016
+ Tolgee.prototype.changeLanguage = function (newLanguage) {
3017
+ return __awaiter(this, void 0, void 0, function () {
3018
+ return __generator(this, function (_a) {
3019
+ switch (_a.label) {
3020
+ case 0: return [4 /*yield*/, this.translationService.loadTranslations(newLanguage)];
3021
+ case 1:
3022
+ _a.sent();
3023
+ this.properties.currentLanguage = newLanguage;
3024
+ this.emitLangChangeEvent(newLanguage);
3025
+ return [2 /*return*/];
3026
+ }
3027
+ });
3028
+ });
3029
+ };
3030
+ Tolgee.prototype.use = function (module) {
3031
+ this.dependencyService.moduleService.addModule(module);
3032
+ return this;
3033
+ };
3034
+ Tolgee.prototype.init = function (config) {
3035
+ this.dependencyService.init(config);
3036
+ return this;
3037
+ };
3009
3038
  Tolgee.prototype.run = function () {
3010
3039
  return __awaiter(this, void 0, void 0, function () {
3011
3040
  return __generator(this, function (_a) {
@@ -3013,7 +3042,7 @@
3013
3042
  case 0:
3014
3043
  this.dependencyService.run();
3015
3044
  if (!(this.properties.config.mode === 'development')) return [3 /*break*/, 2];
3016
- return [4 /*yield*/, this.loadApiKeyDetails()];
3045
+ return [4 /*yield*/, this.coreService.loadApiKeyDetails()];
3017
3046
  case 1:
3018
3047
  _a.sent();
3019
3048
  _a.label = 2;
@@ -3067,18 +3096,15 @@
3067
3096
  props.defaultValue !== undefined ? props.defaultValue : defaultValue;
3068
3097
  orEmpty = props.orEmpty;
3069
3098
  }
3070
- if (!(this.properties.config.mode === 'development' && !noWrap)) return [3 /*break*/, 4];
3071
- return [4 /*yield*/, this.loadApiKeyDetails()];
3099
+ return [4 /*yield*/, this.dependencyService.textService.translate(key, params, undefined, orEmpty, defaultValue)];
3072
3100
  case 1:
3073
- _a.sent();
3074
- return [4 /*yield*/, this.translationService.loadTranslations()];
3101
+ translation = _a.sent();
3102
+ if (!(this.properties.config.mode === 'development' && !noWrap)) return [3 /*break*/, 3];
3103
+ return [4 /*yield*/, this.coreService.loadApiKeyDetails()];
3075
3104
  case 2:
3076
3105
  _a.sent();
3077
- return [4 /*yield*/, this.dependencyService.textService.translate(key, params, undefined, orEmpty, defaultValue)];
3078
- case 3:
3079
- translation = _a.sent();
3080
3106
  return [2 /*return*/, this.dependencyService.wrapper.wrap(key, params, defaultValue, translation)];
3081
- case 4: return [2 /*return*/, this.dependencyService.textService.translate(key, params, undefined, orEmpty, defaultValue)];
3107
+ case 3: return [2 /*return*/, translation];
3082
3108
  }
3083
3109
  });
3084
3110
  });
@@ -3111,23 +3137,9 @@
3111
3137
  }
3112
3138
  return translation;
3113
3139
  };
3114
- Tolgee.prototype.loadApiKeyDetails = function () {
3115
- return __awaiter(this, void 0, void 0, function () {
3116
- var details;
3117
- return __generator(this, function (_a) {
3118
- switch (_a.label) {
3119
- case 0:
3120
- if (!(this.properties.scopes === undefined)) return [3 /*break*/, 2];
3121
- return [4 /*yield*/, this.dependencyService.coreService.getApiKeyDetails()];
3122
- case 1:
3123
- details = _a.sent();
3124
- this.properties.scopes = details.scopes;
3125
- this.properties.projectId = details.projectId;
3126
- _a.label = 2;
3127
- case 2: return [2 /*return*/];
3128
- }
3129
- });
3130
- });
3140
+ Tolgee.prototype.emitLangChangeEvent = function (value) {
3141
+ var langChangedEmitter = this.onLangChange;
3142
+ langChangedEmitter.emit(value);
3131
3143
  };
3132
3144
  return Tolgee;
3133
3145
  }());