@littlebox/strapi-suite 1.0.50 → 1.0.52
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/_chunks/{App-CUhT6I0p.js → App-C-h--Kv_.js} +565 -36
- package/dist/_chunks/{App-CVOV2lQP.mjs → App-C1HyspTk.mjs} +558 -29
- package/dist/_chunks/{SlugInput-Cy1-zfz_.js → SlugInput-3gkr6B0t.js} +3 -4
- package/dist/_chunks/{SlugInput-DDRjbmRt.mjs → SlugInput-CU9_b0dd.mjs} +1 -2
- package/dist/_chunks/{en-CGJcJfk1.js → en-CLNZnNeq.js} +30 -0
- package/dist/_chunks/{en-CRSiVe4w.mjs → en-F6RYx79c.mjs} +30 -0
- package/dist/_chunks/{index-B0aQw-t1.js → index-60sq8G6p.js} +667 -20
- package/dist/_chunks/{index-fPknNRw6.mjs → index-DQ3vX9So.mjs} +639 -9
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +2 -2
- package/dist/admin/src/components/DocumentTranslateBox.d.ts +5 -0
- package/dist/admin/src/components/DocumentTranslateModal.d.ts +11 -0
- package/dist/admin/src/components/TranslationAiModal.d.ts +8 -0
- package/dist/admin/src/components/TranslationSettingsModal.d.ts +6 -0
- package/dist/admin/src/contexts/settings.d.ts +1 -0
- package/dist/admin/src/core/config.d.ts +1 -0
- package/dist/admin/src/core/gateways/aiGateway.d.ts +19 -0
- package/dist/admin/src/core/gateways/documentGateway.d.ts +15 -0
- package/dist/admin/src/core/gateways/translationGateway.d.ts +13 -0
- package/dist/admin/src/core/usecases/translateDocument.d.ts +33 -0
- package/dist/admin/src/core/usecases/translateWithAi.d.ts +17 -0
- package/dist/admin/src/core/utils/documentFields.d.ts +23 -0
- package/dist/server/index.js +386 -79
- package/dist/server/index.mjs +386 -79
- package/dist/server/src/config/index.d.ts +4 -0
- package/dist/server/src/controllers/index.d.ts +6 -0
- package/dist/server/src/controllers/modules/ai-translation.d.ts +7 -0
- package/dist/server/src/controllers/modules/translation.d.ts +1 -0
- package/dist/server/src/index.d.ts +22 -1
- package/dist/server/src/services/app/setting.d.ts +3 -1
- package/dist/server/src/services/index.d.ts +16 -1
- package/dist/server/src/services/modules/ai-translation.d.ts +35 -0
- package/package.json +1 -1
- package/dist/_chunks/convertToSlug-B-OmazDp.js +0 -83
- package/dist/_chunks/convertToSlug-Bht0pDyl.mjs +0 -84
package/dist/server/index.js
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
const PLUGIN_ID = "littlebox-strapi-suite";
|
|
3
3
|
const SLUG_LANGUAGE_STRATEGY = "language";
|
|
4
4
|
const SLUG_CONTENT_STRATEGY = "content";
|
|
5
|
+
const AI_TRANSLATION_MODULE = "translation";
|
|
6
|
+
const AI_TRANSLATION_DEFAULT_MODEL = "gpt-4o";
|
|
7
|
+
const AI_TRANSLATION_BATCH_SIZE = 40;
|
|
8
|
+
const OPENAI_CHAT_COMPLETIONS_URL = "https://api.openai.com/v1/chat/completions";
|
|
5
9
|
const config = {
|
|
6
10
|
default: {
|
|
7
11
|
pluginId: PLUGIN_ID,
|
|
@@ -397,78 +401,104 @@ async function deleteAttributes(event, config2) {
|
|
|
397
401
|
}
|
|
398
402
|
const bootstrap = async ({ strapi: strapi2 }) => {
|
|
399
403
|
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
404
|
+
const defaultSettings = [
|
|
405
|
+
{
|
|
406
|
+
module: "menu",
|
|
407
|
+
property: "active",
|
|
408
|
+
type: "boolean",
|
|
409
|
+
value: "true"
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
module: "attribute",
|
|
413
|
+
property: "active",
|
|
414
|
+
type: "boolean",
|
|
415
|
+
value: "true"
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
module: "parameter",
|
|
419
|
+
property: "active",
|
|
420
|
+
type: "boolean",
|
|
421
|
+
value: "true"
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
module: "translation",
|
|
425
|
+
property: "active",
|
|
426
|
+
type: "boolean",
|
|
427
|
+
value: "true"
|
|
428
|
+
},
|
|
429
|
+
{
|
|
430
|
+
module: "translation",
|
|
431
|
+
property: "aiEnabled",
|
|
432
|
+
type: "boolean",
|
|
433
|
+
value: "false"
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
module: "translation",
|
|
437
|
+
property: "aiApiKey",
|
|
438
|
+
type: "string",
|
|
439
|
+
value: null
|
|
440
|
+
},
|
|
441
|
+
{
|
|
442
|
+
module: "translation",
|
|
443
|
+
property: "aiModel",
|
|
444
|
+
type: "string",
|
|
445
|
+
value: "gpt-4o"
|
|
446
|
+
},
|
|
447
|
+
{
|
|
448
|
+
module: "slug",
|
|
449
|
+
property: "active",
|
|
450
|
+
type: "boolean",
|
|
451
|
+
value: "true"
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
module: "slug",
|
|
455
|
+
property: "showDefaultLanguage",
|
|
456
|
+
type: "boolean",
|
|
457
|
+
value: "false"
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
module: "slug",
|
|
461
|
+
property: "homepageContentId",
|
|
462
|
+
type: "string",
|
|
463
|
+
value: null
|
|
464
|
+
},
|
|
465
|
+
{
|
|
466
|
+
module: "slug",
|
|
467
|
+
property: "homepageContentModel",
|
|
468
|
+
type: "string",
|
|
469
|
+
value: null
|
|
470
|
+
},
|
|
471
|
+
{
|
|
472
|
+
module: "slug",
|
|
473
|
+
property: "homepageSlugStrategy",
|
|
474
|
+
type: "string",
|
|
475
|
+
value: "language"
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
module: "template",
|
|
479
|
+
property: "active",
|
|
480
|
+
type: "boolean",
|
|
481
|
+
value: "true"
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
module: "locale",
|
|
485
|
+
property: "active",
|
|
486
|
+
type: "boolean",
|
|
487
|
+
value: "true"
|
|
488
|
+
}
|
|
489
|
+
];
|
|
490
|
+
for (const setting of defaultSettings) {
|
|
491
|
+
try {
|
|
492
|
+
const current = await strapi2.db.query(config2.uuid.app.setting).findOne({
|
|
493
|
+
where: {
|
|
494
|
+
module: setting.module,
|
|
495
|
+
property: setting.property
|
|
468
496
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
497
|
+
});
|
|
498
|
+
if (current) continue;
|
|
499
|
+
await strapi2.db.query(config2.uuid.app.setting).create({ data: setting });
|
|
500
|
+
} catch (e) {
|
|
501
|
+
}
|
|
472
502
|
}
|
|
473
503
|
try {
|
|
474
504
|
await strapi2.db.query(config2.uuid.modules.template).createMany({
|
|
@@ -1196,6 +1226,9 @@ const TranslationModuleController = ({ strapi: strapi2 }) => ({
|
|
|
1196
1226
|
async adminBulkDelete() {
|
|
1197
1227
|
return await strapi2.plugin(PLUGIN_ID).service("TranslationModuleService").adminBulkDelete();
|
|
1198
1228
|
},
|
|
1229
|
+
async adminAiTranslate() {
|
|
1230
|
+
return await strapi2.plugin(PLUGIN_ID).service("AiTranslationModuleService").adminTranslate();
|
|
1231
|
+
},
|
|
1199
1232
|
async getTranslations() {
|
|
1200
1233
|
return await strapi2.plugin(PLUGIN_ID).service("TranslationModuleService").getTranslations();
|
|
1201
1234
|
}
|
|
@@ -1211,6 +1244,11 @@ const LocalesModuleController = ({ strapi: strapi2 }) => ({
|
|
|
1211
1244
|
};
|
|
1212
1245
|
}
|
|
1213
1246
|
});
|
|
1247
|
+
const AiTranslationModuleController = ({ strapi: strapi2 }) => ({
|
|
1248
|
+
async adminTranslateStrings() {
|
|
1249
|
+
return await strapi2.plugin(PLUGIN_ID).service("AiTranslationModuleService").adminTranslateStrings();
|
|
1250
|
+
}
|
|
1251
|
+
});
|
|
1214
1252
|
const controllers = {
|
|
1215
1253
|
SettingAppController,
|
|
1216
1254
|
SlugModuleController,
|
|
@@ -1219,7 +1257,8 @@ const controllers = {
|
|
|
1219
1257
|
MenuModuleController,
|
|
1220
1258
|
ParameterModuleController,
|
|
1221
1259
|
TranslationModuleController,
|
|
1222
|
-
LocalesModuleController
|
|
1260
|
+
LocalesModuleController,
|
|
1261
|
+
AiTranslationModuleController
|
|
1223
1262
|
};
|
|
1224
1263
|
function getModuleName(url) {
|
|
1225
1264
|
if (url.includes("/pages")) return "slug";
|
|
@@ -1494,6 +1533,22 @@ const adminAPIRoutes = [
|
|
|
1494
1533
|
policies: ["admin::isAuthenticatedAdmin"]
|
|
1495
1534
|
}
|
|
1496
1535
|
},
|
|
1536
|
+
{
|
|
1537
|
+
method: "POST",
|
|
1538
|
+
path: "/admin/ai/strings",
|
|
1539
|
+
handler: "AiTranslationModuleController.adminTranslateStrings",
|
|
1540
|
+
config: {
|
|
1541
|
+
policies: ["admin::isAuthenticatedAdmin"]
|
|
1542
|
+
}
|
|
1543
|
+
},
|
|
1544
|
+
{
|
|
1545
|
+
method: "POST",
|
|
1546
|
+
path: "/admin/translations/actions/ai-translate",
|
|
1547
|
+
handler: "TranslationModuleController.adminAiTranslate",
|
|
1548
|
+
config: {
|
|
1549
|
+
policies: ["admin::isAuthenticatedAdmin"]
|
|
1550
|
+
}
|
|
1551
|
+
},
|
|
1497
1552
|
{
|
|
1498
1553
|
method: "POST",
|
|
1499
1554
|
path: "/admin/translations/actions/delete",
|
|
@@ -1545,6 +1600,22 @@ const routes = {
|
|
|
1545
1600
|
routes: contentAPIRoutes
|
|
1546
1601
|
}
|
|
1547
1602
|
};
|
|
1603
|
+
const SECRET_SETTINGS = [
|
|
1604
|
+
{ module: "translation", property: "aiApiKey" }
|
|
1605
|
+
];
|
|
1606
|
+
function isSecret(module2, property) {
|
|
1607
|
+
return SECRET_SETTINGS.some((secret) => secret.module === module2 && secret.property === property);
|
|
1608
|
+
}
|
|
1609
|
+
function maskSecret(value) {
|
|
1610
|
+
if (!value) return null;
|
|
1611
|
+
if (value.length <= 8) return "********";
|
|
1612
|
+
return `${value.slice(0, 3)}${"*".repeat(8)}${value.slice(-4)}`;
|
|
1613
|
+
}
|
|
1614
|
+
function inferType(value) {
|
|
1615
|
+
if (typeof value === "boolean") return "boolean";
|
|
1616
|
+
if (typeof value === "number") return "number";
|
|
1617
|
+
return "string";
|
|
1618
|
+
}
|
|
1548
1619
|
function parseSettings(data) {
|
|
1549
1620
|
const typeParsers = {
|
|
1550
1621
|
boolean: (value) => value === "true",
|
|
@@ -1554,7 +1625,7 @@ function parseSettings(data) {
|
|
|
1554
1625
|
};
|
|
1555
1626
|
const groupedByModule = data.reduce((acc, item) => {
|
|
1556
1627
|
const parser = typeParsers[item.type] || ((v) => v);
|
|
1557
|
-
const convertedValue = parser(item.value);
|
|
1628
|
+
const convertedValue = isSecret(item.module, item.property) ? maskSecret(item.value) : parser(item.value);
|
|
1558
1629
|
let moduleObject = acc.find((obj) => obj.module === item.module);
|
|
1559
1630
|
if (!moduleObject) {
|
|
1560
1631
|
moduleObject = { module: item.module, settings: {} };
|
|
@@ -1572,18 +1643,39 @@ const SettingAppService = ({ strapi: strapi2 }) => ({
|
|
|
1572
1643
|
const parsedSettings = parseSettings(settings);
|
|
1573
1644
|
return parsedSettings || {};
|
|
1574
1645
|
},
|
|
1646
|
+
async getRawValue(module2, property) {
|
|
1647
|
+
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
1648
|
+
const setting = await strapi2.db.query(config2.uuid.app.setting).findOne({
|
|
1649
|
+
where: { module: module2, property }
|
|
1650
|
+
});
|
|
1651
|
+
return setting ? setting.value : null;
|
|
1652
|
+
},
|
|
1575
1653
|
async update(ctx) {
|
|
1576
1654
|
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
1655
|
+
const { module: module2, property, value } = ctx.request.body;
|
|
1577
1656
|
const setting = await strapi2.db.query(config2.uuid.app.setting).findOne({
|
|
1578
1657
|
where: {
|
|
1579
|
-
module:
|
|
1580
|
-
property
|
|
1658
|
+
module: module2,
|
|
1659
|
+
property
|
|
1581
1660
|
}
|
|
1582
1661
|
});
|
|
1662
|
+
if (isSecret(module2, property) && (value === null || value === void 0 || value === "")) {
|
|
1663
|
+
return setting;
|
|
1664
|
+
}
|
|
1665
|
+
if (!setting) {
|
|
1666
|
+
return await strapi2.db.query(config2.uuid.app.setting).create({
|
|
1667
|
+
data: {
|
|
1668
|
+
module: module2,
|
|
1669
|
+
property,
|
|
1670
|
+
type: inferType(value),
|
|
1671
|
+
value: String(value)
|
|
1672
|
+
}
|
|
1673
|
+
});
|
|
1674
|
+
}
|
|
1583
1675
|
return await strapi2.documents(config2.uuid.app.setting).update({
|
|
1584
1676
|
documentId: setting.documentId,
|
|
1585
1677
|
data: {
|
|
1586
|
-
value: String(
|
|
1678
|
+
value: String(value),
|
|
1587
1679
|
updated_by_id: ctx.state.user.id
|
|
1588
1680
|
}
|
|
1589
1681
|
});
|
|
@@ -2647,7 +2739,7 @@ const ParameterModuleService = ({ strapi: strapi2 }) => ({
|
|
|
2647
2739
|
return documents || [];
|
|
2648
2740
|
}
|
|
2649
2741
|
});
|
|
2650
|
-
function groupByUid(items) {
|
|
2742
|
+
function groupByUid$1(items) {
|
|
2651
2743
|
const groupedMap = items.reduce((acc, item) => {
|
|
2652
2744
|
if (acc[item.uid]) {
|
|
2653
2745
|
acc[item.uid].translations[item.locale] = item.translation;
|
|
@@ -2720,7 +2812,221 @@ const TranslationModuleService = ({ strapi: strapi2 }) => ({
|
|
|
2720
2812
|
translation: "asc"
|
|
2721
2813
|
}
|
|
2722
2814
|
});
|
|
2723
|
-
return documents ? groupByUid(documents) : [];
|
|
2815
|
+
return documents ? groupByUid$1(documents) : [];
|
|
2816
|
+
}
|
|
2817
|
+
});
|
|
2818
|
+
const REQUEST_TIMEOUT = 12e4;
|
|
2819
|
+
function groupByUid(items) {
|
|
2820
|
+
const groupedMap = items.reduce((acc, item) => {
|
|
2821
|
+
if (!acc[item.uid]) {
|
|
2822
|
+
acc[item.uid] = { uid: item.uid, translations: {} };
|
|
2823
|
+
}
|
|
2824
|
+
acc[item.uid].translations[item.locale] = item.translation;
|
|
2825
|
+
return acc;
|
|
2826
|
+
}, {});
|
|
2827
|
+
return Object.values(groupedMap);
|
|
2828
|
+
}
|
|
2829
|
+
function chunk(items, size) {
|
|
2830
|
+
const batches = [];
|
|
2831
|
+
for (let index2 = 0; index2 < items.length; index2 += size) {
|
|
2832
|
+
batches.push(items.slice(index2, index2 + size));
|
|
2833
|
+
}
|
|
2834
|
+
return batches;
|
|
2835
|
+
}
|
|
2836
|
+
function isFilled(value) {
|
|
2837
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
2838
|
+
}
|
|
2839
|
+
function buildSystemPrompt(source, target) {
|
|
2840
|
+
return [
|
|
2841
|
+
`You are a professional translator and copywriter, a native speaker of ${target.name} (locale code ${target.code}), translating from ${source.name} (locale code ${source.code}).`,
|
|
2842
|
+
`The strings you receive are interface copy: labels, buttons, titles, menu entries, notifications and short messages shown to the users of a website or application.`,
|
|
2843
|
+
`Translate them so that a native ${target.name} speaker would never suspect the text was translated. Follow every rule below.`,
|
|
2844
|
+
``,
|
|
2845
|
+
`1. Translate the meaning, never word by word. When the source uses an idiom, a saying, a metaphor or a play on words, replace it with the expression a native ${target.name} speaker would genuinely use in the same situation. Preserving the intent and the logic behind the sentence matters more than preserving its words. When no natural equivalent exists, write the plain, natural sentence that carries the same idea.`,
|
|
2846
|
+
`2. Do not use hyphens or dashes of any kind (-, –, —) as a stylistic device to join clauses, ideas or appositions. Rewrite the sentence instead, splitting it or using the connectors that are natural in ${target.name}. Hyphens are acceptable only when they are a mandatory part of the correct spelling of a word in ${target.name}.`,
|
|
2847
|
+
`3. Match the register and the tone of the source. Informal stays informal, formal stays formal, and the form of address must be the one standard for user interfaces in ${target.name}.`,
|
|
2848
|
+
`4. Stay close to the length of the source. Interface copy has to fit inside buttons and labels, so be concise and never add words the source does not have.`,
|
|
2849
|
+
`5. Preserve exactly, without translating: placeholders such as {name}, {{name}}, %s, %d, :param and $variable; HTML tags and entities; markdown syntax; URLs; email addresses; numbers; and brand or product names.`,
|
|
2850
|
+
`6. Preserve the capitalisation style of the source (sentence case, Title Case, ALL CAPS) adapted to what is correct in ${target.name}, and keep the leading and trailing punctuation, the ellipses and the question or exclamation marks.`,
|
|
2851
|
+
`7. Use the spelling, the vocabulary and the conventions of the ${target.name} variant identified by the code ${target.code}.`,
|
|
2852
|
+
`8. Never explain, comment or add anything, and never leave any part of the text in the source language.`,
|
|
2853
|
+
``,
|
|
2854
|
+
`You receive a JSON object that maps keys to source strings. Return a JSON object with exactly the same keys, where each value is the translation of the corresponding source string. Return nothing else.`
|
|
2855
|
+
].join("\n");
|
|
2856
|
+
}
|
|
2857
|
+
async function requestOpenAi(apiKey, model, systemPrompt, payload, withTemperature = true) {
|
|
2858
|
+
const controller = new AbortController();
|
|
2859
|
+
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT);
|
|
2860
|
+
try {
|
|
2861
|
+
const body = {
|
|
2862
|
+
model,
|
|
2863
|
+
response_format: { type: "json_object" },
|
|
2864
|
+
messages: [
|
|
2865
|
+
{ role: "system", content: systemPrompt },
|
|
2866
|
+
{ role: "user", content: JSON.stringify(payload) }
|
|
2867
|
+
]
|
|
2868
|
+
};
|
|
2869
|
+
if (withTemperature) body.temperature = 0.3;
|
|
2870
|
+
const response = await fetch(OPENAI_CHAT_COMPLETIONS_URL, {
|
|
2871
|
+
method: "POST",
|
|
2872
|
+
headers: {
|
|
2873
|
+
"Content-Type": "application/json",
|
|
2874
|
+
Authorization: `Bearer ${apiKey}`
|
|
2875
|
+
},
|
|
2876
|
+
body: JSON.stringify(body),
|
|
2877
|
+
signal: controller.signal
|
|
2878
|
+
});
|
|
2879
|
+
if (!response.ok) {
|
|
2880
|
+
const error = await response.text();
|
|
2881
|
+
if (withTemperature && response.status === 400 && error.includes("temperature")) {
|
|
2882
|
+
clearTimeout(timeout);
|
|
2883
|
+
return requestOpenAi(apiKey, model, systemPrompt, payload, false);
|
|
2884
|
+
}
|
|
2885
|
+
throw new Error(`OpenAI request failed with status ${response.status}: ${error}`);
|
|
2886
|
+
}
|
|
2887
|
+
const data = await response.json();
|
|
2888
|
+
const content = data?.choices?.[0]?.message?.content;
|
|
2889
|
+
if (!content) throw new Error("OpenAI returned an empty response");
|
|
2890
|
+
return JSON.parse(content);
|
|
2891
|
+
} finally {
|
|
2892
|
+
clearTimeout(timeout);
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
const AiTranslationModuleService = ({ strapi: strapi2 }) => ({
|
|
2896
|
+
async getLocales() {
|
|
2897
|
+
const locales = await strapi2.plugin("i18n").service("locales").find();
|
|
2898
|
+
return locales.map((locale) => ({ code: locale.code, name: locale.name }));
|
|
2899
|
+
},
|
|
2900
|
+
async getCredentials() {
|
|
2901
|
+
const settingService = strapi2.plugin(PLUGIN_ID).service("SettingAppService");
|
|
2902
|
+
const aiEnabled = await settingService.getRawValue(AI_TRANSLATION_MODULE, "aiEnabled");
|
|
2903
|
+
if (aiEnabled !== "true") return null;
|
|
2904
|
+
const apiKey = await settingService.getRawValue(AI_TRANSLATION_MODULE, "aiApiKey");
|
|
2905
|
+
if (!isFilled(apiKey)) return null;
|
|
2906
|
+
const storedModel = await settingService.getRawValue(AI_TRANSLATION_MODULE, "aiModel");
|
|
2907
|
+
return {
|
|
2908
|
+
apiKey,
|
|
2909
|
+
model: isFilled(storedModel) ? storedModel : AI_TRANSLATION_DEFAULT_MODEL
|
|
2910
|
+
};
|
|
2911
|
+
},
|
|
2912
|
+
async translateStrings(credentials, source, target, strings) {
|
|
2913
|
+
const translations = {};
|
|
2914
|
+
const errors = [];
|
|
2915
|
+
let failed = 0;
|
|
2916
|
+
const systemPrompt = buildSystemPrompt(source, target);
|
|
2917
|
+
const keys = Object.keys(strings).filter((key) => isFilled(strings[key]));
|
|
2918
|
+
for (const batch of chunk(keys, AI_TRANSLATION_BATCH_SIZE)) {
|
|
2919
|
+
const payload = batch.reduce((acc, key) => {
|
|
2920
|
+
acc[key] = strings[key];
|
|
2921
|
+
return acc;
|
|
2922
|
+
}, {});
|
|
2923
|
+
let result;
|
|
2924
|
+
try {
|
|
2925
|
+
result = await requestOpenAi(credentials.apiKey, credentials.model, systemPrompt, payload);
|
|
2926
|
+
} catch (e) {
|
|
2927
|
+
failed += batch.length;
|
|
2928
|
+
errors.push(`${target.code}: ${e.message}`);
|
|
2929
|
+
strapi2.log.error(`[${PLUGIN_ID}] AI translation failed for ${target.code}: ${e.message}`);
|
|
2930
|
+
continue;
|
|
2931
|
+
}
|
|
2932
|
+
for (const key of batch) {
|
|
2933
|
+
if (isFilled(result[key])) {
|
|
2934
|
+
translations[key] = result[key];
|
|
2935
|
+
} else {
|
|
2936
|
+
failed += 1;
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2940
|
+
return { translations, failed, errors };
|
|
2941
|
+
},
|
|
2942
|
+
async adminTranslate() {
|
|
2943
|
+
const ctx = strapi2.requestContext.get();
|
|
2944
|
+
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
2945
|
+
const { sourceLocale, targetLocales, overwrite } = ctx.request.body;
|
|
2946
|
+
const credentials = await this.getCredentials();
|
|
2947
|
+
if (!credentials) {
|
|
2948
|
+
return ctx.badRequest("AI translations are disabled or the OpenAI API key is not set");
|
|
2949
|
+
}
|
|
2950
|
+
if (!isFilled(sourceLocale)) {
|
|
2951
|
+
return ctx.badRequest("A source locale is required");
|
|
2952
|
+
}
|
|
2953
|
+
const targets = (targetLocales || []).filter(
|
|
2954
|
+
(locale) => isFilled(locale) && locale !== sourceLocale
|
|
2955
|
+
);
|
|
2956
|
+
if (targets.length === 0) {
|
|
2957
|
+
return ctx.badRequest("At least one target locale is required");
|
|
2958
|
+
}
|
|
2959
|
+
const locales = await this.getLocales();
|
|
2960
|
+
const source = locales.find((locale) => locale.code === sourceLocale);
|
|
2961
|
+
if (!source) {
|
|
2962
|
+
return ctx.badRequest(`The locale ${sourceLocale} is not configured in Strapi`);
|
|
2963
|
+
}
|
|
2964
|
+
const query = strapi2.db.query(config2.uuid.modules.translation);
|
|
2965
|
+
const documents = await query.findMany({ select: ["id", "uid", "translation", "locale"] });
|
|
2966
|
+
const entries = groupByUid(documents).filter(
|
|
2967
|
+
(entry) => isFilled(entry.translations[source.code])
|
|
2968
|
+
);
|
|
2969
|
+
if (entries.length === 0) {
|
|
2970
|
+
return { translated: 0, skipped: 0, failed: 0, errors: [] };
|
|
2971
|
+
}
|
|
2972
|
+
let translated = 0;
|
|
2973
|
+
let skipped = 0;
|
|
2974
|
+
let failed = 0;
|
|
2975
|
+
const errors = [];
|
|
2976
|
+
for (const targetCode of targets) {
|
|
2977
|
+
const target = locales.find((locale) => locale.code === targetCode);
|
|
2978
|
+
if (!target) {
|
|
2979
|
+
errors.push(`The locale ${targetCode} is not configured in Strapi`);
|
|
2980
|
+
continue;
|
|
2981
|
+
}
|
|
2982
|
+
const pending = entries.filter(
|
|
2983
|
+
(entry) => overwrite || !isFilled(entry.translations[target.code])
|
|
2984
|
+
);
|
|
2985
|
+
skipped += entries.length - pending.length;
|
|
2986
|
+
if (pending.length === 0) continue;
|
|
2987
|
+
const strings = pending.reduce((acc, entry) => {
|
|
2988
|
+
acc[entry.uid] = entry.translations[source.code];
|
|
2989
|
+
return acc;
|
|
2990
|
+
}, {});
|
|
2991
|
+
const result = await this.translateStrings(credentials, source, target, strings);
|
|
2992
|
+
failed += result.failed;
|
|
2993
|
+
errors.push(...result.errors);
|
|
2994
|
+
for (const [uid, translation] of Object.entries(result.translations)) {
|
|
2995
|
+
const existing = await query.findOne({ where: { uid, locale: target.code } });
|
|
2996
|
+
if (existing) {
|
|
2997
|
+
await query.update({ where: { id: existing.id }, data: { translation } });
|
|
2998
|
+
} else {
|
|
2999
|
+
await query.create({ data: { uid, translation, locale: target.code } });
|
|
3000
|
+
}
|
|
3001
|
+
const entry = entries.find((item) => item.uid === uid);
|
|
3002
|
+
if (entry) entry.translations[target.code] = translation;
|
|
3003
|
+
translated += 1;
|
|
3004
|
+
}
|
|
3005
|
+
}
|
|
3006
|
+
return { translated, skipped, failed, errors };
|
|
3007
|
+
},
|
|
3008
|
+
async adminTranslateStrings() {
|
|
3009
|
+
const ctx = strapi2.requestContext.get();
|
|
3010
|
+
const { sourceLocale, targetLocale, strings } = ctx.request.body;
|
|
3011
|
+
const credentials = await this.getCredentials();
|
|
3012
|
+
if (!credentials) {
|
|
3013
|
+
return ctx.badRequest("AI translations are disabled or the OpenAI API key is not set");
|
|
3014
|
+
}
|
|
3015
|
+
if (!isFilled(sourceLocale) || !isFilled(targetLocale)) {
|
|
3016
|
+
return ctx.badRequest("A source locale and a target locale are required");
|
|
3017
|
+
}
|
|
3018
|
+
if (sourceLocale === targetLocale) {
|
|
3019
|
+
return ctx.badRequest("The source locale and the target locale must be different");
|
|
3020
|
+
}
|
|
3021
|
+
if (!strings || Object.keys(strings).length === 0) {
|
|
3022
|
+
return { translations: {}, failed: 0, errors: [] };
|
|
3023
|
+
}
|
|
3024
|
+
const locales = await this.getLocales();
|
|
3025
|
+
const source = locales.find((locale) => locale.code === sourceLocale);
|
|
3026
|
+
const target = locales.find((locale) => locale.code === targetLocale);
|
|
3027
|
+
if (!source) return ctx.badRequest(`The locale ${sourceLocale} is not configured in Strapi`);
|
|
3028
|
+
if (!target) return ctx.badRequest(`The locale ${targetLocale} is not configured in Strapi`);
|
|
3029
|
+
return await this.translateStrings(credentials, source, target, strings);
|
|
2724
3030
|
}
|
|
2725
3031
|
});
|
|
2726
3032
|
const services = {
|
|
@@ -2730,7 +3036,8 @@ const services = {
|
|
|
2730
3036
|
TemplateModuleService,
|
|
2731
3037
|
MenuModuleService,
|
|
2732
3038
|
ParameterModuleService,
|
|
2733
|
-
TranslationModuleService
|
|
3039
|
+
TranslationModuleService,
|
|
3040
|
+
AiTranslationModuleService
|
|
2734
3041
|
};
|
|
2735
3042
|
const index = {
|
|
2736
3043
|
register,
|