@adminforth/i18n 1.2.0-next.1 → 1.2.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.
- package/dist/index.js +53 -53
- package/index.ts +51 -51
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -623,65 +623,65 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
623
623
|
// in this plugin we will use plugin to fill the database with missing language messages
|
|
624
624
|
if (!this.externalAppOnly) {
|
|
625
625
|
this.tryProcessAndWatch(adminforth);
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
return msg;
|
|
630
|
-
}
|
|
631
|
-
if (category === 'frontend') {
|
|
632
|
-
throw new Error(`Category 'frontend' is reserved for frontend messages, use any other category for backend messages`);
|
|
633
|
-
}
|
|
634
|
-
// console.log('🪲tr', msg, category, lang);
|
|
635
|
-
// if lang is not supported , throw
|
|
636
|
-
if (!this.options.supportedLanguages.includes(lang)) {
|
|
637
|
-
lang = 'en'; // for now simply fallback to english
|
|
638
|
-
// throwing like line below might be too strict, e.g. for custom apis made with fetch which don't pass accept-language
|
|
639
|
-
// throw new Error(`Language ${lang} is not entered to be supported by requested by browser in request headers accept-language`);
|
|
640
|
-
}
|
|
641
|
-
let result;
|
|
642
|
-
// try to get translation from cache
|
|
643
|
-
const cacheKey = `${resourceConfig.resourceId}:${category}:${lang}:${msg}`;
|
|
644
|
-
const cached = yield this.cache.get(cacheKey);
|
|
645
|
-
if (cached) {
|
|
646
|
-
result = cached;
|
|
647
|
-
}
|
|
648
|
-
if (!result) {
|
|
649
|
-
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
650
|
-
const translation = yield resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
651
|
-
if (!translation) {
|
|
652
|
-
yield resource.create({
|
|
653
|
-
[this.enFieldName]: msg,
|
|
654
|
-
[this.options.categoryFieldName]: category,
|
|
655
|
-
});
|
|
656
|
-
this.updateUntranslatedMenuBadge();
|
|
626
|
+
adminforth.tr = (msg, category, lang, params, pluralizationNumber) => __awaiter(this, void 0, void 0, function* () {
|
|
627
|
+
if (!msg) {
|
|
628
|
+
return msg;
|
|
657
629
|
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
if (lang === 'en') {
|
|
661
|
-
// set to cache to return faster next time
|
|
662
|
-
result = msg;
|
|
630
|
+
if (category === 'frontend') {
|
|
631
|
+
throw new Error(`Category 'frontend' is reserved for frontend messages, use any other category for backend messages`);
|
|
663
632
|
}
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
633
|
+
// console.log('🪲tr', msg, category, lang);
|
|
634
|
+
// if lang is not supported , throw
|
|
635
|
+
if (!this.options.supportedLanguages.includes(lang)) {
|
|
636
|
+
lang = 'en'; // for now simply fallback to english
|
|
637
|
+
// throwing like line below might be too strict, e.g. for custom apis made with fetch which don't pass accept-language
|
|
638
|
+
// throw new Error(`Language ${lang} is not entered to be supported by requested by browser in request headers accept-language`);
|
|
639
|
+
}
|
|
640
|
+
let result;
|
|
641
|
+
// try to get translation from cache
|
|
642
|
+
const cacheKey = `${resourceConfig.resourceId}:${category}:${lang}:${msg}`;
|
|
643
|
+
const cached = yield this.cache.get(cacheKey);
|
|
644
|
+
if (cached) {
|
|
645
|
+
result = cached;
|
|
646
|
+
}
|
|
647
|
+
if (!result) {
|
|
648
|
+
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
649
|
+
const translation = yield resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
650
|
+
if (!translation) {
|
|
651
|
+
yield resource.create({
|
|
652
|
+
[this.enFieldName]: msg,
|
|
653
|
+
[this.options.categoryFieldName]: category,
|
|
654
|
+
});
|
|
655
|
+
this.updateUntranslatedMenuBadge();
|
|
656
|
+
}
|
|
657
|
+
// do this check here, to faster register missing translations
|
|
658
|
+
// also not cache it - no sense to cache english strings
|
|
659
|
+
if (lang === 'en') {
|
|
660
|
+
// set to cache to return faster next time
|
|
668
661
|
result = msg;
|
|
669
662
|
}
|
|
663
|
+
else {
|
|
664
|
+
result = translation === null || translation === void 0 ? void 0 : translation[this.trFieldNames[lang]];
|
|
665
|
+
if (!result) {
|
|
666
|
+
// return english
|
|
667
|
+
result = msg;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
// cache so even if key does not exist, we will not hit database
|
|
671
|
+
yield this.cache.set(cacheKey, result);
|
|
670
672
|
}
|
|
671
|
-
//
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
// if msg has '|' in it, then we need to aplly pluralization
|
|
675
|
-
if (msg.includes('|')) {
|
|
676
|
-
result = this.applyPluralization(result, pluralizationNumber, lang);
|
|
677
|
-
}
|
|
678
|
-
if (params) {
|
|
679
|
-
for (const [key, value] of Object.entries(params)) {
|
|
680
|
-
result = result.replace(`{${key}}`, value);
|
|
673
|
+
// if msg has '|' in it, then we need to aplly pluralization
|
|
674
|
+
if (msg.includes('|')) {
|
|
675
|
+
result = this.applyPluralization(result, pluralizationNumber, lang);
|
|
681
676
|
}
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
677
|
+
if (params) {
|
|
678
|
+
for (const [key, value] of Object.entries(params)) {
|
|
679
|
+
result = result.replace(`{${key}}`, value);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
return result;
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
685
|
}
|
|
686
686
|
applyPluralization(str, number, lang) {
|
|
687
687
|
const choices = str.split('|');
|
package/index.ts
CHANGED
|
@@ -742,70 +742,70 @@ JSON.stringify(strings.reduce((acc: object, s: { en_string: string }): object =>
|
|
|
742
742
|
// in this plugin we will use plugin to fill the database with missing language messages
|
|
743
743
|
if (!this.externalAppOnly) {
|
|
744
744
|
this.tryProcessAndWatch(adminforth);
|
|
745
|
-
}
|
|
746
745
|
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
746
|
+
adminforth.tr = async (msg: string | null | undefined, category: string, lang: string, params, pluralizationNumber: number): Promise<string> => {
|
|
747
|
+
if (!msg) {
|
|
748
|
+
return msg;
|
|
749
|
+
}
|
|
751
750
|
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
751
|
+
if (category === 'frontend') {
|
|
752
|
+
throw new Error(`Category 'frontend' is reserved for frontend messages, use any other category for backend messages`);
|
|
753
|
+
}
|
|
754
|
+
// console.log('🪲tr', msg, category, lang);
|
|
756
755
|
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
756
|
+
// if lang is not supported , throw
|
|
757
|
+
if (!this.options.supportedLanguages.includes(lang as LanguageCode)) {
|
|
758
|
+
lang = 'en'; // for now simply fallback to english
|
|
760
759
|
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
760
|
+
// throwing like line below might be too strict, e.g. for custom apis made with fetch which don't pass accept-language
|
|
761
|
+
// throw new Error(`Language ${lang} is not entered to be supported by requested by browser in request headers accept-language`);
|
|
762
|
+
}
|
|
764
763
|
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
}
|
|
772
|
-
if (!result) {
|
|
773
|
-
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
774
|
-
const translation = await resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
775
|
-
if (!translation) {
|
|
776
|
-
await resource.create({
|
|
777
|
-
[this.enFieldName]: msg,
|
|
778
|
-
[this.options.categoryFieldName]: category,
|
|
779
|
-
});
|
|
780
|
-
this.updateUntranslatedMenuBadge();
|
|
764
|
+
let result;
|
|
765
|
+
// try to get translation from cache
|
|
766
|
+
const cacheKey = `${resourceConfig.resourceId}:${category}:${lang}:${msg}`;
|
|
767
|
+
const cached = await this.cache.get(cacheKey);
|
|
768
|
+
if (cached) {
|
|
769
|
+
result = cached;
|
|
781
770
|
}
|
|
771
|
+
if (!result) {
|
|
772
|
+
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
773
|
+
const translation = await resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
774
|
+
if (!translation) {
|
|
775
|
+
await resource.create({
|
|
776
|
+
[this.enFieldName]: msg,
|
|
777
|
+
[this.options.categoryFieldName]: category,
|
|
778
|
+
});
|
|
779
|
+
this.updateUntranslatedMenuBadge();
|
|
780
|
+
}
|
|
782
781
|
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
result = msg;
|
|
788
|
-
} else {
|
|
789
|
-
result = translation?.[this.trFieldNames[lang]];
|
|
790
|
-
if (!result) {
|
|
791
|
-
// return english
|
|
782
|
+
// do this check here, to faster register missing translations
|
|
783
|
+
// also not cache it - no sense to cache english strings
|
|
784
|
+
if (lang === 'en') {
|
|
785
|
+
// set to cache to return faster next time
|
|
792
786
|
result = msg;
|
|
787
|
+
} else {
|
|
788
|
+
result = translation?.[this.trFieldNames[lang]];
|
|
789
|
+
if (!result) {
|
|
790
|
+
// return english
|
|
791
|
+
result = msg;
|
|
792
|
+
}
|
|
793
793
|
}
|
|
794
|
+
// cache so even if key does not exist, we will not hit database
|
|
795
|
+
await this.cache.set(cacheKey, result);
|
|
796
|
+
}
|
|
797
|
+
// if msg has '|' in it, then we need to aplly pluralization
|
|
798
|
+
if (msg.includes('|')) {
|
|
799
|
+
result = this.applyPluralization(result, pluralizationNumber, lang);
|
|
794
800
|
}
|
|
795
|
-
// cache so even if key does not exist, we will not hit database
|
|
796
|
-
await this.cache.set(cacheKey, result);
|
|
797
|
-
}
|
|
798
|
-
// if msg has '|' in it, then we need to aplly pluralization
|
|
799
|
-
if (msg.includes('|')) {
|
|
800
|
-
result = this.applyPluralization(result, pluralizationNumber, lang);
|
|
801
|
-
}
|
|
802
801
|
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
802
|
+
if (params) {
|
|
803
|
+
for (const [key, value] of Object.entries(params)) {
|
|
804
|
+
result = result.replace(`{${key}}`, value);
|
|
805
|
+
}
|
|
806
806
|
}
|
|
807
|
+
return result;
|
|
807
808
|
}
|
|
808
|
-
return result;
|
|
809
809
|
}
|
|
810
810
|
}
|
|
811
811
|
|