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