@adminforth/i18n 1.1.3-next.5 → 1.2.0-next.2
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/build.log +1 -1
- package/dist/index.js +70 -65
- package/index.ts +71 -64
- package/package.json +1 -1
- package/types.ts +6 -0
package/build.log
CHANGED
package/dist/index.js
CHANGED
|
@@ -128,6 +128,7 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
128
128
|
throw new Error(`Invalid language code ${lang}, please define valid ISO 639-1 language code (2 lowercase letters)`);
|
|
129
129
|
}
|
|
130
130
|
});
|
|
131
|
+
this.externalAppOnly = this.options.externalAppOnly === true;
|
|
131
132
|
// find primary key field
|
|
132
133
|
this.primaryKeyFieldName = (_a = resourceConfig.columns.find(c => c.primaryKey)) === null || _a === void 0 ? void 0 : _a.name;
|
|
133
134
|
if (!this.primaryKeyFieldName) {
|
|
@@ -236,18 +237,20 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
236
237
|
}))
|
|
237
238
|
};
|
|
238
239
|
// add underLogin component
|
|
239
|
-
(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
240
|
+
if (!this.externalAppOnly) {
|
|
241
|
+
(adminforth.config.customization.loginPageInjections.underInputs).push({
|
|
242
|
+
file: this.componentPath('LanguageUnderLogin.vue'),
|
|
243
|
+
meta: compMeta
|
|
244
|
+
});
|
|
245
|
+
(adminforth.config.customization.globalInjections.userMenu).push({
|
|
246
|
+
file: this.componentPath('LanguageInUserMenu.vue'),
|
|
247
|
+
meta: compMeta
|
|
248
|
+
});
|
|
249
|
+
adminforth.config.customization.globalInjections.everyPageBottom.push({
|
|
250
|
+
file: this.componentPath('LanguageEveryPageLoader.vue'),
|
|
251
|
+
meta: compMeta
|
|
252
|
+
});
|
|
253
|
+
}
|
|
251
254
|
// disable create allowedActions for translations
|
|
252
255
|
resourceConfig.options.allowedActions.create = false;
|
|
253
256
|
// add hook to validate user did not screw up with template params
|
|
@@ -618,65 +621,67 @@ ${JSON.stringify(strings.reduce((acc, s) => {
|
|
|
618
621
|
throw new Error(`Field ${this.options.categoryFieldName} should be of type string in resource ${resourceConfig.resourceId}, but it is ${categoryColumn.type}`);
|
|
619
622
|
}
|
|
620
623
|
// in this plugin we will use plugin to fill the database with missing language messages
|
|
621
|
-
this.
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
if (category === 'frontend') {
|
|
627
|
-
throw new Error(`Category 'frontend' is reserved for frontend messages, use any other category for backend messages`);
|
|
628
|
-
}
|
|
629
|
-
// console.log('🪲tr', msg, category, lang);
|
|
630
|
-
// if lang is not supported , throw
|
|
631
|
-
if (!this.options.supportedLanguages.includes(lang)) {
|
|
632
|
-
lang = 'en'; // for now simply fallback to english
|
|
633
|
-
// throwing like line below might be too strict, e.g. for custom apis made with fetch which don't pass accept-language
|
|
634
|
-
// throw new Error(`Language ${lang} is not entered to be supported by requested by browser in request headers accept-language`);
|
|
635
|
-
}
|
|
636
|
-
let result;
|
|
637
|
-
// try to get translation from cache
|
|
638
|
-
const cacheKey = `${resourceConfig.resourceId}:${category}:${lang}:${msg}`;
|
|
639
|
-
const cached = yield this.cache.get(cacheKey);
|
|
640
|
-
if (cached) {
|
|
641
|
-
result = cached;
|
|
642
|
-
}
|
|
643
|
-
if (!result) {
|
|
644
|
-
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
645
|
-
const translation = yield resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
646
|
-
if (!translation) {
|
|
647
|
-
yield resource.create({
|
|
648
|
-
[this.enFieldName]: msg,
|
|
649
|
-
[this.options.categoryFieldName]: category,
|
|
650
|
-
});
|
|
651
|
-
this.updateUntranslatedMenuBadge();
|
|
624
|
+
if (!this.externalAppOnly) {
|
|
625
|
+
this.tryProcessAndWatch(adminforth);
|
|
626
|
+
adminforth.tr = (msg, category, lang, params, pluralizationNumber) => __awaiter(this, void 0, void 0, function* () {
|
|
627
|
+
if (!msg) {
|
|
628
|
+
return msg;
|
|
652
629
|
}
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
if (lang === 'en') {
|
|
656
|
-
// set to cache to return faster next time
|
|
657
|
-
result = msg;
|
|
630
|
+
if (category === 'frontend') {
|
|
631
|
+
throw new Error(`Category 'frontend' is reserved for frontend messages, use any other category for backend messages`);
|
|
658
632
|
}
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
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
|
|
663
661
|
result = msg;
|
|
664
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);
|
|
665
672
|
}
|
|
666
|
-
//
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
// if msg has '|' in it, then we need to aplly pluralization
|
|
670
|
-
if (msg.includes('|')) {
|
|
671
|
-
result = this.applyPluralization(result, pluralizationNumber, lang);
|
|
672
|
-
}
|
|
673
|
-
if (params) {
|
|
674
|
-
for (const [key, value] of Object.entries(params)) {
|
|
675
|
-
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);
|
|
676
676
|
}
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
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
|
+
}
|
|
680
685
|
}
|
|
681
686
|
applyPluralization(str, number, lang) {
|
|
682
687
|
const choices = str.split('|');
|
package/index.ts
CHANGED
|
@@ -94,6 +94,8 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
94
94
|
|
|
95
95
|
adminforth: IAdminForth;
|
|
96
96
|
|
|
97
|
+
externalAppOnly: boolean;
|
|
98
|
+
|
|
97
99
|
// sorted by name list of all supported languages, without en e.g. 'al|ro|uk'
|
|
98
100
|
fullCompleatedFieldValue: string;
|
|
99
101
|
|
|
@@ -140,7 +142,8 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
140
142
|
throw new Error(`Invalid language code ${lang}, please define valid ISO 639-1 language code (2 lowercase letters)`);
|
|
141
143
|
}
|
|
142
144
|
});
|
|
143
|
-
|
|
145
|
+
|
|
146
|
+
this.externalAppOnly = this.options.externalAppOnly === true;
|
|
144
147
|
|
|
145
148
|
// find primary key field
|
|
146
149
|
this.primaryKeyFieldName = resourceConfig.columns.find(c => c.primaryKey)?.name;
|
|
@@ -265,20 +268,22 @@ export default class I18nPlugin extends AdminForthPlugin {
|
|
|
265
268
|
))
|
|
266
269
|
};
|
|
267
270
|
// add underLogin component
|
|
268
|
-
(
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
271
|
+
if (!this.externalAppOnly) {
|
|
272
|
+
(adminforth.config.customization.loginPageInjections.underInputs).push({
|
|
273
|
+
file: this.componentPath('LanguageUnderLogin.vue'),
|
|
274
|
+
meta: compMeta
|
|
275
|
+
});
|
|
272
276
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
+
(adminforth.config.customization.globalInjections.userMenu).push({
|
|
278
|
+
file: this.componentPath('LanguageInUserMenu.vue'),
|
|
279
|
+
meta: compMeta
|
|
280
|
+
});
|
|
277
281
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
+
adminforth.config.customization.globalInjections.everyPageBottom.push({
|
|
283
|
+
file: this.componentPath('LanguageEveryPageLoader.vue'),
|
|
284
|
+
meta: compMeta
|
|
285
|
+
});
|
|
286
|
+
}
|
|
282
287
|
|
|
283
288
|
// disable create allowedActions for translations
|
|
284
289
|
resourceConfig.options.allowedActions.create = false;
|
|
@@ -735,70 +740,72 @@ JSON.stringify(strings.reduce((acc: object, s: { en_string: string }): object =>
|
|
|
735
740
|
}
|
|
736
741
|
|
|
737
742
|
// in this plugin we will use plugin to fill the database with missing language messages
|
|
738
|
-
this.
|
|
743
|
+
if (!this.externalAppOnly) {
|
|
744
|
+
this.tryProcessAndWatch(adminforth);
|
|
739
745
|
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
746
|
+
adminforth.tr = async (msg: string | null | undefined, category: string, lang: string, params, pluralizationNumber: number): Promise<string> => {
|
|
747
|
+
if (!msg) {
|
|
748
|
+
return msg;
|
|
749
|
+
}
|
|
744
750
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
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);
|
|
749
755
|
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
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
|
|
753
759
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
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
|
+
}
|
|
757
763
|
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
}
|
|
765
|
-
if (!result) {
|
|
766
|
-
const resource = adminforth.resource(resourceConfig.resourceId);
|
|
767
|
-
const translation = await resource.get([Filters.EQ(this.enFieldName, msg), Filters.EQ(this.options.categoryFieldName, category)]);
|
|
768
|
-
if (!translation) {
|
|
769
|
-
await resource.create({
|
|
770
|
-
[this.enFieldName]: msg,
|
|
771
|
-
[this.options.categoryFieldName]: category,
|
|
772
|
-
});
|
|
773
|
-
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;
|
|
774
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
|
+
}
|
|
775
781
|
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
result = msg;
|
|
781
|
-
} else {
|
|
782
|
-
result = translation?.[this.trFieldNames[lang]];
|
|
783
|
-
if (!result) {
|
|
784
|
-
// 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
|
|
785
786
|
result = msg;
|
|
787
|
+
} else {
|
|
788
|
+
result = translation?.[this.trFieldNames[lang]];
|
|
789
|
+
if (!result) {
|
|
790
|
+
// return english
|
|
791
|
+
result = msg;
|
|
792
|
+
}
|
|
786
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);
|
|
787
800
|
}
|
|
788
|
-
// cache so even if key does not exist, we will not hit database
|
|
789
|
-
await this.cache.set(cacheKey, result);
|
|
790
|
-
}
|
|
791
|
-
// if msg has '|' in it, then we need to aplly pluralization
|
|
792
|
-
if (msg.includes('|')) {
|
|
793
|
-
result = this.applyPluralization(result, pluralizationNumber, lang);
|
|
794
|
-
}
|
|
795
801
|
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
802
|
+
if (params) {
|
|
803
|
+
for (const [key, value] of Object.entries(params)) {
|
|
804
|
+
result = result.replace(`{${key}}`, value);
|
|
805
|
+
}
|
|
799
806
|
}
|
|
807
|
+
return result;
|
|
800
808
|
}
|
|
801
|
-
return result;
|
|
802
809
|
}
|
|
803
810
|
}
|
|
804
811
|
|
package/package.json
CHANGED
package/types.ts
CHANGED
|
@@ -36,4 +36,10 @@ export interface PluginOptions {
|
|
|
36
36
|
* semiautomatically (creates a bulk action for generating translations)
|
|
37
37
|
*/
|
|
38
38
|
completeAdapter?: CompletionAdapter
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Optionally translation plugin available to translate only external applications (like SEO-facing or user-facing services),
|
|
42
|
+
* not AdminForth applications
|
|
43
|
+
*/
|
|
44
|
+
externalAppOnly?: boolean;
|
|
39
45
|
}
|