Skip to content

Commit 8cdf611

Browse files
committed
Merge branch 'main' of github.com:devforth/adminforth
2 parents 5d0a8b4 + fdef3b1 commit 8cdf611

File tree

9 files changed

+58
-23
lines changed

9 files changed

+58
-23
lines changed

adminforth/documentation/docs/tutorial/05-Plugins/10-i18n.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ And on the backend side you can use tr function to translate the string:
337337
admin.express.translatable(
338338
async (req, res) => {
339339
res.json({
340-
text: await tr('Welcome, {name}', 'customApis', { name: req.adminUser.username }),
340+
text: await req.tr('Welcome, {name}', 'customApis', { name: req.adminUser.username }),
341341
});
342342
}
343343
)

adminforth/modules/restApi.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
459459
})
460460
);
461461

462-
console.log('🗣️translated', translated);
463-
464462

465463
const toReturn = {
466464
...resource,

adminforth/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adminforth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "adminforth",
3-
"version": "1.5.8-next.11",
3+
"version": "1.5.8-next.12",
44
"description": "OpenSource Vue3 powered forth-generation admin panel",
55
"main": "dist/index.js",
66
"module": "dist/index.js",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [v1.0.10]
9+
10+
## Fixed
11+
12+
- fix automatic translations for duplicate strings
13+
- improve slavik pluralization generations by splitting the requests

adminforth/plugins/i18n/index.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ export default class I18N extends AdminForthPlugin {
347347
});
348348
}
349349

350-
async bulkTranslate({ selectedIds }: { selectedIds: string[] }) {
350+
// returns translated count
351+
async bulkTranslate({ selectedIds }: { selectedIds: string[] }): Promise<number> {
351352

352353
const needToTranslateByLang : Partial<
353354
Record<
@@ -382,26 +383,33 @@ export default class I18N extends AdminForthPlugin {
382383
const maxKeysInOneReq = 10;
383384

384385
const updateStrings: Record<string, {
385-
updates: any, category: string, strId: string
386+
updates: any,
387+
category: string,
388+
strId: string,
389+
translatedStr: string
386390
}> = {};
387391

388-
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[]) => {
389-
392+
const translateToLang = async (langIsoCode: LanguageCode, strings: { en_string: string, category: string }[], plurals=false): Promise<number> => {
393+
if (strings.length === 0) {
394+
return 0;
395+
}
390396

391397
if (strings.length > maxKeysInOneReq) {
398+
let totalTranslated = 0;
392399
for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
393400
const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
394-
await translateToLang(langIsoCode, slicedStrings);
401+
console.log('🪲🔪slicedStrings ', slicedStrings);
402+
totalTranslated += await translateToLang(langIsoCode, slicedStrings, plurals);
395403
}
396-
return;
404+
return totalTranslated;
397405
}
398406
const lang = langIsoCode;
399407

400-
const isSlavikPlural = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang);
408+
const requestSlavicPlurals = Object.keys(SLAVIC_PLURAL_EXAMPLES).includes(lang) && plurals;
401409

402410
const prompt = `
403411
I need to translate strings in JSON to ${lang} language from English for my web app.
404-
${isSlavikPlural ? `If string contains '|' it means it is plural form, you should provide 4 translations (zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
412+
${requestSlavicPlurals ? `You should provide 4 translations (in format zero | singular | 2-4 | 5+) e.g. ${SLAVIC_PLURAL_EXAMPLES[lang]}` : ''}
405413
Keep keys, as is, write translation into values! Here are the strings:
406414
407415
\`\`\`json
@@ -442,29 +450,42 @@ ${
442450
return;
443451
}
444452
res = JSON.parse(res);
445-
for (const [enStr, translatedStr] of Object.entries(res)) {
453+
454+
455+
for (const [enStr, translatedStr] of Object.entries(res) as [string, string][]) {
446456
const translationsTargeted = translations.filter(t => t[this.enFieldName] === enStr);
447457
// might be several with same en_string
448458
for (const translation of translationsTargeted) {
449-
translation[this.trFieldNames[lang]] = translatedStr;
459+
//translation[this.trFieldNames[lang]] = translatedStr;
450460
// process.env.HEAVY_DEBUG && console.log(`🪲translated to ${lang} ${translation.en_string}, ${translatedStr}`)
451-
if (!updateStrings[enStr]) {
452-
updateStrings[enStr] = {
461+
if (!updateStrings[translation[this.primaryKeyFieldName]]) {
462+
463+
updateStrings[translation[this.primaryKeyFieldName]] = {
453464
updates: {},
465+
translatedStr,
454466
category: translation[this.options.categoryFieldName],
455467
strId: translation[this.primaryKeyFieldName],
456468
};
457469
}
458-
updateStrings[enStr].updates[this.trFieldNames[lang]] = translatedStr;
470+
updateStrings[
471+
translation[this.primaryKeyFieldName]
472+
].updates[this.trFieldNames[lang]] = translatedStr;
459473
}
460474
}
461475

476+
return res.length;
462477
}
463478

464479
const langsInvolved = new Set(Object.keys(needToTranslateByLang));
465480

481+
let totalTranslated = 0;
466482
await Promise.all(Object.entries(needToTranslateByLang).map(async ([lang, strings]: [LanguageCode, { en_string: string, category: string }[]]) => {
467-
await translateToLang(lang, strings);
483+
// first translate without plurals
484+
const stringsWithoutPlurals = strings.filter(s => !s.en_string.includes('|'));
485+
totalTranslated += await translateToLang(lang, stringsWithoutPlurals, false);
486+
487+
const stringsWithPlurals = strings.filter(s => s.en_string.includes('|'));
488+
totalTranslated += await translateToLang(lang, stringsWithPlurals, true);
468489
}));
469490

470491
await Promise.all(
@@ -488,6 +509,8 @@ ${
488509
}
489510
}
490511

512+
return totalTranslated;
513+
491514
}
492515

493516
async processExtractedMessages(adminforth: IAdminForth, filePath: string) {

adminforth/plugins/i18n/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adminforth/plugins/i18n/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adminforth/i18n",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"type": "module",

dev-demo/resources/translation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default {
1717
plugins: [
1818
new I18nPlugin({
1919
supportedLanguages: ['en', 'uk', 'ja', 'fr'],
20+
// supportedLanguages: ['en', 'uk'],
2021

2122
// names of the fields in the resource which will store translations
2223
translationFieldNames: {

0 commit comments

Comments
 (0)