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