Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended for DeepL #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions classes/translationproviders/deepltranslate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package filter_translations
* @author Andrew Hancox <[email protected]>
* @author Open Source Learning <[email protected]>
* @link https://opensourcelearning.co.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2021, Andrew Hancox
* @copyright 2023, Tina John <[email protected]>
*/

namespace filter_translations\translationproviders;

use admin_setting_configcheckbox;
use admin_setting_configtext;
use curl;
use filter_translations\translation;
use moodle_url;

/**
* Translation provider to fetch and then retain translations from Google translate.
*/
class deepltranslate extends translationprovider {
/**
* If deepl translate is enabled and configured return config, else return false.
*
* @return false|mixed|object|string|null
* @throws \dml_exception
*/
private static function config() {
static $config = null;

if (!isset($config)) {
$config = get_config('filter_translations');

if (!empty($config->deepl_backoffonerror) && $config->deepl_backoffonerror_time < time() - HOURSECS) {
$config->deepl_backoffonerror = false;
set_config('deepl_backoffonerror', false, 'filter_translations');
$cache = \filter_translations::cache();
$cache->purge();
}

if (empty($config->deepl_enable)
|| empty($config->deepl_apikey)
|| empty($config->deepl_apiendpoint)
|| !empty($config->deepl_backoffonerror)) {
$config = false;
}
}

return $config;
}

/**
* Get a piece of text translated into a specific language.
* The language of the source text is auto-detected by deepl.
*
* Either the translated text or if there is an error start backing off from the API and return null.
*
* @param $text
* @param $targetlanguage
* @return string|null
* @throws \dml_exception
* @throws \moodle_exception
*/
protected function generate_translation($text, $targetlanguage) {
$config = self::config();

if (empty($config)) {
return null;
}

global $CFG;
require_once($CFG->libdir . "/filelib.php");

$targetlanguage = str_replace('_wp', '', $targetlanguage);

// Look for any base64 encoded files, create an md5 of their content,
// use the md5 as a placeholder while we send the text to deepl translate.
$base64s = [];
if (strpos($text, 'base64') !== false) {
$text = preg_replace_callback(
'/(data:[^;]+\/[^;]+;base64)([^"]+)/i',
function ($m) use (&$base64s) {
$md5 = md5($m[2]);
$base64s[$md5] = $m[2];

return $m[1] . $md5;
},
$text
);
}


// Added tinjohn logic for deepl.
$authKey = $config->deepl_apikey;
//$authKey = 'for_debugging_off';
$apiUrl = $config->deepl_apiendpoint;

// Data to be translated and target language
$data = array(
'text' => array($text),
'target_lang' => $targetlanguage
);

// Convert data to JSON format
$dataJson = json_encode($data);

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: DeepL-Auth-Key ' . $authKey,
'Content-Type: application/json'
));

// Execute cURL session and get the response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
error_log("Error calling DeepL Translate: \n" . curl_error($ch));
$this->backoff();
return null;
// echo 'cURL Error: ' . curl_error($ch);
}

// Close cURL session
curl_close($ch);

// Decode the JSON response
$translatedData = json_decode($response, true);

// Output the translated text
if (isset($translatedData['translations'][0]['text'])) {
$text = $translatedData['translations'][0]['text'];
} else {
return null;
}

// Swap the base 64 encoded images back in.
foreach ($base64s as $md5 => $base64) {
$text = str_replace($md5, $base64, $text);
}

return $text;
}

/**
* Back off from API - used when errors are getting returned.
*
* @return void
*/
private function backoff() {
set_config('google_backoffonerror', true, 'filter_translations');
set_config('google_backoffonerror_time', time(), 'filter_translations');
}
}
36 changes: 29 additions & 7 deletions classes/translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
* @link https://opensourcelearning.co.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2021, Andrew Hancox
* @copyright 2023, Tina John <[email protected]>
*/

namespace filter_translations;

use cache;
use filter_translations\translationproviders\googletranslate;
use filter_translations\translationproviders\languagestringreverse;
// Added tinjohn for DeepL.
use filter_translations\translationproviders\deepltranslate;

/**
*
Expand All @@ -39,6 +42,8 @@ class translator {
public static $existingautotranslationsfound = 0;
public static $translationnotfound = 0;
public static $cachehit = 0;
// Added tinjohn for DeepL.
public static $deepltranslatefetches = 0;

/**
* Wrapper function to allow overriding in translator_testable.
Expand Down Expand Up @@ -98,13 +103,30 @@ public function get_best_translation($language, $generatedhash, $foundhash, $tex
self::$langstringlookupfetches++;
$translation = $languagestringtranslation;
} else {
// No dice... try google translate.
$google = new googletranslate();
$googletranslation = $google->createorupdate_translation($foundhash, $generatedhash, $text, $language, $translation);

if (!empty($googletranslation)) {
self::$googletranslatefetches++;
$translation = $googletranslation;
if (!isset($config)) {
$config = get_config('filter_translations');
}
// Added tinjohn for DeepL vs. Google.
if ($config->google_enable) {
// No dice... try google translate.
$google = new googletranslate();
$googletranslation = $google->createorupdate_translation($foundhash, $generatedhash, $text, $language, $translation);

if (!empty($googletranslation)) {
self::$googletranslatefetches++;
$translation = $googletranslation;
}
} else {
if ($config->deepl_enable) {
// No dice... try deepl translate.
$deepl = new deepltranslate();
$deepltranslation = $deepl->createorupdate_translation($foundhash, $generatedhash, $text, $language, $translation);

if (!empty($deepltranslation)) {
self::$deepltranslatefetches++;
$translation = $deepltranslation;
}
}
}
}
} else if (!empty($translation)) {
Expand Down
8 changes: 8 additions & 0 deletions lang/en/filter_translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @link https://opensourcelearning.co.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2021, Andrew Hancox
* @copyright 2023, Tina John <[email protected]>
*/

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -190,3 +191,10 @@
$string['untranslatedpages_desc'] = 'One per line.';
$string['url'] = 'Page';
$string['userid'] = 'User ID';
// Added tinjohn for DeepL.
$string['deepl_apiendpoint'] = 'API Endpoint';
$string['deepl_apikey'] = 'API key';
$string['deepl_backoffonerror'] = 'Back off from erroring API';
$string['deepl_enable'] = 'Use DeepL Translate API';
$string['deepltranslate'] = 'DeepL Translate';

20 changes: 19 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @link https://opensourcelearning.co.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2021, Andrew Hancox
* @copyright 2023, Tina John <[email protected]>
*/

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -109,4 +110,21 @@

$settings->add(new admin_setting_configtext('filter_translations/google_apikey',
get_string('google_apikey', 'filter_translations'), '', null, PARAM_RAW_TRIMMED, 40));
}

// Added tinjohn Deepl additions.
$settings->add(new admin_setting_heading('deepltranslateapi',
get_string('deepltranslate', 'filter_translations'), ''));

$settings->add(new admin_setting_configcheckbox('filter_translations/deepl_enable',
get_string('deepl_enable', 'filter_translations'), '', false));

$settings->add(new admin_setting_configcheckbox('filter_translations/deepl_backoffonerror',
get_string('deepl_backoffonerror', 'filter_translations'), '', false));

$settings->add(new admin_setting_configtext('filter_translations/deepl_apiendpoint',
get_string('deepl_apiendpoint', 'filter_translations'), '', 'https://api-free.deepl.com/v2/translate',
PARAM_URL));

$settings->add(new admin_setting_configtext('filter_translations/deepl_apikey',
get_string('deepl_apikey', 'filter_translations'), '', null, PARAM_RAW_TRIMMED, 40));
}
2 changes: 2 additions & 0 deletions templates/translationperfdata.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<dd>{{cachehit}}</dd>
<dt>Google translate fetches</dt>
<dd>{{googletranslatefetches}}</dd>
<dt>DeepL translate fetches</dt>
<dd>{{deepltranslatefetches}}</dd>
<dt>Language string lookup fetches</dt>
<dd>{{langstringlookupfetches}}</dd>
<dt>Existing manual translations found</dt>
Expand Down