@tolgee/core 3.1.2 → 3.3.0-prerelease-07e7034.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.
@@ -398,6 +398,9 @@ var TranslationService = /** @class */ (function () {
398
398
  this.eventService = eventService;
399
399
  this.translationsCache = new Map();
400
400
  this.fetchPromises = {};
401
+ // we need to distinguish which languages are in cache initially
402
+ // because we need to refetch them in dev mode
403
+ this.fetchedDev = {};
401
404
  this.updateTranslationInCache = function (data) { return __awaiter(_this, void 0, void 0, function () {
402
405
  var result;
403
406
  var _this = this;
@@ -490,8 +493,7 @@ var TranslationService = /** @class */ (function () {
490
493
  TranslationService.prototype.initStatic = function () {
491
494
  var _this = this;
492
495
  var _a;
493
- if (this.properties.config.mode === 'production' &&
494
- typeof ((_a = this.properties.config) === null || _a === void 0 ? void 0 : _a.staticData) === 'object') {
496
+ if (typeof ((_a = this.properties.config) === null || _a === void 0 ? void 0 : _a.staticData) === 'object') {
495
497
  Object.entries(this.properties.config.staticData).forEach(function (_a) {
496
498
  var _b = __read(_a, 2), language = _b[0], data = _b[1];
497
499
  //if not provider or promise then it is raw data
@@ -510,7 +512,7 @@ var TranslationService = /** @class */ (function () {
510
512
  return __generator(this, function (_a) {
511
513
  switch (_a.label) {
512
514
  case 0:
513
- if (!(this.translationsCache.get(lang) == undefined)) return [3 /*break*/, 2];
515
+ if (!this.isFetchNeeded(lang)) return [3 /*break*/, 2];
514
516
  if (!(this.fetchPromises[lang] instanceof Promise)) {
515
517
  this.fetchPromises[lang] = this.fetchTranslations(lang);
516
518
  }
@@ -529,25 +531,28 @@ var TranslationService = /** @class */ (function () {
529
531
  TranslationService.prototype.getTranslation = function (key, lang, defaultValue) {
530
532
  if (lang === void 0) { lang = this.properties.currentLanguage; }
531
533
  return __awaiter(this, void 0, void 0, function () {
532
- var message;
534
+ var message, fallbackLang;
533
535
  return __generator(this, function (_a) {
534
536
  switch (_a.label) {
535
537
  case 0:
536
- message = this.getFromCache(key, lang);
537
- if (!!message) return [3 /*break*/, 3];
538
+ if (!this.isFetchNeeded(lang)) return [3 /*break*/, 2];
538
539
  return [4 /*yield*/, this.loadTranslations(lang)];
539
540
  case 1:
540
541
  _a.sent();
542
+ _a.label = 2;
543
+ case 2:
541
544
  message = this.getFromCache(key, lang);
542
- if (!!message) return [3 /*break*/, 3];
543
- message = this.getFromCache(key, this.properties.config.fallbackLanguage);
544
- if (!!message) return [3 /*break*/, 3];
545
+ if (!!message) return [3 /*break*/, 5];
546
+ fallbackLang = this.properties.config.fallbackLanguage;
547
+ if (!this.isFetchNeeded(fallbackLang)) return [3 /*break*/, 4];
545
548
  return [4 /*yield*/, this.loadTranslations(this.properties.config.fallbackLanguage)];
546
- case 2:
549
+ case 3:
547
550
  _a.sent();
551
+ _a.label = 4;
552
+ case 4:
548
553
  message = this.getFromCache(key, this.properties.config.fallbackLanguage);
549
- _a.label = 3;
550
- case 3: return [2 /*return*/, TranslationService.translationByValue(message, defaultValue)];
554
+ _a.label = 5;
555
+ case 5: return [2 /*return*/, TranslationService.translationByValue(message, defaultValue)];
551
556
  }
552
557
  });
553
558
  });
@@ -682,12 +687,20 @@ var TranslationService = /** @class */ (function () {
682
687
  this.getFromCache(key, this.properties.config.fallbackLanguage);
683
688
  return TranslationService.translationByValue(message, defaultValue);
684
689
  };
690
+ TranslationService.prototype.isFetchNeeded = function (lang) {
691
+ var isDevMode = this.properties.config.mode === 'development';
692
+ var dataPresent = this.translationsCache.get(lang) !== undefined;
693
+ var devFetched = Boolean(this.fetchedDev[lang]);
694
+ return (isDevMode && !devFetched) || !dataPresent;
695
+ };
685
696
  TranslationService.prototype.fetchTranslations = function (lang) {
686
697
  return __awaiter(this, void 0, void 0, function () {
698
+ var isDevMode;
687
699
  return __generator(this, function (_a) {
688
700
  switch (_a.label) {
689
701
  case 0:
690
- if (!(this.properties.config.mode === 'development')) return [3 /*break*/, 2];
702
+ isDevMode = this.properties.config.mode === 'development';
703
+ if (!isDevMode) return [3 /*break*/, 2];
691
704
  return [4 /*yield*/, this.fetchTranslationsDevelopment(lang)];
692
705
  case 1: return [2 /*return*/, _a.sent()];
693
706
  case 2: return [4 /*yield*/, this.fetchTranslationsProduction(lang)];
@@ -772,6 +785,7 @@ var TranslationService = /** @class */ (function () {
772
785
  return [4 /*yield*/, this.apiHttpService.fetchJson("v2/projects/translations/".concat(language))];
773
786
  case 3:
774
787
  data = _a.sent();
788
+ this.fetchedDev[language] = true;
775
789
  this.setLanguageData(language, data[language] || {});
776
790
  return [3 /*break*/, 5];
777
791
  case 4:
@@ -1074,37 +1088,38 @@ var TranslationHighlighter = /** @class */ (function () {
1074
1088
  }); }); });
1075
1089
  };
1076
1090
  TranslationHighlighter.prototype.getRenderer = function () {
1091
+ var _a;
1077
1092
  return __awaiter(this, void 0, void 0, function () {
1078
1093
  var possibleProviders, possibleProviders_1, possibleProviders_1_1, possibleProvider, constructorProvider, constructor, constructor, e_2_1;
1079
- var e_2, _a;
1080
- return __generator(this, function (_b) {
1081
- switch (_b.label) {
1094
+ var e_2, _b;
1095
+ return __generator(this, function (_c) {
1096
+ switch (_c.label) {
1082
1097
  case 0:
1083
1098
  if (!(this._renderer === undefined)) return [3 /*break*/, 11];
1084
1099
  possibleProviders = [
1085
1100
  this.dependencies.properties.config.ui,
1086
- window['@tolgee/ui'],
1101
+ (_a = window['@tolgee/ui']) === null || _a === void 0 ? void 0 : _a.UI,
1087
1102
  ];
1088
- _b.label = 1;
1103
+ _c.label = 1;
1089
1104
  case 1:
1090
- _b.trys.push([1, 8, 9, 10]);
1105
+ _c.trys.push([1, 8, 9, 10]);
1091
1106
  possibleProviders_1 = __values(possibleProviders), possibleProviders_1_1 = possibleProviders_1.next();
1092
- _b.label = 2;
1107
+ _c.label = 2;
1093
1108
  case 2:
1094
1109
  if (!!possibleProviders_1_1.done) return [3 /*break*/, 7];
1095
1110
  possibleProvider = possibleProviders_1_1.value;
1096
1111
  if (!(typeof possibleProvider === 'function')) return [3 /*break*/, 6];
1097
- _b.label = 3;
1112
+ _c.label = 3;
1098
1113
  case 3:
1099
- _b.trys.push([3, 5, , 6]);
1114
+ _c.trys.push([3, 5, , 6]);
1100
1115
  constructorProvider = possibleProvider;
1101
1116
  return [4 /*yield*/, constructorProvider()];
1102
1117
  case 4:
1103
- constructor = _b.sent();
1118
+ constructor = _c.sent();
1104
1119
  this._renderer = new constructor(this.dependencies);
1105
1120
  return [3 /*break*/, 6];
1106
1121
  case 5:
1107
- _b.sent();
1122
+ _c.sent();
1108
1123
  constructor = possibleProvider;
1109
1124
  this._renderer = new constructor(this.dependencies);
1110
1125
  return [3 /*break*/, 6];
@@ -1113,12 +1128,12 @@ var TranslationHighlighter = /** @class */ (function () {
1113
1128
  return [3 /*break*/, 2];
1114
1129
  case 7: return [3 /*break*/, 10];
1115
1130
  case 8:
1116
- e_2_1 = _b.sent();
1131
+ e_2_1 = _c.sent();
1117
1132
  e_2 = { error: e_2_1 };
1118
1133
  return [3 /*break*/, 10];
1119
1134
  case 9:
1120
1135
  try {
1121
- if (possibleProviders_1_1 && !possibleProviders_1_1.done && (_a = possibleProviders_1.return)) _a.call(possibleProviders_1);
1136
+ if (possibleProviders_1_1 && !possibleProviders_1_1.done && (_b = possibleProviders_1.return)) _b.call(possibleProviders_1);
1122
1137
  }
1123
1138
  finally { if (e_2) throw e_2.error; }
1124
1139
  return [7 /*endfinally*/];
@@ -1128,7 +1143,7 @@ var TranslationHighlighter = /** @class */ (function () {
1128
1143
  console.warn('Tolgee UI is not provided. To translate interactively provide tolgee ui constructor to "ui" configuration property. ' +
1129
1144
  'To disable highlighting use production mode.');
1130
1145
  }
1131
- _b.label = 11;
1146
+ _c.label = 11;
1132
1147
  case 11: return [2 /*return*/, this._renderer];
1133
1148
  }
1134
1149
  });
@@ -1598,6 +1613,8 @@ var CoreService = /** @class */ (function () {
1598
1613
  return CoreService;
1599
1614
  }());
1600
1615
 
1616
+ var API_KEY_LOCAL_STORAGE = '__tolgee_apiKey';
1617
+ var API_URL_LOCAL_STORAGE = '__tolgee_apiUrl';
1601
1618
  var DEFAULT_TARGET_ELEMENT_SUPPLIER = function () {
1602
1619
  if (typeof document !== 'undefined') {
1603
1620
  return document.body;
@@ -1605,7 +1622,6 @@ var DEFAULT_TARGET_ELEMENT_SUPPLIER = function () {
1605
1622
  };
1606
1623
  var TolgeeConfig = /** @class */ (function () {
1607
1624
  function TolgeeConfig(config) {
1608
- this.apiUrl = 'https://app.tolgee.io';
1609
1625
  this.inputPrefix = '%-%tolgee:';
1610
1626
  this.inputSuffix = '%-%';
1611
1627
  this.defaultLanguage = 'en';
@@ -1647,6 +1663,12 @@ var TolgeeConfig = /** @class */ (function () {
1647
1663
  },
1648
1664
  });
1649
1665
  Object.assign(this, config || {});
1666
+ if (typeof sessionStorage !== 'undefined') {
1667
+ this.apiUrl =
1668
+ sessionStorage.getItem(API_URL_LOCAL_STORAGE) || this.apiUrl;
1669
+ this.apiKey =
1670
+ sessionStorage.getItem(API_KEY_LOCAL_STORAGE) || this.apiKey;
1671
+ }
1650
1672
  if (this._targetElement === undefined) {
1651
1673
  this._targetElement = DEFAULT_TARGET_ELEMENT_SUPPLIER();
1652
1674
  }
@@ -1712,7 +1734,7 @@ var PluginManager = /** @class */ (function () {
1712
1734
  this.handshake = function () {
1713
1735
  var sharedConfiguration = __assign(__assign({}, _this.properties), { config: __assign(__assign({}, _this.properties.config), {
1714
1736
  //remove properties, which cannot be sent by window.postMessage
1715
- staticData: undefined, targetElement: undefined, _targetElement: undefined, ui: undefined }) });
1737
+ staticData: undefined, targetElement: undefined, _targetElement: undefined, ui: undefined }), uiPresent: Boolean(_this.properties.config.ui), uiVersion: "3.3.0-prerelease-07e7034.0" });
1716
1738
  var timer = null;
1717
1739
  var ping = function () {
1718
1740
  _this.messages.send('TOLGEE_READY', sharedConfiguration);
@@ -2968,19 +2990,16 @@ var Tolgee = /** @class */ (function () {
2968
2990
  * fetching so initial loading is not needed at all.
2969
2991
  */
2970
2992
  get: function () {
2971
- var _a, _b, _c, _d;
2972
- if (((_b = (_a = this.properties) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.mode) !== 'production') {
2973
- return true;
2974
- }
2993
+ var _a, _b;
2975
2994
  var currentLang = this.properties.currentLanguage;
2976
2995
  var fallbackLang = this.properties.config.fallbackLanguage;
2977
2996
  var fallbackPreloading = this.properties.config.preloadFallback;
2978
2997
  var isStaticDataProvided = function (data) {
2979
2998
  return data !== undefined && typeof data !== 'function';
2980
2999
  };
2981
- return (!isStaticDataProvided((_c = this.properties.config.staticData) === null || _c === void 0 ? void 0 : _c[currentLang]) ||
3000
+ return (!isStaticDataProvided((_a = this.properties.config.staticData) === null || _a === void 0 ? void 0 : _a[currentLang]) ||
2982
3001
  (!!fallbackPreloading &&
2983
- !isStaticDataProvided((_d = this.properties.config.staticData) === null || _d === void 0 ? void 0 : _d[fallbackLang])));
3002
+ !isStaticDataProvided((_b = this.properties.config.staticData) === null || _b === void 0 ? void 0 : _b[fallbackLang])));
2984
3003
  },
2985
3004
  enumerable: false,
2986
3005
  configurable: true
@@ -3030,28 +3049,40 @@ var Tolgee = /** @class */ (function () {
3030
3049
  };
3031
3050
  Tolgee.prototype.run = function () {
3032
3051
  return __awaiter(this, void 0, void 0, function () {
3052
+ var e_1;
3033
3053
  return __generator(this, function (_a) {
3034
3054
  switch (_a.label) {
3035
3055
  case 0:
3036
3056
  this.dependencyService.run();
3037
- if (!(this.properties.config.mode === 'development')) return [3 /*break*/, 2];
3038
- return [4 /*yield*/, this.coreService.loadApiKeyDetails()];
3057
+ if (!(this.properties.config.mode === 'development')) return [3 /*break*/, 4];
3058
+ _a.label = 1;
3039
3059
  case 1:
3060
+ _a.trys.push([1, 3, , 4]);
3061
+ return [4 /*yield*/, this.coreService.loadApiKeyDetails()];
3062
+ case 2:
3040
3063
  _a.sent();
3041
- _a.label = 2;
3042
- case 2: return [4 /*yield*/, this.dependencyService.translationService.loadTranslations()];
3064
+ return [3 /*break*/, 4];
3043
3065
  case 3:
3066
+ e_1 = _a.sent();
3067
+ // eslint-disable-next-line no-console
3068
+ console.error("Couldn't connect to tolgee");
3069
+ // eslint-disable-next-line no-console
3070
+ console.error(e_1);
3071
+ this.properties.config.mode = 'production';
3072
+ return [3 /*break*/, 4];
3073
+ case 4: return [4 /*yield*/, this.dependencyService.translationService.loadTranslations()];
3074
+ case 5:
3044
3075
  _a.sent();
3045
3076
  return [4 /*yield*/, this.dependencyService.pluginManager.run()];
3046
- case 4:
3077
+ case 6:
3047
3078
  _a.sent();
3048
- if (!this.properties.config.preloadFallback) return [3 /*break*/, 6];
3079
+ if (!this.properties.config.preloadFallback) return [3 /*break*/, 8];
3049
3080
  return [4 /*yield*/, this.dependencyService.translationService.loadTranslations(this.properties.config.fallbackLanguage)];
3050
- case 5:
3051
- _a.sent();
3052
- _a.label = 6;
3053
- case 6: return [4 /*yield*/, this.refresh()];
3054
3081
  case 7:
3082
+ _a.sent();
3083
+ _a.label = 8;
3084
+ case 8: return [4 /*yield*/, this.refresh()];
3085
+ case 9:
3055
3086
  _a.sent();
3056
3087
  if (this.properties.config.watch) {
3057
3088
  this.dependencyService.observer.observe();