@littlebox/strapi-suite 1.0.51 → 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-C7RgdLC-.js → App-C-h--Kv_.js} +557 -35
- package/dist/_chunks/{App-b_1MuDrk.mjs → App-C1HyspTk.mjs} +550 -28
- package/dist/_chunks/{SlugInput-DvQWzpBu.js → SlugInput-3gkr6B0t.js} +3 -4
- package/dist/_chunks/{SlugInput-DlOanbzA.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-UZTUyNY3.js → index-60sq8G6p.js} +667 -20
- package/dist/_chunks/{index-C3MUddO-.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-DmgViUjP.mjs +0 -84
- package/dist/_chunks/convertToSlug-DqHa9ej4.js +0 -83
package/dist/server/index.mjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
const PLUGIN_ID = "littlebox-strapi-suite";
|
|
2
2
|
const SLUG_LANGUAGE_STRATEGY = "language";
|
|
3
3
|
const SLUG_CONTENT_STRATEGY = "content";
|
|
4
|
+
const AI_TRANSLATION_MODULE = "translation";
|
|
5
|
+
const AI_TRANSLATION_DEFAULT_MODEL = "gpt-4o";
|
|
6
|
+
const AI_TRANSLATION_BATCH_SIZE = 40;
|
|
7
|
+
const OPENAI_CHAT_COMPLETIONS_URL = "https://api.openai.com/v1/chat/completions";
|
|
4
8
|
const config = {
|
|
5
9
|
default: {
|
|
6
10
|
pluginId: PLUGIN_ID,
|
|
@@ -396,78 +400,104 @@ async function deleteAttributes(event, config2) {
|
|
|
396
400
|
}
|
|
397
401
|
const bootstrap = async ({ strapi: strapi2 }) => {
|
|
398
402
|
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
399
|
-
|
|
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
|
-
|
|
403
|
+
const defaultSettings = [
|
|
404
|
+
{
|
|
405
|
+
module: "menu",
|
|
406
|
+
property: "active",
|
|
407
|
+
type: "boolean",
|
|
408
|
+
value: "true"
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
module: "attribute",
|
|
412
|
+
property: "active",
|
|
413
|
+
type: "boolean",
|
|
414
|
+
value: "true"
|
|
415
|
+
},
|
|
416
|
+
{
|
|
417
|
+
module: "parameter",
|
|
418
|
+
property: "active",
|
|
419
|
+
type: "boolean",
|
|
420
|
+
value: "true"
|
|
421
|
+
},
|
|
422
|
+
{
|
|
423
|
+
module: "translation",
|
|
424
|
+
property: "active",
|
|
425
|
+
type: "boolean",
|
|
426
|
+
value: "true"
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
module: "translation",
|
|
430
|
+
property: "aiEnabled",
|
|
431
|
+
type: "boolean",
|
|
432
|
+
value: "false"
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
module: "translation",
|
|
436
|
+
property: "aiApiKey",
|
|
437
|
+
type: "string",
|
|
438
|
+
value: null
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
module: "translation",
|
|
442
|
+
property: "aiModel",
|
|
443
|
+
type: "string",
|
|
444
|
+
value: "gpt-4o"
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
module: "slug",
|
|
448
|
+
property: "active",
|
|
449
|
+
type: "boolean",
|
|
450
|
+
value: "true"
|
|
451
|
+
},
|
|
452
|
+
{
|
|
453
|
+
module: "slug",
|
|
454
|
+
property: "showDefaultLanguage",
|
|
455
|
+
type: "boolean",
|
|
456
|
+
value: "false"
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
module: "slug",
|
|
460
|
+
property: "homepageContentId",
|
|
461
|
+
type: "string",
|
|
462
|
+
value: null
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
module: "slug",
|
|
466
|
+
property: "homepageContentModel",
|
|
467
|
+
type: "string",
|
|
468
|
+
value: null
|
|
469
|
+
},
|
|
470
|
+
{
|
|
471
|
+
module: "slug",
|
|
472
|
+
property: "homepageSlugStrategy",
|
|
473
|
+
type: "string",
|
|
474
|
+
value: "language"
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
module: "template",
|
|
478
|
+
property: "active",
|
|
479
|
+
type: "boolean",
|
|
480
|
+
value: "true"
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
module: "locale",
|
|
484
|
+
property: "active",
|
|
485
|
+
type: "boolean",
|
|
486
|
+
value: "true"
|
|
487
|
+
}
|
|
488
|
+
];
|
|
489
|
+
for (const setting of defaultSettings) {
|
|
490
|
+
try {
|
|
491
|
+
const current = await strapi2.db.query(config2.uuid.app.setting).findOne({
|
|
492
|
+
where: {
|
|
493
|
+
module: setting.module,
|
|
494
|
+
property: setting.property
|
|
467
495
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
496
|
+
});
|
|
497
|
+
if (current) continue;
|
|
498
|
+
await strapi2.db.query(config2.uuid.app.setting).create({ data: setting });
|
|
499
|
+
} catch (e) {
|
|
500
|
+
}
|
|
471
501
|
}
|
|
472
502
|
try {
|
|
473
503
|
await strapi2.db.query(config2.uuid.modules.template).createMany({
|
|
@@ -1195,6 +1225,9 @@ const TranslationModuleController = ({ strapi: strapi2 }) => ({
|
|
|
1195
1225
|
async adminBulkDelete() {
|
|
1196
1226
|
return await strapi2.plugin(PLUGIN_ID).service("TranslationModuleService").adminBulkDelete();
|
|
1197
1227
|
},
|
|
1228
|
+
async adminAiTranslate() {
|
|
1229
|
+
return await strapi2.plugin(PLUGIN_ID).service("AiTranslationModuleService").adminTranslate();
|
|
1230
|
+
},
|
|
1198
1231
|
async getTranslations() {
|
|
1199
1232
|
return await strapi2.plugin(PLUGIN_ID).service("TranslationModuleService").getTranslations();
|
|
1200
1233
|
}
|
|
@@ -1210,6 +1243,11 @@ const LocalesModuleController = ({ strapi: strapi2 }) => ({
|
|
|
1210
1243
|
};
|
|
1211
1244
|
}
|
|
1212
1245
|
});
|
|
1246
|
+
const AiTranslationModuleController = ({ strapi: strapi2 }) => ({
|
|
1247
|
+
async adminTranslateStrings() {
|
|
1248
|
+
return await strapi2.plugin(PLUGIN_ID).service("AiTranslationModuleService").adminTranslateStrings();
|
|
1249
|
+
}
|
|
1250
|
+
});
|
|
1213
1251
|
const controllers = {
|
|
1214
1252
|
SettingAppController,
|
|
1215
1253
|
SlugModuleController,
|
|
@@ -1218,7 +1256,8 @@ const controllers = {
|
|
|
1218
1256
|
MenuModuleController,
|
|
1219
1257
|
ParameterModuleController,
|
|
1220
1258
|
TranslationModuleController,
|
|
1221
|
-
LocalesModuleController
|
|
1259
|
+
LocalesModuleController,
|
|
1260
|
+
AiTranslationModuleController
|
|
1222
1261
|
};
|
|
1223
1262
|
function getModuleName(url) {
|
|
1224
1263
|
if (url.includes("/pages")) return "slug";
|
|
@@ -1493,6 +1532,22 @@ const adminAPIRoutes = [
|
|
|
1493
1532
|
policies: ["admin::isAuthenticatedAdmin"]
|
|
1494
1533
|
}
|
|
1495
1534
|
},
|
|
1535
|
+
{
|
|
1536
|
+
method: "POST",
|
|
1537
|
+
path: "/admin/ai/strings",
|
|
1538
|
+
handler: "AiTranslationModuleController.adminTranslateStrings",
|
|
1539
|
+
config: {
|
|
1540
|
+
policies: ["admin::isAuthenticatedAdmin"]
|
|
1541
|
+
}
|
|
1542
|
+
},
|
|
1543
|
+
{
|
|
1544
|
+
method: "POST",
|
|
1545
|
+
path: "/admin/translations/actions/ai-translate",
|
|
1546
|
+
handler: "TranslationModuleController.adminAiTranslate",
|
|
1547
|
+
config: {
|
|
1548
|
+
policies: ["admin::isAuthenticatedAdmin"]
|
|
1549
|
+
}
|
|
1550
|
+
},
|
|
1496
1551
|
{
|
|
1497
1552
|
method: "POST",
|
|
1498
1553
|
path: "/admin/translations/actions/delete",
|
|
@@ -1544,6 +1599,22 @@ const routes = {
|
|
|
1544
1599
|
routes: contentAPIRoutes
|
|
1545
1600
|
}
|
|
1546
1601
|
};
|
|
1602
|
+
const SECRET_SETTINGS = [
|
|
1603
|
+
{ module: "translation", property: "aiApiKey" }
|
|
1604
|
+
];
|
|
1605
|
+
function isSecret(module, property) {
|
|
1606
|
+
return SECRET_SETTINGS.some((secret) => secret.module === module && secret.property === property);
|
|
1607
|
+
}
|
|
1608
|
+
function maskSecret(value) {
|
|
1609
|
+
if (!value) return null;
|
|
1610
|
+
if (value.length <= 8) return "********";
|
|
1611
|
+
return `${value.slice(0, 3)}${"*".repeat(8)}${value.slice(-4)}`;
|
|
1612
|
+
}
|
|
1613
|
+
function inferType(value) {
|
|
1614
|
+
if (typeof value === "boolean") return "boolean";
|
|
1615
|
+
if (typeof value === "number") return "number";
|
|
1616
|
+
return "string";
|
|
1617
|
+
}
|
|
1547
1618
|
function parseSettings(data) {
|
|
1548
1619
|
const typeParsers = {
|
|
1549
1620
|
boolean: (value) => value === "true",
|
|
@@ -1553,7 +1624,7 @@ function parseSettings(data) {
|
|
|
1553
1624
|
};
|
|
1554
1625
|
const groupedByModule = data.reduce((acc, item) => {
|
|
1555
1626
|
const parser = typeParsers[item.type] || ((v) => v);
|
|
1556
|
-
const convertedValue = parser(item.value);
|
|
1627
|
+
const convertedValue = isSecret(item.module, item.property) ? maskSecret(item.value) : parser(item.value);
|
|
1557
1628
|
let moduleObject = acc.find((obj) => obj.module === item.module);
|
|
1558
1629
|
if (!moduleObject) {
|
|
1559
1630
|
moduleObject = { module: item.module, settings: {} };
|
|
@@ -1571,18 +1642,39 @@ const SettingAppService = ({ strapi: strapi2 }) => ({
|
|
|
1571
1642
|
const parsedSettings = parseSettings(settings);
|
|
1572
1643
|
return parsedSettings || {};
|
|
1573
1644
|
},
|
|
1645
|
+
async getRawValue(module, property) {
|
|
1646
|
+
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
1647
|
+
const setting = await strapi2.db.query(config2.uuid.app.setting).findOne({
|
|
1648
|
+
where: { module, property }
|
|
1649
|
+
});
|
|
1650
|
+
return setting ? setting.value : null;
|
|
1651
|
+
},
|
|
1574
1652
|
async update(ctx) {
|
|
1575
1653
|
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
1654
|
+
const { module, property, value } = ctx.request.body;
|
|
1576
1655
|
const setting = await strapi2.db.query(config2.uuid.app.setting).findOne({
|
|
1577
1656
|
where: {
|
|
1578
|
-
module
|
|
1579
|
-
property
|
|
1657
|
+
module,
|
|
1658
|
+
property
|
|
1580
1659
|
}
|
|
1581
1660
|
});
|
|
1661
|
+
if (isSecret(module, property) && (value === null || value === void 0 || value === "")) {
|
|
1662
|
+
return setting;
|
|
1663
|
+
}
|
|
1664
|
+
if (!setting) {
|
|
1665
|
+
return await strapi2.db.query(config2.uuid.app.setting).create({
|
|
1666
|
+
data: {
|
|
1667
|
+
module,
|
|
1668
|
+
property,
|
|
1669
|
+
type: inferType(value),
|
|
1670
|
+
value: String(value)
|
|
1671
|
+
}
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1582
1674
|
return await strapi2.documents(config2.uuid.app.setting).update({
|
|
1583
1675
|
documentId: setting.documentId,
|
|
1584
1676
|
data: {
|
|
1585
|
-
value: String(
|
|
1677
|
+
value: String(value),
|
|
1586
1678
|
updated_by_id: ctx.state.user.id
|
|
1587
1679
|
}
|
|
1588
1680
|
});
|
|
@@ -2646,7 +2738,7 @@ const ParameterModuleService = ({ strapi: strapi2 }) => ({
|
|
|
2646
2738
|
return documents || [];
|
|
2647
2739
|
}
|
|
2648
2740
|
});
|
|
2649
|
-
function groupByUid(items) {
|
|
2741
|
+
function groupByUid$1(items) {
|
|
2650
2742
|
const groupedMap = items.reduce((acc, item) => {
|
|
2651
2743
|
if (acc[item.uid]) {
|
|
2652
2744
|
acc[item.uid].translations[item.locale] = item.translation;
|
|
@@ -2719,7 +2811,221 @@ const TranslationModuleService = ({ strapi: strapi2 }) => ({
|
|
|
2719
2811
|
translation: "asc"
|
|
2720
2812
|
}
|
|
2721
2813
|
});
|
|
2722
|
-
return documents ? groupByUid(documents) : [];
|
|
2814
|
+
return documents ? groupByUid$1(documents) : [];
|
|
2815
|
+
}
|
|
2816
|
+
});
|
|
2817
|
+
const REQUEST_TIMEOUT = 12e4;
|
|
2818
|
+
function groupByUid(items) {
|
|
2819
|
+
const groupedMap = items.reduce((acc, item) => {
|
|
2820
|
+
if (!acc[item.uid]) {
|
|
2821
|
+
acc[item.uid] = { uid: item.uid, translations: {} };
|
|
2822
|
+
}
|
|
2823
|
+
acc[item.uid].translations[item.locale] = item.translation;
|
|
2824
|
+
return acc;
|
|
2825
|
+
}, {});
|
|
2826
|
+
return Object.values(groupedMap);
|
|
2827
|
+
}
|
|
2828
|
+
function chunk(items, size) {
|
|
2829
|
+
const batches = [];
|
|
2830
|
+
for (let index2 = 0; index2 < items.length; index2 += size) {
|
|
2831
|
+
batches.push(items.slice(index2, index2 + size));
|
|
2832
|
+
}
|
|
2833
|
+
return batches;
|
|
2834
|
+
}
|
|
2835
|
+
function isFilled(value) {
|
|
2836
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
2837
|
+
}
|
|
2838
|
+
function buildSystemPrompt(source, target) {
|
|
2839
|
+
return [
|
|
2840
|
+
`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}).`,
|
|
2841
|
+
`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.`,
|
|
2842
|
+
`Translate them so that a native ${target.name} speaker would never suspect the text was translated. Follow every rule below.`,
|
|
2843
|
+
``,
|
|
2844
|
+
`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.`,
|
|
2845
|
+
`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}.`,
|
|
2846
|
+
`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}.`,
|
|
2847
|
+
`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.`,
|
|
2848
|
+
`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.`,
|
|
2849
|
+
`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.`,
|
|
2850
|
+
`7. Use the spelling, the vocabulary and the conventions of the ${target.name} variant identified by the code ${target.code}.`,
|
|
2851
|
+
`8. Never explain, comment or add anything, and never leave any part of the text in the source language.`,
|
|
2852
|
+
``,
|
|
2853
|
+
`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.`
|
|
2854
|
+
].join("\n");
|
|
2855
|
+
}
|
|
2856
|
+
async function requestOpenAi(apiKey, model, systemPrompt, payload, withTemperature = true) {
|
|
2857
|
+
const controller = new AbortController();
|
|
2858
|
+
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT);
|
|
2859
|
+
try {
|
|
2860
|
+
const body = {
|
|
2861
|
+
model,
|
|
2862
|
+
response_format: { type: "json_object" },
|
|
2863
|
+
messages: [
|
|
2864
|
+
{ role: "system", content: systemPrompt },
|
|
2865
|
+
{ role: "user", content: JSON.stringify(payload) }
|
|
2866
|
+
]
|
|
2867
|
+
};
|
|
2868
|
+
if (withTemperature) body.temperature = 0.3;
|
|
2869
|
+
const response = await fetch(OPENAI_CHAT_COMPLETIONS_URL, {
|
|
2870
|
+
method: "POST",
|
|
2871
|
+
headers: {
|
|
2872
|
+
"Content-Type": "application/json",
|
|
2873
|
+
Authorization: `Bearer ${apiKey}`
|
|
2874
|
+
},
|
|
2875
|
+
body: JSON.stringify(body),
|
|
2876
|
+
signal: controller.signal
|
|
2877
|
+
});
|
|
2878
|
+
if (!response.ok) {
|
|
2879
|
+
const error = await response.text();
|
|
2880
|
+
if (withTemperature && response.status === 400 && error.includes("temperature")) {
|
|
2881
|
+
clearTimeout(timeout);
|
|
2882
|
+
return requestOpenAi(apiKey, model, systemPrompt, payload, false);
|
|
2883
|
+
}
|
|
2884
|
+
throw new Error(`OpenAI request failed with status ${response.status}: ${error}`);
|
|
2885
|
+
}
|
|
2886
|
+
const data = await response.json();
|
|
2887
|
+
const content = data?.choices?.[0]?.message?.content;
|
|
2888
|
+
if (!content) throw new Error("OpenAI returned an empty response");
|
|
2889
|
+
return JSON.parse(content);
|
|
2890
|
+
} finally {
|
|
2891
|
+
clearTimeout(timeout);
|
|
2892
|
+
}
|
|
2893
|
+
}
|
|
2894
|
+
const AiTranslationModuleService = ({ strapi: strapi2 }) => ({
|
|
2895
|
+
async getLocales() {
|
|
2896
|
+
const locales = await strapi2.plugin("i18n").service("locales").find();
|
|
2897
|
+
return locales.map((locale) => ({ code: locale.code, name: locale.name }));
|
|
2898
|
+
},
|
|
2899
|
+
async getCredentials() {
|
|
2900
|
+
const settingService = strapi2.plugin(PLUGIN_ID).service("SettingAppService");
|
|
2901
|
+
const aiEnabled = await settingService.getRawValue(AI_TRANSLATION_MODULE, "aiEnabled");
|
|
2902
|
+
if (aiEnabled !== "true") return null;
|
|
2903
|
+
const apiKey = await settingService.getRawValue(AI_TRANSLATION_MODULE, "aiApiKey");
|
|
2904
|
+
if (!isFilled(apiKey)) return null;
|
|
2905
|
+
const storedModel = await settingService.getRawValue(AI_TRANSLATION_MODULE, "aiModel");
|
|
2906
|
+
return {
|
|
2907
|
+
apiKey,
|
|
2908
|
+
model: isFilled(storedModel) ? storedModel : AI_TRANSLATION_DEFAULT_MODEL
|
|
2909
|
+
};
|
|
2910
|
+
},
|
|
2911
|
+
async translateStrings(credentials, source, target, strings) {
|
|
2912
|
+
const translations = {};
|
|
2913
|
+
const errors = [];
|
|
2914
|
+
let failed = 0;
|
|
2915
|
+
const systemPrompt = buildSystemPrompt(source, target);
|
|
2916
|
+
const keys = Object.keys(strings).filter((key) => isFilled(strings[key]));
|
|
2917
|
+
for (const batch of chunk(keys, AI_TRANSLATION_BATCH_SIZE)) {
|
|
2918
|
+
const payload = batch.reduce((acc, key) => {
|
|
2919
|
+
acc[key] = strings[key];
|
|
2920
|
+
return acc;
|
|
2921
|
+
}, {});
|
|
2922
|
+
let result;
|
|
2923
|
+
try {
|
|
2924
|
+
result = await requestOpenAi(credentials.apiKey, credentials.model, systemPrompt, payload);
|
|
2925
|
+
} catch (e) {
|
|
2926
|
+
failed += batch.length;
|
|
2927
|
+
errors.push(`${target.code}: ${e.message}`);
|
|
2928
|
+
strapi2.log.error(`[${PLUGIN_ID}] AI translation failed for ${target.code}: ${e.message}`);
|
|
2929
|
+
continue;
|
|
2930
|
+
}
|
|
2931
|
+
for (const key of batch) {
|
|
2932
|
+
if (isFilled(result[key])) {
|
|
2933
|
+
translations[key] = result[key];
|
|
2934
|
+
} else {
|
|
2935
|
+
failed += 1;
|
|
2936
|
+
}
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
return { translations, failed, errors };
|
|
2940
|
+
},
|
|
2941
|
+
async adminTranslate() {
|
|
2942
|
+
const ctx = strapi2.requestContext.get();
|
|
2943
|
+
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`);
|
|
2944
|
+
const { sourceLocale, targetLocales, overwrite } = ctx.request.body;
|
|
2945
|
+
const credentials = await this.getCredentials();
|
|
2946
|
+
if (!credentials) {
|
|
2947
|
+
return ctx.badRequest("AI translations are disabled or the OpenAI API key is not set");
|
|
2948
|
+
}
|
|
2949
|
+
if (!isFilled(sourceLocale)) {
|
|
2950
|
+
return ctx.badRequest("A source locale is required");
|
|
2951
|
+
}
|
|
2952
|
+
const targets = (targetLocales || []).filter(
|
|
2953
|
+
(locale) => isFilled(locale) && locale !== sourceLocale
|
|
2954
|
+
);
|
|
2955
|
+
if (targets.length === 0) {
|
|
2956
|
+
return ctx.badRequest("At least one target locale is required");
|
|
2957
|
+
}
|
|
2958
|
+
const locales = await this.getLocales();
|
|
2959
|
+
const source = locales.find((locale) => locale.code === sourceLocale);
|
|
2960
|
+
if (!source) {
|
|
2961
|
+
return ctx.badRequest(`The locale ${sourceLocale} is not configured in Strapi`);
|
|
2962
|
+
}
|
|
2963
|
+
const query = strapi2.db.query(config2.uuid.modules.translation);
|
|
2964
|
+
const documents = await query.findMany({ select: ["id", "uid", "translation", "locale"] });
|
|
2965
|
+
const entries = groupByUid(documents).filter(
|
|
2966
|
+
(entry) => isFilled(entry.translations[source.code])
|
|
2967
|
+
);
|
|
2968
|
+
if (entries.length === 0) {
|
|
2969
|
+
return { translated: 0, skipped: 0, failed: 0, errors: [] };
|
|
2970
|
+
}
|
|
2971
|
+
let translated = 0;
|
|
2972
|
+
let skipped = 0;
|
|
2973
|
+
let failed = 0;
|
|
2974
|
+
const errors = [];
|
|
2975
|
+
for (const targetCode of targets) {
|
|
2976
|
+
const target = locales.find((locale) => locale.code === targetCode);
|
|
2977
|
+
if (!target) {
|
|
2978
|
+
errors.push(`The locale ${targetCode} is not configured in Strapi`);
|
|
2979
|
+
continue;
|
|
2980
|
+
}
|
|
2981
|
+
const pending = entries.filter(
|
|
2982
|
+
(entry) => overwrite || !isFilled(entry.translations[target.code])
|
|
2983
|
+
);
|
|
2984
|
+
skipped += entries.length - pending.length;
|
|
2985
|
+
if (pending.length === 0) continue;
|
|
2986
|
+
const strings = pending.reduce((acc, entry) => {
|
|
2987
|
+
acc[entry.uid] = entry.translations[source.code];
|
|
2988
|
+
return acc;
|
|
2989
|
+
}, {});
|
|
2990
|
+
const result = await this.translateStrings(credentials, source, target, strings);
|
|
2991
|
+
failed += result.failed;
|
|
2992
|
+
errors.push(...result.errors);
|
|
2993
|
+
for (const [uid, translation] of Object.entries(result.translations)) {
|
|
2994
|
+
const existing = await query.findOne({ where: { uid, locale: target.code } });
|
|
2995
|
+
if (existing) {
|
|
2996
|
+
await query.update({ where: { id: existing.id }, data: { translation } });
|
|
2997
|
+
} else {
|
|
2998
|
+
await query.create({ data: { uid, translation, locale: target.code } });
|
|
2999
|
+
}
|
|
3000
|
+
const entry = entries.find((item) => item.uid === uid);
|
|
3001
|
+
if (entry) entry.translations[target.code] = translation;
|
|
3002
|
+
translated += 1;
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
return { translated, skipped, failed, errors };
|
|
3006
|
+
},
|
|
3007
|
+
async adminTranslateStrings() {
|
|
3008
|
+
const ctx = strapi2.requestContext.get();
|
|
3009
|
+
const { sourceLocale, targetLocale, strings } = ctx.request.body;
|
|
3010
|
+
const credentials = await this.getCredentials();
|
|
3011
|
+
if (!credentials) {
|
|
3012
|
+
return ctx.badRequest("AI translations are disabled or the OpenAI API key is not set");
|
|
3013
|
+
}
|
|
3014
|
+
if (!isFilled(sourceLocale) || !isFilled(targetLocale)) {
|
|
3015
|
+
return ctx.badRequest("A source locale and a target locale are required");
|
|
3016
|
+
}
|
|
3017
|
+
if (sourceLocale === targetLocale) {
|
|
3018
|
+
return ctx.badRequest("The source locale and the target locale must be different");
|
|
3019
|
+
}
|
|
3020
|
+
if (!strings || Object.keys(strings).length === 0) {
|
|
3021
|
+
return { translations: {}, failed: 0, errors: [] };
|
|
3022
|
+
}
|
|
3023
|
+
const locales = await this.getLocales();
|
|
3024
|
+
const source = locales.find((locale) => locale.code === sourceLocale);
|
|
3025
|
+
const target = locales.find((locale) => locale.code === targetLocale);
|
|
3026
|
+
if (!source) return ctx.badRequest(`The locale ${sourceLocale} is not configured in Strapi`);
|
|
3027
|
+
if (!target) return ctx.badRequest(`The locale ${targetLocale} is not configured in Strapi`);
|
|
3028
|
+
return await this.translateStrings(credentials, source, target, strings);
|
|
2723
3029
|
}
|
|
2724
3030
|
});
|
|
2725
3031
|
const services = {
|
|
@@ -2729,7 +3035,8 @@ const services = {
|
|
|
2729
3035
|
TemplateModuleService,
|
|
2730
3036
|
MenuModuleService,
|
|
2731
3037
|
ParameterModuleService,
|
|
2732
|
-
TranslationModuleService
|
|
3038
|
+
TranslationModuleService,
|
|
3039
|
+
AiTranslationModuleService
|
|
2733
3040
|
};
|
|
2734
3041
|
const index = {
|
|
2735
3042
|
register,
|
|
@@ -2,6 +2,10 @@ import { UID } from "@strapi/strapi";
|
|
|
2
2
|
export declare const PLUGIN_ID = "littlebox-strapi-suite";
|
|
3
3
|
export declare const SLUG_LANGUAGE_STRATEGY = "language";
|
|
4
4
|
export declare const SLUG_CONTENT_STRATEGY = "content";
|
|
5
|
+
export declare const AI_TRANSLATION_MODULE = "translation";
|
|
6
|
+
export declare const AI_TRANSLATION_DEFAULT_MODEL = "gpt-4o";
|
|
7
|
+
export declare const AI_TRANSLATION_BATCH_SIZE = 40;
|
|
8
|
+
export declare const OPENAI_CHAT_COMPLETIONS_URL = "https://api.openai.com/v1/chat/completions";
|
|
5
9
|
export interface LtbConfigs {
|
|
6
10
|
pluginId: string;
|
|
7
11
|
uuid: {
|
|
@@ -56,6 +56,7 @@ declare const _default: {
|
|
|
56
56
|
adminCreate(): Promise<any>;
|
|
57
57
|
adminEdit(): Promise<any>;
|
|
58
58
|
adminBulkDelete(): Promise<any>;
|
|
59
|
+
adminAiTranslate(): Promise<any>;
|
|
59
60
|
getTranslations(): Promise<any>;
|
|
60
61
|
};
|
|
61
62
|
LocalesModuleController: ({ strapi }: {
|
|
@@ -66,5 +67,10 @@ declare const _default: {
|
|
|
66
67
|
default: any;
|
|
67
68
|
}>;
|
|
68
69
|
};
|
|
70
|
+
AiTranslationModuleController: ({ strapi }: {
|
|
71
|
+
strapi: import("@strapi/types/dist/core").Strapi;
|
|
72
|
+
}) => {
|
|
73
|
+
adminTranslateStrings(): Promise<any>;
|
|
74
|
+
};
|
|
69
75
|
};
|
|
70
76
|
export default _default;
|
|
@@ -6,6 +6,7 @@ declare const TranslationModuleController: ({ strapi }: {
|
|
|
6
6
|
adminCreate(): Promise<any>;
|
|
7
7
|
adminEdit(): Promise<any>;
|
|
8
8
|
adminBulkDelete(): Promise<any>;
|
|
9
|
+
adminAiTranslate(): Promise<any>;
|
|
9
10
|
getTranslations(): Promise<any>;
|
|
10
11
|
};
|
|
11
12
|
export default TranslationModuleController;
|
|
@@ -97,6 +97,7 @@ declare const _default: {
|
|
|
97
97
|
adminCreate(): Promise<any>;
|
|
98
98
|
adminEdit(): Promise<any>;
|
|
99
99
|
adminBulkDelete(): Promise<any>;
|
|
100
|
+
adminAiTranslate(): Promise<any>;
|
|
100
101
|
getTranslations(): Promise<any>;
|
|
101
102
|
};
|
|
102
103
|
LocalesModuleController: ({ strapi }: {
|
|
@@ -107,6 +108,11 @@ declare const _default: {
|
|
|
107
108
|
default: any;
|
|
108
109
|
}>;
|
|
109
110
|
};
|
|
111
|
+
AiTranslationModuleController: ({ strapi }: {
|
|
112
|
+
strapi: import("@strapi/types/dist/core").Strapi;
|
|
113
|
+
}) => {
|
|
114
|
+
adminTranslateStrings(): Promise<any>;
|
|
115
|
+
};
|
|
110
116
|
};
|
|
111
117
|
routes: {
|
|
112
118
|
admin: {
|
|
@@ -142,7 +148,8 @@ declare const _default: {
|
|
|
142
148
|
strapi: import("@strapi/types/dist/core").Strapi;
|
|
143
149
|
}) => {
|
|
144
150
|
getAll(): Promise<any>;
|
|
145
|
-
|
|
151
|
+
getRawValue(module: string, property: string): Promise<any>;
|
|
152
|
+
update(ctx: import("koa").Context): Promise<any>;
|
|
146
153
|
};
|
|
147
154
|
SlugModuleService: ({ strapi }: {
|
|
148
155
|
strapi: import("@strapi/types/dist/core").Strapi;
|
|
@@ -203,6 +210,20 @@ declare const _default: {
|
|
|
203
210
|
adminEdit(): Promise<void>;
|
|
204
211
|
getTranslations(): Promise<import("./services/modules/translation").TranslationItem[]>;
|
|
205
212
|
};
|
|
213
|
+
AiTranslationModuleService: ({ strapi }: {
|
|
214
|
+
strapi: import("@strapi/types/dist/core").Strapi;
|
|
215
|
+
}) => {
|
|
216
|
+
getLocales(): Promise<import("./services/modules/ai-translation").Locale[]>;
|
|
217
|
+
getCredentials(): Promise<import("./services/modules/ai-translation").Credentials>;
|
|
218
|
+
translateStrings(credentials: import("./services/modules/ai-translation").Credentials, source: import("./services/modules/ai-translation").Locale, target: import("./services/modules/ai-translation").Locale, strings: Record<string, string>): Promise<import("./services/modules/ai-translation").TranslateStringsResult>;
|
|
219
|
+
adminTranslate(): Promise<import("koa").Context | {
|
|
220
|
+
translated: number;
|
|
221
|
+
skipped: number;
|
|
222
|
+
failed: number;
|
|
223
|
+
errors: string[];
|
|
224
|
+
}>;
|
|
225
|
+
adminTranslateStrings(): Promise<import("koa").Context | import("./services/modules/ai-translation").TranslateStringsResult>;
|
|
226
|
+
};
|
|
206
227
|
};
|
|
207
228
|
contentTypes: {
|
|
208
229
|
"ltb-st-app-setting": {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { Core } from '@strapi/strapi';
|
|
2
2
|
import { Context } from 'koa';
|
|
3
|
+
export declare function maskSecret(value: string | null): string | null;
|
|
3
4
|
declare const SettingAppService: ({ strapi }: {
|
|
4
5
|
strapi: Core.Strapi;
|
|
5
6
|
}) => {
|
|
6
7
|
getAll(): Promise<any>;
|
|
7
|
-
|
|
8
|
+
getRawValue(module: string, property: string): Promise<any>;
|
|
9
|
+
update(ctx: Context): Promise<any>;
|
|
8
10
|
};
|
|
9
11
|
export default SettingAppService;
|