Skip to content

Commit e5aeb2c

Browse files
committed
i18n: counting fixes
1 parent da49c64 commit e5aeb2c

File tree

4 files changed

+82
-26
lines changed

4 files changed

+82
-26
lines changed

adminforth/plugins/i18n/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,17 +406,18 @@ export default class I18N extends AdminForthPlugin {
406406
translatedStr: string
407407
}> = {};
408408

409-
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<number> => {
409+
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<string[]> => {
410410
if (strings.length === 0) {
411-
return 0;
411+
return [];
412412
}
413413

414414
if (strings.length > maxKeysInOneReq) {
415-
let totalTranslated = 0;
415+
let totalTranslated = [];
416416
for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
417417
const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
418418
process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
419-
totalTranslated += await translateToLang(langIsoCode, slicedStrings, plurals);
419+
const madeKeys = await translateToLang(langIsoCode, slicedStrings, plurals);
420+
totalTranslated = totalTranslated.concat(madeKeys);
420421
}
421422
return totalTranslated;
422423
}
@@ -490,19 +491,20 @@ ${
490491
}
491492
}
492493

493-
return Object.keys(updateStrings).length;
494+
return Object.keys(updateStrings);
494495
}
495496

496497
const langsInvolved = new Set(Object.keys(needToTranslateByLang));
497498

498-
let totalTranslated = 0;
499+
let totalTranslated = [];
499500
await Promise.all(Object.entries(needToTranslateByLang).map(async ([lang, strings]: [LanguageCode, { en_string: string, category: string }[]]) => {
500501
// first translate without plurals
501502
const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
502-
totalTranslated += await translateToLang(lang, stringsWithoutPlurals, false);
503+
const noPluralKeys = await translateToLang(lang, stringsWithoutPlurals, false);
503504

504505
const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
505-
totalTranslated += await translateToLang(lang, stringsWithPlurals, true);
506+
const pluralKeys = await translateToLang(lang, stringsWithPlurals, true);
507+
totalTranslated = totalTranslated.concat(noPluralKeys, pluralKeys);
506508
}));
507509

508510
await Promise.all(
@@ -526,7 +528,7 @@ ${
526528
}
527529
}
528530

529-
return totalTranslated;
531+
return new Set(totalTranslated).size;
530532

531533
}
532534

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- AlterTable
2+
ALTER TABLE "translations" ADD COLUMN "ar_string" TEXT;
3+
ALTER TABLE "translations" ADD COLUMN "az_string" TEXT;
4+
ALTER TABLE "translations" ADD COLUMN "bn_string" TEXT;
5+
ALTER TABLE "translations" ADD COLUMN "de_string" TEXT;
6+
ALTER TABLE "translations" ADD COLUMN "hi_string" TEXT;
7+
ALTER TABLE "translations" ADD COLUMN "it_string" TEXT;
8+
ALTER TABLE "translations" ADD COLUMN "ko_string" TEXT;
9+
ALTER TABLE "translations" ADD COLUMN "pa_string" TEXT;
10+
ALTER TABLE "translations" ADD COLUMN "pt_string" TEXT;
11+
ALTER TABLE "translations" ADD COLUMN "ru_string" TEXT;
12+
ALTER TABLE "translations" ADD COLUMN "tr_string" TEXT;
13+
ALTER TABLE "translations" ADD COLUMN "ur_string" TEXT;
14+
ALTER TABLE "translations" ADD COLUMN "vi_string" TEXT;
15+
ALTER TABLE "translations" ADD COLUMN "zh_string" TEXT;

dev-demo/resources/translation.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,32 @@ export default {
1616
recordLabel: (r: any) => `👤 ${r.en_string}`,
1717
plugins: [
1818
new I18nPlugin({
19-
supportedLanguages: ['en', 'uk', 'ja', 'fr'],
19+
// supportedLanguages: ['en', 'uk', 'ja', 'fr'],
2020
// supportedLanguages: ['en', 'uk'],
2121

22+
supportedLanguages: ['en', 'uk', 'ar', 'ru', 'zh', 'es', 'hi', 'bn', 'pt', 'ja', 'de', 'tr', 'fr', 'pa', 'ko', 'ur', 'az', 'vi', 'it'],
23+
2224
// names of the fields in the resource which will store translations
2325
translationFieldNames: {
2426
en: 'en_string',
2527
uk: 'uk_string',
28+
ar: 'ar_string',
29+
ru: 'ru_string',
30+
zh: 'zh_string',
31+
es: 'es_string',
32+
hi: 'hi_string',
33+
bn: 'bn_string',
34+
pt: 'pt_string',
2635
ja: 'ja_string',
36+
de: 'de_string',
37+
tr: 'tr_string',
2738
fr: 'fr_string',
39+
pa: 'pa_string',
40+
ko: 'ko_string',
41+
ur: 'ur_string',
42+
az: 'az_string',
43+
vi: 'vi_string',
44+
it: 'it_string',
2845
},
2946

3047
// name of the field which will store the category of the string
@@ -70,18 +87,24 @@ export default {
7087
name: "created_at",
7188
fillOnCreate: ({ initialRecord, adminUser }: any) => new Date().toISOString(),
7289
},
73-
{
74-
name: "uk_string",
75-
type: AdminForthDataTypes.STRING,
76-
},
77-
{
78-
name: "ja_string",
79-
type: AdminForthDataTypes.STRING,
80-
},
81-
{
82-
name: "fr_string",
83-
type: AdminForthDataTypes.STRING,
84-
},
90+
{ name: "uk_string", type: AdminForthDataTypes.STRING, label: "Ukrainian" },
91+
{ name: "ar_string", type: AdminForthDataTypes.STRING, label: "Arabic" },
92+
{ name: "ru_string", type: AdminForthDataTypes.STRING, label: "Russian" },
93+
{ name: "zh_string", type: AdminForthDataTypes.STRING, label: "Chinese" },
94+
{ name: "es_string", type: AdminForthDataTypes.STRING, label: "Spanish" },
95+
{ name: "hi_string", type: AdminForthDataTypes.STRING, label: "Hindi" },
96+
{ name: "bn_string", type: AdminForthDataTypes.STRING, label: "Bengali" },
97+
{ name: "pt_string", type: AdminForthDataTypes.STRING, label: "Portuguese" },
98+
{ name: "ja_string", type: AdminForthDataTypes.STRING, label: "Japanese" },
99+
{ name: "de_string", type: AdminForthDataTypes.STRING, label: "German" },
100+
{ name: "tr_string", type: AdminForthDataTypes.STRING, label: "Turkish" },
101+
{ name: "fr_string", type: AdminForthDataTypes.STRING, label: "French" },
102+
{ name: "pa_string", type: AdminForthDataTypes.STRING, label: "Punjabi" },
103+
{ name: "ko_string", type: AdminForthDataTypes.STRING, label: "Korean" },
104+
{ name: "ur_string", type: AdminForthDataTypes.STRING, label: "Urdu" },
105+
{ name: "az_string", type: AdminForthDataTypes.STRING, label: "Azerbaijani" },
106+
{ name: "vi_string", type: AdminForthDataTypes.STRING, label: "Vietnamese" },
107+
{ name: "it_string", type: AdminForthDataTypes.STRING, label: "Italian" },
85108
{
86109
name: "completedLangs",
87110
},

dev-demo/schema.prisma

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,26 @@ model translations {
5757
id String @id
5858
en_string String
5959
created_at DateTime
60-
uk_string String?
61-
ja_string String?
62-
fr_string String?
63-
es_string String?
60+
61+
uk_string String? // translation for Ukrainian language
62+
ar_string String? // translation for Arabic language
63+
ru_string String? // translation for Russian language
64+
zh_string String? // translation for Chinese language
65+
es_string String? // translation for Spanish language
66+
hi_string String? // translation for Hindi language
67+
bn_string String? // translation for Bengali language
68+
pt_string String? // translation for Portuguese language
69+
ja_string String? // translation for Japanese language
70+
de_string String? // translation for German language
71+
tr_string String? // translation for Turkish language
72+
fr_string String? // translation for French language
73+
pa_string String? // translation for Punjabi language
74+
ko_string String? // translation for Korean language
75+
ur_string String? // translation for Urdu language
76+
az_string String? // translation for Azerbaijani language
77+
vi_string String? // translation for Vietnamese language
78+
it_string String? // translation for Italian language
79+
6480
category String
6581
source String?
6682
completedLangs String?

0 commit comments

Comments
 (0)