Skip to content

Commit da49c64

Browse files
committed
i18n plugin: return actual translated count, more logging
1 parent 32c6c7f commit da49c64

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export default {
109109
{
110110
name: "en_string",
111111
type: AdminForthDataTypes.STRING,
112-
title: 'English',
112+
label: 'English',
113113
},
114114
{
115115
name: "created_at",
@@ -118,17 +118,17 @@ export default {
118118
{
119119
name: "uk_string",
120120
type: AdminForthDataTypes.STRING,
121-
title: 'Ukrainian',
121+
label: 'Ukrainian',
122122
},
123123
{
124124
name: "ja_string",
125125
type: AdminForthDataTypes.STRING,
126-
title: 'Japanese',
126+
label: 'Japanese',
127127
},
128128
{
129129
name: "fr_string",
130130
type: AdminForthDataTypes.STRING,
131-
title: 'French',
131+
label: 'French',
132132
},
133133
{
134134
name: "completedLangs",

adminforth/plugins/i18n/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,22 @@ export default class I18N extends AdminForthPlugin {
318318
confirm: 'Are you sure you want to translate selected items?',
319319
state: 'selected',
320320
action: async ({ selectedIds, tr }) => {
321+
let translatedCount = 0;
321322
try {
322-
await this.bulkTranslate({ selectedIds });
323+
translatedCount = await this.bulkTranslate({ selectedIds });
323324
} catch (e) {
324325
if (e instanceof AiTranslateError) {
326+
process.env.HEAVY_DEBUG && console.error('🪲⛔ bulkTranslate error', e);
325327
return { ok: false, error: e.message };
326328
}
327329
}
328-
console.log('🪲bulkTranslate done', selectedIds);
330+
process.env.HEAVY_DEBUG && console.log('🪲bulkTranslate done', selectedIds);
329331
this.updateUntranslatedMenuBadge();
330332
return {
331333
ok: true, error: undefined,
332-
successMessage: await tr(`Translated {count} items`, 'backend', {count: selectedIds.length}),
334+
successMessage: await tr(`Translated {count} items`, 'backend', {
335+
count: translatedCount,
336+
}),
333337
};
334338
}
335339
}
@@ -411,7 +415,7 @@ export default class I18N extends AdminForthPlugin {
411415
let totalTranslated = 0;
412416
for (let i = 0; i < strings.length; i += maxKeysInOneReq) {
413417
const slicedStrings = strings.slice(i, i + maxKeysInOneReq);
414-
console.log('🪲🔪slicedStrings ', slicedStrings);
418+
process.env.HEAVY_DEBUG && console.log('🪲🔪slicedStrings len', slicedStrings.length);
415419
totalTranslated += await translateToLang(langIsoCode, slicedStrings, plurals);
416420
}
417421
return totalTranslated;
@@ -435,7 +439,7 @@ ${
435439
\`\`\`
436440
`;
437441

438-
process.env.HEAVY_DEBUG && console.log('llm prompt', prompt);
442+
process.env.HEAVY_DEBUG && console.log('🧠 llm prompt', prompt);
439443

440444
// call OpenAI
441445
const resp = await this.options.completeAdapter.complete(
@@ -444,7 +448,7 @@ ${
444448
300,
445449
);
446450

447-
process.env.HEAVY_DEBUG && console.log('llm resp', resp);
451+
process.env.HEAVY_DEBUG && console.log('🧠 llm resp', resp);
448452

449453
if (resp.error) {
450454
throw new AiTranslateError(resp.error);
@@ -460,7 +464,7 @@ ${
460464
res = resp.content.split("```json")[1].split("```")[0];
461465
} catch (e) {
462466
console.error('error in parsing OpenAI', resp);
463-
return;
467+
throw new AiTranslateError('Error in parsing OpenAI response');
464468
}
465469
res = JSON.parse(res);
466470

@@ -486,7 +490,7 @@ ${
486490
}
487491
}
488492

489-
return res.length;
493+
return Object.keys(updateStrings).length;
490494
}
491495

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

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "@adminforth/i18n",
3-
"version": "1.0.13",
3+
"version": "1.0.18-next.1",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"type": "module",
77
"scripts": {
8-
"build": "tsc && rsync -av --exclude 'node_modules' custom dist/ && npm version patch",
9-
"rollout": "npm run build && npm publish --access public",
8+
"build": "tsc && rsync -av --exclude 'node_modules' custom dist/",
9+
"rollout": "npm run build && npm version patch && npm publish --access public",
10+
"rollout-next": "npm run build && npm version prerelease --preid=next && npm publish --tag next",
1011
"prepare": "npm link adminforth"
1112
},
1213
"keywords": [],

0 commit comments

Comments
 (0)