@tolgee/core 1.4.0 → 1.6.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.
- package/dist/tolgee.cjs.js +253 -171
- package/dist/tolgee.cjs.js.map +1 -1
- package/dist/tolgee.cjs.min.js +1 -1
- package/dist/tolgee.cjs.min.js.map +1 -1
- package/dist/tolgee.umd.js +253 -171
- package/dist/tolgee.umd.js.map +1 -1
- package/dist/tolgee.umd.min.js +16 -0
- package/dist/tolgee.umd.min.js.map +1 -0
- package/lib/Tolgee.d.ts +5 -3
- package/lib/Tolgee.js +51 -28
- package/lib/Tolgee.js.map +1 -1
- package/lib/TolgeeConfig.d.ts +2 -2
- package/lib/services/TextService.d.ts +3 -3
- package/lib/services/TextService.js +106 -24
- package/lib/services/TextService.js.map +1 -1
- package/lib/services/TranslationService.d.ts +3 -2
- package/lib/services/TranslationService.js +53 -77
- package/lib/services/TranslationService.js.map +1 -1
- package/lib/types.d.ts +18 -2
- package/package.json +2 -2
package/dist/tolgee.umd.js
CHANGED
|
@@ -549,63 +549,6 @@
|
|
|
549
549
|
return TranslationData;
|
|
550
550
|
}());
|
|
551
551
|
|
|
552
|
-
var TextHelper = /** @class */ (function () {
|
|
553
|
-
function TextHelper() {
|
|
554
|
-
}
|
|
555
|
-
TextHelper.splitOnNonEscapedDelimiter = function (string, delimiter) {
|
|
556
|
-
var result = [];
|
|
557
|
-
var actual = '';
|
|
558
|
-
var escaped = false;
|
|
559
|
-
for (var i = 0; i < string.length; i++) {
|
|
560
|
-
var char = string[i];
|
|
561
|
-
if (char === '\\' && !escaped) {
|
|
562
|
-
escaped = true;
|
|
563
|
-
continue;
|
|
564
|
-
}
|
|
565
|
-
if (escaped) {
|
|
566
|
-
escaped = false;
|
|
567
|
-
actual += char;
|
|
568
|
-
continue;
|
|
569
|
-
}
|
|
570
|
-
if (char === delimiter) {
|
|
571
|
-
result.push(actual);
|
|
572
|
-
actual = '';
|
|
573
|
-
continue;
|
|
574
|
-
}
|
|
575
|
-
actual += char;
|
|
576
|
-
}
|
|
577
|
-
result.push(actual);
|
|
578
|
-
return result;
|
|
579
|
-
};
|
|
580
|
-
TextHelper.isCharEscaped = function (position, fullString) {
|
|
581
|
-
var escapeCharsCount = 0;
|
|
582
|
-
while (position > -1 && fullString[position - 1] === '\\') {
|
|
583
|
-
escapeCharsCount++;
|
|
584
|
-
position--;
|
|
585
|
-
}
|
|
586
|
-
return escapeCharsCount % 2 == 1;
|
|
587
|
-
};
|
|
588
|
-
TextHelper.removeEscapes = function (string) {
|
|
589
|
-
var result = '';
|
|
590
|
-
var escaped = false;
|
|
591
|
-
for (var i = 0; i < string.length; i++) {
|
|
592
|
-
var char = string[i];
|
|
593
|
-
if (char === '\\' && !escaped) {
|
|
594
|
-
escaped = true;
|
|
595
|
-
continue;
|
|
596
|
-
}
|
|
597
|
-
if (escaped) {
|
|
598
|
-
escaped = false;
|
|
599
|
-
result += char;
|
|
600
|
-
continue;
|
|
601
|
-
}
|
|
602
|
-
result += char;
|
|
603
|
-
}
|
|
604
|
-
return result;
|
|
605
|
-
};
|
|
606
|
-
return TextHelper;
|
|
607
|
-
}());
|
|
608
|
-
|
|
609
552
|
var TranslationService = /** @class */ (function () {
|
|
610
553
|
function TranslationService(properties, coreService, apiHttpService, eventService) {
|
|
611
554
|
var _this = this;
|
|
@@ -618,47 +561,48 @@
|
|
|
618
561
|
this.getTranslationsOfKey = function (key, languages) {
|
|
619
562
|
if (languages === void 0) { languages = new Set([_this.properties.currentLanguage]); }
|
|
620
563
|
return __awaiter(_this, void 0, void 0, function () {
|
|
621
|
-
var languagesArray, languagesQuery, data, translationData_1, firstItem,
|
|
622
|
-
var _b, _c
|
|
623
|
-
return __generator(this, function (
|
|
624
|
-
switch (
|
|
564
|
+
var languagesArray, languagesQuery, data, translationData_1, firstItem, e_1, _a;
|
|
565
|
+
var _b, _c;
|
|
566
|
+
return __generator(this, function (_d) {
|
|
567
|
+
switch (_d.label) {
|
|
625
568
|
case 0:
|
|
626
569
|
this.coreService.checkScope('translations.view');
|
|
627
|
-
|
|
570
|
+
_d.label = 1;
|
|
628
571
|
case 1:
|
|
629
|
-
|
|
572
|
+
_d.trys.push([1, 3, , 7]);
|
|
630
573
|
languagesArray = __spreadArray([], __read(languages));
|
|
631
574
|
languagesQuery = languagesArray
|
|
632
575
|
.map(function (l) { return "languages=" + l; })
|
|
633
576
|
.join('&');
|
|
634
577
|
return [4 /*yield*/, this.apiHttpService.fetchJson("v2/projects/translations?" + languagesQuery + "&filterKeyName=" + encodeURIComponent(key))];
|
|
635
578
|
case 2:
|
|
636
|
-
data =
|
|
579
|
+
data = _d.sent();
|
|
637
580
|
translationData_1 = languagesArray.reduce(function (acc, curr) {
|
|
638
581
|
var _a;
|
|
639
582
|
return (__assign(__assign({}, acc), (_a = {}, _a[curr] = '', _a)));
|
|
640
583
|
}, {});
|
|
641
584
|
firstItem = (_c = (_b = data._embedded) === null || _b === void 0 ? void 0 : _b.keys) === null || _c === void 0 ? void 0 : _c[0];
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
Object.entries((_f = (_e = (_d = data._embedded) === null || _d === void 0 ? void 0 : _d.keys) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.translations).forEach(function (_a) {
|
|
585
|
+
if (firstItem === null || firstItem === void 0 ? void 0 : firstItem.translations) {
|
|
586
|
+
Object.entries(firstItem.translations).forEach(function (_a) {
|
|
645
587
|
var _b = __read(_a, 2), language = _b[0], translation = _b[1];
|
|
646
588
|
return (translationData_1[language] = translation.text);
|
|
647
589
|
});
|
|
648
590
|
}
|
|
649
591
|
return [2 /*return*/, new TranslationData(key, translationData_1, firstItem === null || firstItem === void 0 ? void 0 : firstItem.keyId)];
|
|
650
592
|
case 3:
|
|
651
|
-
e_1 =
|
|
593
|
+
e_1 = _d.sent();
|
|
652
594
|
if (!(e_1 instanceof ApiHttpError &&
|
|
653
595
|
e_1.response.status === 404 &&
|
|
654
596
|
e_1.code === 'language_not_found')) return [3 /*break*/, 5];
|
|
655
|
-
//only possible reason for this error is, that languages definition
|
|
597
|
+
// only possible reason for this error is, that languages definition
|
|
598
|
+
// is changed, but the old value is stored in preferred languages
|
|
656
599
|
_a = this.properties;
|
|
657
600
|
return [4 /*yield*/, this.coreService.getLanguages()];
|
|
658
601
|
case 4:
|
|
659
|
-
//only possible reason for this error is, that languages definition
|
|
602
|
+
// only possible reason for this error is, that languages definition
|
|
603
|
+
// is changed, but the old value is stored in preferred languages
|
|
660
604
|
_a.preferredLanguages =
|
|
661
|
-
|
|
605
|
+
_d.sent();
|
|
662
606
|
// eslint-disable-next-line no-console
|
|
663
607
|
console.error('Requested language not found, refreshing the page!');
|
|
664
608
|
location.reload();
|
|
@@ -671,15 +615,17 @@
|
|
|
671
615
|
});
|
|
672
616
|
};
|
|
673
617
|
}
|
|
674
|
-
TranslationService.translationByValue = function (message, key, orEmpty) {
|
|
618
|
+
TranslationService.translationByValue = function (message, key, orEmpty, defaultValue) {
|
|
675
619
|
if (message) {
|
|
676
620
|
return message;
|
|
677
621
|
}
|
|
622
|
+
if (defaultValue) {
|
|
623
|
+
return defaultValue;
|
|
624
|
+
}
|
|
678
625
|
if (orEmpty) {
|
|
679
626
|
return '';
|
|
680
627
|
}
|
|
681
|
-
|
|
682
|
-
return path[path.length - 1];
|
|
628
|
+
return key;
|
|
683
629
|
};
|
|
684
630
|
TranslationService.prototype.initStatic = function () {
|
|
685
631
|
var _this = this;
|
|
@@ -690,7 +636,7 @@
|
|
|
690
636
|
var _b = __read(_a, 2), language = _b[0], data = _b[1];
|
|
691
637
|
//if not provider or promise then it is raw data
|
|
692
638
|
if (typeof data !== 'function') {
|
|
693
|
-
_this.
|
|
639
|
+
_this.setLanguageData(language, data);
|
|
694
640
|
}
|
|
695
641
|
});
|
|
696
642
|
}
|
|
@@ -717,7 +663,7 @@
|
|
|
717
663
|
});
|
|
718
664
|
});
|
|
719
665
|
};
|
|
720
|
-
TranslationService.prototype.getTranslation = function (key, lang, orEmpty) {
|
|
666
|
+
TranslationService.prototype.getTranslation = function (key, lang, orEmpty, defaultValue) {
|
|
721
667
|
if (lang === void 0) { lang = this.properties.currentLanguage; }
|
|
722
668
|
if (orEmpty === void 0) { orEmpty = false; }
|
|
723
669
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -739,7 +685,7 @@
|
|
|
739
685
|
_a.sent();
|
|
740
686
|
message = this.getFromCache(key, this.properties.config.fallbackLanguage);
|
|
741
687
|
_a.label = 3;
|
|
742
|
-
case 3: return [2 /*return*/, TranslationService.translationByValue(message, key, orEmpty)];
|
|
688
|
+
case 3: return [2 /*return*/, TranslationService.translationByValue(message, key, orEmpty, defaultValue)];
|
|
743
689
|
}
|
|
744
690
|
});
|
|
745
691
|
});
|
|
@@ -759,21 +705,9 @@
|
|
|
759
705
|
case 1:
|
|
760
706
|
result = _a.sent();
|
|
761
707
|
Object.keys(translationData.translations).forEach(function (lang) {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
var root = _this.translationsCache.get(lang);
|
|
766
|
-
for (var i = 0; i < path.length; i++) {
|
|
767
|
-
var item = path[i];
|
|
768
|
-
if (root[item] === undefined) {
|
|
769
|
-
root[item] = {};
|
|
770
|
-
}
|
|
771
|
-
if (i === path.length - 1) {
|
|
772
|
-
root[item] = translationData.translations[lang];
|
|
773
|
-
return;
|
|
774
|
-
}
|
|
775
|
-
root = root[item];
|
|
776
|
-
}
|
|
708
|
+
var data = _this.translationsCache.get(lang);
|
|
709
|
+
if (data) {
|
|
710
|
+
data[translationData.key] = translationData.translations[lang];
|
|
777
711
|
}
|
|
778
712
|
});
|
|
779
713
|
return [2 /*return*/, result];
|
|
@@ -798,12 +732,12 @@
|
|
|
798
732
|
});
|
|
799
733
|
});
|
|
800
734
|
};
|
|
801
|
-
TranslationService.prototype.getFromCacheOrFallback = function (key, lang, orEmpty) {
|
|
735
|
+
TranslationService.prototype.getFromCacheOrFallback = function (key, lang, orEmpty, defaultValue) {
|
|
802
736
|
if (lang === void 0) { lang = this.properties.currentLanguage; }
|
|
803
737
|
if (orEmpty === void 0) { orEmpty = false; }
|
|
804
738
|
var message = this.getFromCache(key, lang) ||
|
|
805
739
|
this.getFromCache(key, this.properties.config.fallbackLanguage);
|
|
806
|
-
return TranslationService.translationByValue(message, key, orEmpty);
|
|
740
|
+
return TranslationService.translationByValue(message, key, orEmpty, defaultValue);
|
|
807
741
|
};
|
|
808
742
|
TranslationService.prototype.fetchTranslations = function (lang) {
|
|
809
743
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -831,11 +765,11 @@
|
|
|
831
765
|
return [4 /*yield*/, langStaticData()];
|
|
832
766
|
case 1:
|
|
833
767
|
data = _c.sent();
|
|
834
|
-
this.
|
|
768
|
+
this.setLanguageData(language, data);
|
|
835
769
|
return [2 /*return*/];
|
|
836
770
|
case 2:
|
|
837
771
|
if (langStaticData !== undefined) {
|
|
838
|
-
this.
|
|
772
|
+
this.setLanguageData(language, langStaticData);
|
|
839
773
|
return [2 /*return*/];
|
|
840
774
|
}
|
|
841
775
|
_c.label = 3;
|
|
@@ -851,7 +785,7 @@
|
|
|
851
785
|
//on error set language data as empty object to not break the flow
|
|
852
786
|
// eslint-disable-next-line no-console
|
|
853
787
|
console.error('Server responded with error status while loading localization data.');
|
|
854
|
-
this.
|
|
788
|
+
this.setLanguageData(language, {});
|
|
855
789
|
return [2 /*return*/];
|
|
856
790
|
}
|
|
857
791
|
_c.label = 6;
|
|
@@ -860,7 +794,7 @@
|
|
|
860
794
|
return [4 /*yield*/, result.json()];
|
|
861
795
|
case 7:
|
|
862
796
|
data = _c.sent();
|
|
863
|
-
this.
|
|
797
|
+
this.setLanguageData(language, data);
|
|
864
798
|
return [3 /*break*/, 9];
|
|
865
799
|
case 8:
|
|
866
800
|
_c.sent();
|
|
@@ -893,7 +827,7 @@
|
|
|
893
827
|
return [4 /*yield*/, this.apiHttpService.fetchJson("v2/projects/translations/" + language)];
|
|
894
828
|
case 2:
|
|
895
829
|
data = _a.sent();
|
|
896
|
-
this.
|
|
830
|
+
this.setLanguageData(language, data[language] || {});
|
|
897
831
|
return [3 /*break*/, 4];
|
|
898
832
|
case 3:
|
|
899
833
|
e_4 = _a.sent();
|
|
@@ -909,38 +843,92 @@
|
|
|
909
843
|
TranslationService.prototype.setEmptyLanguageData = function (language) {
|
|
910
844
|
this.translationsCache.set(language, {});
|
|
911
845
|
};
|
|
846
|
+
TranslationService.prototype.setLanguageData = function (language, data) {
|
|
847
|
+
// recursively walk the tree and make it flat, when tree data are provided
|
|
848
|
+
var makeFlat = function (data) {
|
|
849
|
+
var result = {};
|
|
850
|
+
Object.entries(data).forEach(function (_a) {
|
|
851
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
852
|
+
if (typeof value === 'object') {
|
|
853
|
+
Object.entries(makeFlat(value)).forEach(function (_a) {
|
|
854
|
+
var _b = __read(_a, 2), flatKey = _b[0], flatValue = _b[1];
|
|
855
|
+
result[key + '.' + flatKey] = flatValue;
|
|
856
|
+
});
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
result[key] = value;
|
|
860
|
+
});
|
|
861
|
+
return result;
|
|
862
|
+
};
|
|
863
|
+
this.translationsCache.set(language, makeFlat(data));
|
|
864
|
+
};
|
|
912
865
|
TranslationService.prototype.getFromCache = function (key, lang) {
|
|
913
|
-
var e_5, _a;
|
|
914
866
|
if (lang === void 0) { lang = this.properties.currentLanguage; }
|
|
915
|
-
var path = TextHelper.splitOnNonEscapedDelimiter(key, '.');
|
|
916
867
|
var root = this.translationsCache.get(lang);
|
|
917
868
|
//if lang is not downloaded or does not exist at all
|
|
918
869
|
if (root === undefined) {
|
|
919
870
|
return undefined;
|
|
920
871
|
}
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
872
|
+
return root[key];
|
|
873
|
+
};
|
|
874
|
+
return TranslationService;
|
|
875
|
+
}());
|
|
876
|
+
|
|
877
|
+
var TextHelper = /** @class */ (function () {
|
|
878
|
+
function TextHelper() {
|
|
879
|
+
}
|
|
880
|
+
TextHelper.splitOnNonEscapedDelimiter = function (string, delimiter) {
|
|
881
|
+
var result = [];
|
|
882
|
+
var actual = '';
|
|
883
|
+
var escaped = false;
|
|
884
|
+
for (var i = 0; i < string.length; i++) {
|
|
885
|
+
var char = string[i];
|
|
886
|
+
if (char === '\\' && !escaped) {
|
|
887
|
+
escaped = true;
|
|
888
|
+
continue;
|
|
932
889
|
}
|
|
890
|
+
if (escaped) {
|
|
891
|
+
escaped = false;
|
|
892
|
+
actual += char;
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (char === delimiter) {
|
|
896
|
+
result.push(actual);
|
|
897
|
+
actual = '';
|
|
898
|
+
continue;
|
|
899
|
+
}
|
|
900
|
+
actual += char;
|
|
933
901
|
}
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
902
|
+
result.push(actual);
|
|
903
|
+
return result;
|
|
904
|
+
};
|
|
905
|
+
TextHelper.isCharEscaped = function (position, fullString) {
|
|
906
|
+
var escapeCharsCount = 0;
|
|
907
|
+
while (position > -1 && fullString[position - 1] === '\\') {
|
|
908
|
+
escapeCharsCount++;
|
|
909
|
+
position--;
|
|
910
|
+
}
|
|
911
|
+
return escapeCharsCount % 2 == 1;
|
|
912
|
+
};
|
|
913
|
+
TextHelper.removeEscapes = function (string) {
|
|
914
|
+
var result = '';
|
|
915
|
+
var escaped = false;
|
|
916
|
+
for (var i = 0; i < string.length; i++) {
|
|
917
|
+
var char = string[i];
|
|
918
|
+
if (char === '\\' && !escaped) {
|
|
919
|
+
escaped = true;
|
|
920
|
+
continue;
|
|
921
|
+
}
|
|
922
|
+
if (escaped) {
|
|
923
|
+
escaped = false;
|
|
924
|
+
result += char;
|
|
925
|
+
continue;
|
|
938
926
|
}
|
|
939
|
-
|
|
927
|
+
result += char;
|
|
940
928
|
}
|
|
941
|
-
return
|
|
929
|
+
return result;
|
|
942
930
|
};
|
|
943
|
-
return
|
|
931
|
+
return TextHelper;
|
|
944
932
|
}());
|
|
945
933
|
|
|
946
934
|
var ErrorKind;
|
|
@@ -3371,19 +3359,81 @@
|
|
|
3371
3359
|
enumerable: false,
|
|
3372
3360
|
configurable: true
|
|
3373
3361
|
});
|
|
3374
|
-
TextService.parseUnwrapped = function (
|
|
3375
|
-
var
|
|
3362
|
+
TextService.parseUnwrapped = function (unwrappedString) {
|
|
3363
|
+
var e_1, _a;
|
|
3364
|
+
var escaped = false;
|
|
3365
|
+
var actual = '';
|
|
3366
|
+
var paramName = '';
|
|
3367
|
+
var readingState = 'KEY';
|
|
3376
3368
|
var result = {
|
|
3377
|
-
key:
|
|
3369
|
+
key: '',
|
|
3378
3370
|
params: {},
|
|
3371
|
+
defaultValue: undefined,
|
|
3379
3372
|
};
|
|
3380
|
-
|
|
3381
|
-
var
|
|
3382
|
-
|
|
3373
|
+
try {
|
|
3374
|
+
for (var unwrappedString_1 = __values(unwrappedString), unwrappedString_1_1 = unwrappedString_1.next(); !unwrappedString_1_1.done; unwrappedString_1_1 = unwrappedString_1.next()) {
|
|
3375
|
+
var char = unwrappedString_1_1.value;
|
|
3376
|
+
if (char === '\\' && !escaped) {
|
|
3377
|
+
escaped = true;
|
|
3378
|
+
continue;
|
|
3379
|
+
}
|
|
3380
|
+
if (escaped) {
|
|
3381
|
+
escaped = false;
|
|
3382
|
+
actual += char;
|
|
3383
|
+
continue;
|
|
3384
|
+
}
|
|
3385
|
+
if (readingState === 'KEY' && char === ',') {
|
|
3386
|
+
readingState = 'DEFAULT_VALUE';
|
|
3387
|
+
result.key = actual;
|
|
3388
|
+
actual = '';
|
|
3389
|
+
continue;
|
|
3390
|
+
}
|
|
3391
|
+
if (readingState === 'KEY' && char === ':') {
|
|
3392
|
+
readingState = 'PARAM_NAME';
|
|
3393
|
+
result.key = actual;
|
|
3394
|
+
actual = '';
|
|
3395
|
+
continue;
|
|
3396
|
+
}
|
|
3397
|
+
if (readingState === 'DEFAULT_VALUE' && char === ':') {
|
|
3398
|
+
readingState = 'PARAM_NAME';
|
|
3399
|
+
result.defaultValue = actual;
|
|
3400
|
+
actual = '';
|
|
3401
|
+
continue;
|
|
3402
|
+
}
|
|
3403
|
+
if (readingState === 'PARAM_NAME' && char === ':') {
|
|
3404
|
+
readingState = 'PARAM_VALUE';
|
|
3405
|
+
paramName = actual;
|
|
3406
|
+
actual = '';
|
|
3407
|
+
continue;
|
|
3408
|
+
}
|
|
3409
|
+
if (readingState === 'PARAM_VALUE' && char === ',') {
|
|
3410
|
+
readingState = 'PARAM_NAME';
|
|
3411
|
+
result.params[paramName] = actual;
|
|
3412
|
+
actual = '';
|
|
3413
|
+
continue;
|
|
3414
|
+
}
|
|
3415
|
+
actual += char;
|
|
3416
|
+
}
|
|
3417
|
+
}
|
|
3418
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
3419
|
+
finally {
|
|
3420
|
+
try {
|
|
3421
|
+
if (unwrappedString_1_1 && !unwrappedString_1_1.done && (_a = unwrappedString_1.return)) _a.call(unwrappedString_1);
|
|
3422
|
+
}
|
|
3423
|
+
finally { if (e_1) throw e_1.error; }
|
|
3424
|
+
}
|
|
3425
|
+
if (readingState === 'KEY') {
|
|
3426
|
+
result.key = actual;
|
|
3427
|
+
}
|
|
3428
|
+
if (readingState === 'DEFAULT_VALUE') {
|
|
3429
|
+
result.defaultValue = actual;
|
|
3430
|
+
}
|
|
3431
|
+
if (readingState === 'PARAM_VALUE') {
|
|
3432
|
+
result.params[paramName] = actual;
|
|
3383
3433
|
}
|
|
3384
3434
|
return result;
|
|
3385
3435
|
};
|
|
3386
|
-
TextService.prototype.translate = function (key, params, lang, orEmpty) {
|
|
3436
|
+
TextService.prototype.translate = function (key, params, lang, orEmpty, defaultValue) {
|
|
3387
3437
|
if (lang === void 0) { lang = this.properties.currentLanguage; }
|
|
3388
3438
|
return __awaiter(this, void 0, void 0, function () {
|
|
3389
3439
|
var _a;
|
|
@@ -3391,19 +3441,19 @@
|
|
|
3391
3441
|
switch (_b.label) {
|
|
3392
3442
|
case 0:
|
|
3393
3443
|
_a = this.format;
|
|
3394
|
-
return [4 /*yield*/, this.translationService.getTranslation(key, lang, orEmpty)];
|
|
3444
|
+
return [4 /*yield*/, this.translationService.getTranslation(key, lang, orEmpty, defaultValue)];
|
|
3395
3445
|
case 1: return [2 /*return*/, _a.apply(this, [_b.sent(), params])];
|
|
3396
3446
|
}
|
|
3397
3447
|
});
|
|
3398
3448
|
});
|
|
3399
3449
|
};
|
|
3400
|
-
TextService.prototype.instant = function (key, params, lang, orEmpty) {
|
|
3450
|
+
TextService.prototype.instant = function (key, params, lang, orEmpty, defaultValue) {
|
|
3401
3451
|
if (lang === void 0) { lang = this.properties.currentLanguage; }
|
|
3402
|
-
return this.format(this.translationService.getFromCacheOrFallback(key, lang, orEmpty), params);
|
|
3452
|
+
return this.format(this.translationService.getFromCacheOrFallback(key, lang, orEmpty, defaultValue), params);
|
|
3403
3453
|
};
|
|
3404
3454
|
TextService.prototype.replace = function (text) {
|
|
3405
3455
|
return __awaiter(this, void 0, void 0, function () {
|
|
3406
|
-
var matchRegexp, keysAndParams, matched, match, start, result, _a, fullMatch,
|
|
3456
|
+
var matchRegexp, keysAndParams, matched, match, start, result, pre, _a, fullMatch, wrapped, unwrapped, index, input, translated;
|
|
3407
3457
|
return __generator(this, function (_b) {
|
|
3408
3458
|
switch (_b.label) {
|
|
3409
3459
|
case 0:
|
|
@@ -3415,35 +3465,43 @@
|
|
|
3415
3465
|
_b.label = 1;
|
|
3416
3466
|
case 1:
|
|
3417
3467
|
if (!((match = matchRegexp.exec(text)) !== null)) return [3 /*break*/, 3];
|
|
3418
|
-
|
|
3468
|
+
pre = match[1];
|
|
3469
|
+
_a = __read(match, 4), fullMatch = _a[0], wrapped = _a[2], unwrapped = _a[3];
|
|
3419
3470
|
index = match.index, input = match.input;
|
|
3420
3471
|
result += input.substr(start, index - start);
|
|
3421
3472
|
start = index + fullMatch.length;
|
|
3422
|
-
if (pre === '\\'
|
|
3423
|
-
|
|
3424
|
-
|
|
3473
|
+
if (pre === '\\') {
|
|
3474
|
+
if (!TextHelper.isCharEscaped(index, text)) {
|
|
3475
|
+
result += wrapped;
|
|
3476
|
+
return [3 /*break*/, 1];
|
|
3477
|
+
}
|
|
3478
|
+
pre = '';
|
|
3425
3479
|
}
|
|
3426
3480
|
return [4 /*yield*/, this.getTranslatedWithMetadata(unwrapped)];
|
|
3427
3481
|
case 2:
|
|
3428
3482
|
translated = _b.sent();
|
|
3429
|
-
keysAndParams.push({
|
|
3483
|
+
keysAndParams.push({
|
|
3484
|
+
key: translated.key,
|
|
3485
|
+
params: translated.params,
|
|
3486
|
+
defaultValue: translated.defaultValue,
|
|
3487
|
+
});
|
|
3430
3488
|
matched = true;
|
|
3431
3489
|
result += pre + translated.translated;
|
|
3432
3490
|
return [3 /*break*/, 1];
|
|
3433
3491
|
case 3:
|
|
3434
3492
|
result += text.substring(start);
|
|
3435
|
-
withoutEscapes = TextHelper.removeEscapes(result);
|
|
3436
3493
|
if (matched) {
|
|
3437
|
-
return [2 /*return*/, { text:
|
|
3494
|
+
return [2 /*return*/, { text: result, keys: keysAndParams }];
|
|
3438
3495
|
}
|
|
3439
3496
|
return [2 /*return*/, undefined];
|
|
3440
3497
|
}
|
|
3441
3498
|
});
|
|
3442
3499
|
});
|
|
3443
3500
|
};
|
|
3444
|
-
TextService.prototype.wrap = function (key, params) {
|
|
3501
|
+
TextService.prototype.wrap = function (key, params, defaultValue) {
|
|
3445
3502
|
var _this = this;
|
|
3446
3503
|
if (params === void 0) { params = {}; }
|
|
3504
|
+
if (defaultValue === void 0) { defaultValue = undefined; }
|
|
3447
3505
|
var paramString = Object.entries(params)
|
|
3448
3506
|
.map(function (_a) {
|
|
3449
3507
|
var _b = __read(_a, 2), name = _b[0], value = _b[1];
|
|
@@ -3451,19 +3509,20 @@
|
|
|
3451
3509
|
})
|
|
3452
3510
|
.join(',');
|
|
3453
3511
|
paramString = paramString.length ? ":" + paramString : '';
|
|
3454
|
-
|
|
3512
|
+
var defaultString = defaultValue !== undefined ? "," + this.escapeParam(defaultValue) : '';
|
|
3513
|
+
return "" + this.properties.config.inputPrefix + this.escapeParam(key) + defaultString + paramString + this.properties.config.inputSuffix;
|
|
3455
3514
|
};
|
|
3456
3515
|
TextService.prototype.getTranslatedWithMetadata = function (text) {
|
|
3457
3516
|
return __awaiter(this, void 0, void 0, function () {
|
|
3458
|
-
var _a, key, params, translated;
|
|
3517
|
+
var _a, key, params, defaultValue, translated;
|
|
3459
3518
|
return __generator(this, function (_b) {
|
|
3460
3519
|
switch (_b.label) {
|
|
3461
3520
|
case 0:
|
|
3462
|
-
_a = TextService.parseUnwrapped(text), key = _a.key, params = _a.params;
|
|
3463
|
-
return [4 /*yield*/, this.translate(key, params, undefined, false)];
|
|
3521
|
+
_a = TextService.parseUnwrapped(text), key = _a.key, params = _a.params, defaultValue = _a.defaultValue;
|
|
3522
|
+
return [4 /*yield*/, this.translate(key, params, undefined, false, defaultValue)];
|
|
3464
3523
|
case 1:
|
|
3465
3524
|
translated = _b.sent();
|
|
3466
|
-
return [2 /*return*/, { translated: translated, key: key, params: params }];
|
|
3525
|
+
return [2 /*return*/, { translated: translated, key: key, params: params, defaultValue: defaultValue }];
|
|
3467
3526
|
}
|
|
3468
3527
|
});
|
|
3469
3528
|
});
|
|
@@ -4552,34 +4611,6 @@
|
|
|
4552
4611
|
var Tolgee = /** @class */ (function () {
|
|
4553
4612
|
function Tolgee(config) {
|
|
4554
4613
|
var _this = this;
|
|
4555
|
-
this.translate = function (key, params, noWrap) {
|
|
4556
|
-
if (params === void 0) { params = {}; }
|
|
4557
|
-
if (noWrap === void 0) { noWrap = false; }
|
|
4558
|
-
return __awaiter(_this, void 0, void 0, function () {
|
|
4559
|
-
return __generator(this, function (_a) {
|
|
4560
|
-
switch (_a.label) {
|
|
4561
|
-
case 0:
|
|
4562
|
-
if (!(this.properties.config.mode === 'development' && !noWrap)) return [3 /*break*/, 3];
|
|
4563
|
-
return [4 /*yield*/, this.loadScopes()];
|
|
4564
|
-
case 1:
|
|
4565
|
-
_a.sent();
|
|
4566
|
-
return [4 /*yield*/, this.translationService.loadTranslations()];
|
|
4567
|
-
case 2:
|
|
4568
|
-
_a.sent();
|
|
4569
|
-
return [2 /*return*/, this.dependencyStore.textService.wrap(key, params)];
|
|
4570
|
-
case 3: return [2 /*return*/, this.dependencyStore.textService.translate(key, params)];
|
|
4571
|
-
}
|
|
4572
|
-
});
|
|
4573
|
-
});
|
|
4574
|
-
};
|
|
4575
|
-
this.instant = function (key, params, noWrap, orEmpty) {
|
|
4576
|
-
if (params === void 0) { params = {}; }
|
|
4577
|
-
if (noWrap === void 0) { noWrap = false; }
|
|
4578
|
-
if (_this.properties.config.mode === 'development' && !noWrap) {
|
|
4579
|
-
return _this.dependencyStore.textService.wrap(key, params);
|
|
4580
|
-
}
|
|
4581
|
-
return _this.dependencyStore.textService.instant(key, params, undefined, orEmpty);
|
|
4582
|
-
};
|
|
4583
4614
|
this.stop = function () {
|
|
4584
4615
|
_this.dependencyStore.observer.stopObserving();
|
|
4585
4616
|
_this.dependencyStore.elementRegistrar.cleanAll();
|
|
@@ -4712,6 +4743,57 @@
|
|
|
4712
4743
|
});
|
|
4713
4744
|
});
|
|
4714
4745
|
};
|
|
4746
|
+
Tolgee.prototype.translate = function (keyOrProps, params, noWrap, defaultValue) {
|
|
4747
|
+
if (params === void 0) { params = {}; }
|
|
4748
|
+
if (noWrap === void 0) { noWrap = false; }
|
|
4749
|
+
if (defaultValue === void 0) { defaultValue = undefined; }
|
|
4750
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
4751
|
+
var key, props;
|
|
4752
|
+
return __generator(this, function (_a) {
|
|
4753
|
+
switch (_a.label) {
|
|
4754
|
+
case 0:
|
|
4755
|
+
key = typeof keyOrProps === 'string' ? keyOrProps : keyOrProps.key;
|
|
4756
|
+
if (typeof keyOrProps === 'object') {
|
|
4757
|
+
props = keyOrProps;
|
|
4758
|
+
// if values are not provided in props object, get them from function
|
|
4759
|
+
// params defaults
|
|
4760
|
+
params = props.params !== undefined ? props.params : params;
|
|
4761
|
+
noWrap = props.noWrap !== undefined ? props.noWrap : noWrap;
|
|
4762
|
+
defaultValue =
|
|
4763
|
+
props.defaultValue !== undefined ? props.defaultValue : defaultValue;
|
|
4764
|
+
}
|
|
4765
|
+
if (!(this.properties.config.mode === 'development' && !noWrap)) return [3 /*break*/, 3];
|
|
4766
|
+
return [4 /*yield*/, this.loadScopes()];
|
|
4767
|
+
case 1:
|
|
4768
|
+
_a.sent();
|
|
4769
|
+
return [4 /*yield*/, this.translationService.loadTranslations()];
|
|
4770
|
+
case 2:
|
|
4771
|
+
_a.sent();
|
|
4772
|
+
return [2 /*return*/, this.dependencyStore.textService.wrap(key, params, defaultValue)];
|
|
4773
|
+
case 3: return [2 /*return*/, this.dependencyStore.textService.translate(key, params, undefined, undefined, defaultValue)];
|
|
4774
|
+
}
|
|
4775
|
+
});
|
|
4776
|
+
});
|
|
4777
|
+
};
|
|
4778
|
+
Tolgee.prototype.instant = function (keyOrProps, params, noWrap, orEmpty, defaultValue) {
|
|
4779
|
+
if (params === void 0) { params = {}; }
|
|
4780
|
+
if (noWrap === void 0) { noWrap = false; }
|
|
4781
|
+
var key = typeof keyOrProps === 'string' ? keyOrProps : keyOrProps.key;
|
|
4782
|
+
if (typeof keyOrProps === 'object') {
|
|
4783
|
+
var props = keyOrProps;
|
|
4784
|
+
// if values are not provided in props object, get them from function
|
|
4785
|
+
// params defaults
|
|
4786
|
+
params = props.params !== undefined ? props.params : params;
|
|
4787
|
+
noWrap = props.noWrap !== undefined ? props.noWrap : noWrap;
|
|
4788
|
+
defaultValue =
|
|
4789
|
+
props.defaultValue !== undefined ? props.defaultValue : defaultValue;
|
|
4790
|
+
orEmpty = props.orEmpty !== undefined ? props.orEmpty : orEmpty;
|
|
4791
|
+
}
|
|
4792
|
+
if (this.properties.config.mode === 'development' && !noWrap) {
|
|
4793
|
+
return this.dependencyStore.textService.wrap(key, params, defaultValue);
|
|
4794
|
+
}
|
|
4795
|
+
return this.dependencyStore.textService.instant(key, params, undefined, orEmpty, defaultValue);
|
|
4796
|
+
};
|
|
4715
4797
|
Tolgee.prototype.loadScopes = function () {
|
|
4716
4798
|
return __awaiter(this, void 0, void 0, function () {
|
|
4717
4799
|
var _a;
|