Skip to content

Commit 857d1a1

Browse files
Release 1.2.8
1 parent 67bdf0f commit 857d1a1

File tree

20 files changed

+531
-135
lines changed

20 files changed

+531
-135
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 1.2.8 - 2025-06-12
6+
### Added
7+
- Support for DeepL glossaries.
8+
- Option to exclude entries from bulk translations.
9+
510
## 1.2.7 - 2025-06-10
611
### Added
712
- Added support for translating [Craft Commerce](https://craftcms.com/commerce) products, including product variant fields. [Issue #40](https://github.com/convergine/craft-content-buddy/issues/40)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "convergine/craft-content-buddy",
33
"description": "Content Buddy is an AI-driven CraftCMS plugin that leverages the ChatGPT API to automatically generate and manage multi-language content, including text and images, with customizable settings and features for precise control and enhanced content creation.",
44
"type": "craft-plugin",
5-
"version": "1.2.7",
5+
"version": "1.2.8",
66
"keywords": [
77
"craftcms",
88
"content-buddy",

src/BuddyPlugin.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use convergine\contentbuddy\services\Request;
1111
use convergine\contentbuddy\services\Translate;
1212
use convergine\contentbuddy\variables\BuddyVariable;
13+
use craft\events\ModelEvent;
1314
use Craft;
1415
use craft\base\Element;
1516
use craft\base\Field;
1617
use craft\base\Plugin;
1718
use craft\controllers\ElementsController;
19+
use craft\elements\Entry;
1820
use craft\events\DefineElementEditorHtmlEvent;
1921
use craft\events\DefineFieldHtmlEvent;
2022
use craft\events\DefineHtmlEvent;
@@ -39,7 +41,7 @@
3941
class BuddyPlugin extends Plugin {
4042
public static string $plugin;
4143
public ?string $name = 'Content Buddy';
42-
public string $schemaVersion = '1.2.7';
44+
public string $schemaVersion = '1.2.8';
4345

4446
public function init() {
4547
/* plugin initialization */
@@ -90,6 +92,8 @@ function (RegisterUrlRulesEvent $event) {
9092

9193
$event->rules['convergine-contentbuddy/site-translate'] = 'convergine-contentbuddy/translate/index';
9294
$event->rules['convergine-contentbuddy/site-translate/log'] = 'convergine-contentbuddy/translate/log';
95+
96+
$event->rules['convergine-contentbuddy/deepl/glossaries'] = 'convergine-contentbuddy/deep-l/glossaries';
9397
}
9498
);
9599
}
@@ -226,6 +230,34 @@ function (DefineHtmlEvent $event) {
226230

227231
} );
228232
}
233+
234+
Event::on(
235+
Entry::class,
236+
Entry::EVENT_DEFINE_SIDEBAR_HTML,
237+
static function(DefineHtmlEvent $event) {
238+
/** @var craft\models\Site $isDefaultSite */
239+
$isDefaultSite = $event->sender->getSite()->primary;
240+
241+
$html = Craft::$app->view->renderTemplate('convergine-contentbuddy/_sidebars/entry-preview.twig',[
242+
'element' => $event->sender,
243+
'isDefaultSite' => $isDefaultSite,
244+
'excludedSites' => BuddyPlugin::getInstance()->translate->getExcludedEntries($event->sender->id)
245+
]);
246+
247+
$event->html .= $html;
248+
}
249+
);
250+
251+
Event::on(
252+
Entry::class,
253+
Entry::EVENT_AFTER_PROPAGATE,
254+
function (ModelEvent $event) {
255+
if($event->sender->id) {
256+
$this->translate->saveExcludeBulkSites( $event->sender );
257+
}
258+
}
259+
);
260+
229261
}
230262

231263
protected function createSettingsModel(): SettingsModel {

src/api/TextApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct() {
1414
$this->settings = $settings;
1515
}
1616

17-
function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = '') {}
17+
function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = '', $source_lang = '') {}
1818

1919
protected function getMaxTokensForModel($model): int {
2020
return match($model) {

src/api/text/DeepL.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@
1010
use yii\helpers\StringHelper;
1111

1212
class DeepL extends TextApi {
13-
public function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = ''): string {
13+
public function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = '', $source_lang = ''): string {
1414
try {
1515
$translateData = $this->_getTranslatedData($prompt,$lang);
16-
$client = new Client();
17-
$sendData = [
18-
'text' => [$translateData['text']],
19-
"tag_handling"=>"html",
20-
"target_lang" => $translateData['lang'],
21-
];
16+
17+
$sendData = [
18+
'text' => [$translateData['text']],
19+
"tag_handling"=>"html",
20+
"target_lang" => $translateData['lang'],
21+
];
22+
23+
if(!empty($this->settings->deepLGlossaryId) && $source_lang !== 'skip') {
24+
$source_lang = explode("-",$source_lang)[0];
25+
$sendData['source_lang'] = $source_lang ?: 'EN';
26+
$sendData['glossary_id'] = $this->settings->deepLGlossaryId;
27+
}
28+
29+
$client = new Client();
2230

2331
Craft::info( "Translate with DeepL" , 'content-buddy' );
2432
Craft::info( StringHelper::truncateWords($prompt,20,'...',true), 'content-buddy' );
@@ -43,6 +51,12 @@ public function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = fa
4351
$message = $json['message'];
4452
Craft::info( 'DeepL ERROR', 'content-buddy' );
4553
Craft::info( $message, 'content-buddy' );
54+
55+
// if glossary dictionary is not found, we can try again without it
56+
if(str_contains($message, 'No dictionary found for language pair')) {
57+
return $this->sendRequest($prompt, $maxTokens, $temperature, $isTranslate, $instructions, $lang, 'skip');
58+
}
59+
4660
throw new Exception( $message );
4761
}
4862
} catch ( Throwable $e ) {
@@ -78,4 +92,34 @@ private function _getTranslatedData($prompt,$lang ):array {
7892
$text = $promptParts[1];
7993
return compact('lang','text');
8094
}
81-
}
95+
96+
public function getGlossaries() : array {
97+
$client = new Client();
98+
$res = $client->request('GET', 'https://api.deepl.com/v3/glossaries', [
99+
'headers' => [
100+
'Authorization' => 'DeepL-Auth-Key '.$this->settings->getDeepLApiKey(),
101+
'Content-Type' => 'application/json'
102+
],
103+
'http_errors'=>false
104+
]);
105+
$body = $res->getBody();
106+
$json = json_decode($body, true);
107+
if ($res->getStatusCode() == 403) {
108+
Craft::info('DeepL ERROR', 'content-buddy');
109+
throw new Exception('DeepL API key is invalid');
110+
} elseif (isset($json['message'])) {
111+
$message = $json['message'];
112+
Craft::info('DeepL ERROR', 'content-buddy');
113+
Craft::info($message, 'content-buddy');
114+
throw new Exception($message);
115+
}
116+
$glossaries = [];
117+
foreach ($json['glossaries'] as $glossary) {
118+
$glossaries[] = [
119+
'id' => $glossary['glossary_id'],
120+
'name' => $glossary['name'],
121+
];
122+
}
123+
return $glossaries;
124+
}
125+
}

src/api/text/OpenAi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use yii\helpers\StringHelper;
1111

1212
class OpenAi extends TextApi {
13-
public function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = ''): string {
13+
public function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = '', $source_lang = ''): string {
1414
try {
1515

1616
$model = $isTranslate?$this->settings->preferredTranslationModel:$this->settings->preferredModel;

src/api/text/XAi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use yii\helpers\StringHelper;
1010

1111
class XAi extends TextApi {
12-
public function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = ''): string {
12+
public function sendRequest($prompt, $maxTokens, $temperature, $isTranslate = false, $instructions = '', $lang = '', $source_lang = ''): string {
1313
try {
1414
$model = $this->settings->xAiModel;
1515

src/assets/dist/contentbuddy.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace convergine\contentbuddy\controllers;
3+
4+
use convergine\contentbuddy\api\text\DeepL;
5+
use convergine\contentbuddy\BuddyPlugin;
6+
use convergine\contentbuddy\models\SettingsModel;
7+
use Craft;
8+
use craft\web\Controller;
9+
10+
class DeepLController extends Controller {
11+
public function actionGlossaries() {
12+
//$this->requirePostRequest();
13+
try {
14+
$deepl = new DeepL();
15+
$glossaries = $deepl->getGlossaries();
16+
return $this->asJson(['success' => true, 'glossaries' => $glossaries]);
17+
} catch (\Exception $e) {
18+
return $this->asJson(['success' => false, 'error' => $e->getMessage()]);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)