<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14205] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations: Release the wporg-gp-bulk-pretranslations plugin</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/14205">14205</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/14205","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>amieiro</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-11-26 11:06:04 +0000 (Tue, 26 Nov 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Release the wporg-gp-bulk-pretranslations plugin</pre>
<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsREADMEmd">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/README.md</a></li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclassdeeplphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-deepl.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclassopenaiphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-openai.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-plugin.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclasspretranslationphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-pretranslation.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclasstranslationmemoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-translation-memory.php</a></li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/</li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/wordpressdotorg/</li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/wordpressdotorg/autoload/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsvendorwordpressdotorgautoloadclassautoloaderphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/wordpressdotorg/autoload/class-autoloader.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationswporggpbulkpretranslationsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/wporg-gp-bulk-pretranslations.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsREADMEmd"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/README.md</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/README.md (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/README.md 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,22 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+# WPORG GP Bulk pre-translations
+
+This plugin, designed for https://translate.wordpress.org/, adds the ability to use internal (translation memory) and
+external systems (currently, OpenAI and DeepL) to pre-translate the strings of a project.
+
+It adds a some new options to the bulk select
+
+![image](https://github.com/amieiro/wporg-gp-bulk-pretranslations/assets/1667814/dd8306b0-e325-4bbc-9ede-0d38cb685222)
+
+so the translator is able to select the strings she wants to pretranslate and the system used:
+- Translation memory.
+- OpenAI.
+- DeepL.
+
+![image](https://github.com/amieiro/wporg-gp-bulk-pretranslations/assets/1667814/dd0c969e-8ac3-4e91-8138-d49b9e941a65)
+
+After using this functionality, the user obtains a bar with the number of pre-translations added.
+
+![image](https://github.com/amieiro/wporg-gp-bulk-pretranslations/assets/1667814/338ff187-e320-49b2-9d72-68623823f8fb)
+
+
+This functionality is only available to GTE.
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclassdeeplphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-deepl.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-deepl.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-deepl.php 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,193 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * This file contains the Deepl pre-translation class.
+ *
+ * @package WordPressdotorg\GlotPress\Bulk_Pretranslations
+ * @author WordPress.org
+ * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License
+ * @link https://wordpress.org/
+ */
+
+namespace WordPressdotorg\GlotPress\Bulk_Pretranslations;
+
+use GP;
+use GP_Locale;
+use GP_Translation_Set;
+
+/**
+ * Deepl pre-translation class.
+ */
+class Deepl extends Pretranslation {
+
+ /**
+ * Gets the suggestion for the translation from Deepl.
+ *
+ * Only works for strings with no plural forms.
+ *
+ * @param int $original_id The original ID.
+ * @param GP_Locale $locale The locale.
+ * @param GP_Translation_Set $translation_set The translation set.
+ *
+ * @return false|string
+ */
+ public function get_suggestion_0( int $original_id, GP_Locale $locale, GP_Translation_Set $translation_set ) {
+ if ( ! $this->should_pretranslate( $original_id, $translation_set ) ) {
+ return false;
+ }
+ $original = GP::$original->get( $original_id );
+
+ $gp_default_sort = get_user_option( 'gp_default_sort' );
+ $deepl_api_key = gp_array_get( $gp_default_sort, 'deepl_api_key' );
+ $deepl_url_free = 'https://api-free.deepl.com/v2/translate';
+ $deepl_url_pro = 'https://api.deepl.com/v2/translate';
+ $deepl_url = gp_array_get( $gp_default_sort, 'deepl_use_api_pro', false ) ? $deepl_url_pro : $deepl_url_free;
+ if ( empty( trim( $deepl_api_key ) ) ) {
+ return false;
+ }
+ $target_lang = $this->get_deepl_locale( $locale->slug );
+ if ( empty( $target_lang ) ) {
+ return false;
+ }
+
+ $deepl_response = wp_remote_post(
+ $deepl_url,
+ array(
+ 'timeout' => 20,
+ 'body' => array(
+ 'auth_key' => $deepl_api_key,
+ 'text' => $original->singular,
+ 'source_lang' => 'EN',
+ 'target_lang' => $target_lang,
+ 'formality' => $this->get_language_formality( $target_lang, $locale->slug ),
+ ),
+ ),
+ );
+ if ( is_wp_error( $deepl_response ) ) {
+ return false;
+ }
+ $response_status = wp_remote_retrieve_response_code( $deepl_response );
+ if ( 200 !== $response_status ) {
+ return false;
+ }
+ $body = wp_remote_retrieve_body( $deepl_response );
+
+ return json_decode( $body )->translations[0]->text;
+ }
+
+ /**
+ * Gets the Deepl locale.
+ *
+ * List of available languages https://developers.deepl.com/docs/resources/supported-languages#target-languages
+ *
+ * @param string $locale The WordPress locale.
+ *
+ * @return string
+ */
+ public function get_deepl_locale( string $locale ): string {
+ $available_locales = array(
+ 'ar' => 'AR',
+ 'bg' => 'BG',
+ 'cs' => 'CS',
+ 'da' => 'DA',
+ 'de' => 'DE',
+ 'el' => 'EL',
+ 'en-gb' => 'EN-GB',
+ 'es' => 'ES',
+ 'et' => 'ET',
+ 'fi' => 'FI',
+ 'fr' => 'FR',
+ 'hu' => 'HU',
+ 'id' => 'ID',
+ 'it' => 'IT',
+ 'ja' => 'JA',
+ 'ko' => 'KO',
+ 'lt' => 'LT',
+ 'lv' => 'LV',
+ 'nb' => 'NB',
+ 'nl' => 'NL',
+ 'pl' => 'PL',
+ 'pt' => 'PT-PT',
+ 'pt-br' => 'PT-BR',
+ 'ro' => 'RO',
+ 'ru' => 'RU',
+ 'sk' => 'SK',
+ 'sl' => 'SL',
+ 'sv' => 'SV',
+ 'tr' => 'TR',
+ 'uk' => 'UK',
+ 'zh-cn' => 'ZH',
+ );
+ if ( array_key_exists( $locale, $available_locales ) ) {
+ return $available_locales[ $locale ];
+ }
+
+ return '';
+ }
+
+ /**
+ * Gets the formality of the language.
+ *
+ * @param string $locale The locale.
+ * @param string $set_slug The set slug.
+ *
+ * @return string
+ */
+ private function get_language_formality( string $locale, string $set_slug ): string {
+ $lang_informality = array(
+ 'BG' => 'prefer_more',
+ 'CS' => 'prefer_less',
+ 'DA' => 'prefer_less',
+ 'DE' => 'prefer_less',
+ 'EL' => 'prefer_more',
+ 'EN-GB' => 'prefer_less',
+ 'ES' => 'prefer_less',
+ 'ET' => 'prefer_less',
+ 'FI' => 'prefer_less',
+ 'FR' => 'prefer_more',
+ 'HU' => 'prefer_more',
+ 'ID' => 'prefer_more',
+ 'IT' => 'prefer_less',
+ 'JA' => 'prefer_more',
+ 'KO' => 'prefer_less',
+ 'LT' => 'prefer_more',
+ 'LV' => 'prefer_less',
+ 'NB' => 'prefer_less',
+ 'NL' => 'prefer_less',
+ 'PL' => 'prefer_less',
+ 'PT-BR' => 'prefer_less',
+ 'PT-PT' => 'prefer_more',
+ 'RO' => 'prefer_less',
+ 'RU' => 'prefer_more',
+ 'SK' => 'prefer_less',
+ 'SL' => 'prefer_less',
+ 'SV' => 'prefer_less',
+ 'TR' => 'prefer_less',
+ 'UK' => 'prefer_more',
+ 'ZH' => 'prefer_more',
+ );
+
+ if ( ( 'DE' === $locale || 'NL' === $locale ) && 'formal' === $set_slug ) {
+ return 'prefer_more';
+ }
+ if ( array_key_exists( $locale, $lang_informality ) ) {
+ return $lang_informality[ $locale ];
+ }
+
+ return 'default';
+ }
+
+ /**
+ * Updates the number of characters used by DeepL.
+ */
+ public function update_deepl_chars_used() {
+ $gp_external_translations = get_user_option( 'gp_external_translations' );
+ $deepl_chars_used = gp_array_get( $gp_external_translations, 'deepl_chars_used', 0 );
+ if ( ! is_int( $deepl_chars_used ) || $deepl_chars_used < 0 ) {
+ $deepl_chars_used = 0;
+ }
+ $deepl_chars_used += mb_strlen( $this->original->singular );
+ $gp_external_translations['deepl_chars_used'] = $deepl_chars_used;
+ update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
+ }
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-deepl.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclassopenaiphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-openai.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-openai.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-openai.php 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,150 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * This file contains the OpenAI pre-translation class.
+ *
+ * @package WordPressdotorg\GlotPress\Bulk_Pretranslations
+ * @author WordPress.org
+ * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License
+ * @link https://wordpress.org/
+ */
+
+namespace WordPressdotorg\GlotPress\Bulk_Pretranslations;
+
+use GP;
+use GP_Locale;
+use GP_Translation_Set;
+
+/**
+ * OpenAI pre-translation class.
+ */
+class OpenAI extends Pretranslation {
+
+ /**
+ * Tokens used by OpenAI.
+ *
+ * @var ?int
+ */
+ protected ?int $tokens_used = null;
+
+ /**
+ * Gets the suggestion for the translation from OpenAI.
+ *
+ * Only works for strings with no plural forms.
+ *
+ * @param int $original_id The original ID.
+ * @param GP_Locale $locale The locale.
+ * @param GP_Translation_Set $translation_set The translation set.
+ *
+ * @return false|string
+ */
+ public function get_suggestion_0( int $original_id, GP_Locale $locale, GP_Translation_Set $translation_set ) {
+ if ( ! $this->should_pretranslate( $original_id, $translation_set ) ) {
+ return false;
+ }
+ $original = GP::$original->get( $original_id );
+
+ $current_set_slug = 'default';
+
+ $locale_glossary_translation_set = GP::$translation_set->by_project_id_slug_and_locale( 0, $current_set_slug, $locale->slug );
+ $locale_glossary = GP::$glossary->by_set_id( $locale_glossary_translation_set->id );
+
+ $openai_query = '';
+ $glossary_query = '';
+ $gp_default_sort = get_user_option( 'gp_default_sort' );
+ $openai_key = gp_array_get( $gp_default_sort, 'openai_api_key' );
+ if ( empty( trim( $openai_key ) ) ) {
+ return false;
+ }
+ $openai_prompt = gp_array_get( $gp_default_sort, 'openai_custom_prompt' );
+ $openai_temperature = gp_array_get( $gp_default_sort, 'openai_temperature', 0 );
+ if ( ! is_float( $openai_temperature ) || $openai_temperature < 0 || $openai_temperature > 2 ) {
+ $openai_temperature = 0;
+ }
+
+ $glossary_entries = array();
+ foreach ( $locale_glossary->get_entries() as $gp_glossary_entry ) {
+ if ( strpos( strtolower( $original->singular ), strtolower( $gp_glossary_entry->term ) ) !== false ) {
+ // Use the translation as key, because we could have multiple translations with the same term.
+ $glossary_entries[ $gp_glossary_entry->translation ] = $gp_glossary_entry->term;
+ }
+ }
+ if ( ! empty( $glossary_entries ) ) {
+ $glossary_query = ' The following terms are translated as follows: ';
+ foreach ( $glossary_entries as $translation => $term ) {
+ $glossary_query .= '"' . $term . '" is translated as "' . $translation . '"';
+ if ( array_key_last( $glossary_entries ) !== $translation ) {
+ $glossary_query .= ', ';
+ }
+ }
+ $glossary_query .= '.';
+ }
+
+ $openai_query .= ' Translate the following text to ' . $locale->english_name . ": \n";
+ $openai_query .= '"' . $original->singular . '"';
+ $openai_model = gp_array_get( $gp_default_sort, 'openai_model', 'gpt-3.5-turbo' );
+
+ $messages = array(
+ array(
+ 'role' => 'system',
+ 'content' => $openai_prompt . $glossary_query,
+ ),
+ array(
+ 'role' => 'user',
+ 'content' => $openai_query,
+ ),
+ );
+ $openai_response = wp_remote_post(
+ 'https://api.openai.com/v1/chat/completions',
+ array(
+ 'timeout' => 20,
+ 'headers' => array(
+ 'Content-Type' => 'application/json',
+ 'Authorization' => 'Bearer ' . $openai_key,
+ ),
+ 'body' => wp_json_encode(
+ array(
+ 'model' => $openai_model,
+ 'max_tokens' => 1000,
+ 'n' => 1,
+ 'messages' => $messages,
+ 'temperature' => $openai_temperature,
+ )
+ ),
+ )
+ );
+ if ( is_wp_error( $openai_response ) ) {
+ return false;
+ }
+ $response_status = wp_remote_retrieve_response_code( $openai_response );
+ if ( 200 !== $response_status ) {
+ return false;
+ }
+ $output = json_decode( wp_remote_retrieve_body( $openai_response ), true );
+ $message = $output['choices'][0]['message'];
+ $this->tokens_used = $output['usage']['total_tokens'];
+ $this->suggestion_0 = trim( trim( $message['content'] ), '"' );
+
+ return $this->suggestion_0;
+ }
+
+ /**
+ * Updates the number of tokens used by OpenAI.
+ *
+ * @return bool
+ */
+ public function update_openai_tokens_used(): bool {
+ if ( is_null( $this->tokens_used ) ) {
+ return false;
+ }
+ $gp_external_translations = get_user_option( 'gp_external_translations' );
+ $openai_tokens_used = gp_array_get( $gp_external_translations, 'openai_tokens_used' );
+ if ( ! is_int( $openai_tokens_used ) || $openai_tokens_used < 0 ) {
+ $openai_tokens_used = 0;
+ }
+ $openai_tokens_used += $this->tokens_used;
+ $gp_external_translations['openai_tokens_used'] = $openai_tokens_used;
+ update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
+
+ return true;
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-openai.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclasspluginphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-plugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-plugin.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-plugin.php 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,166 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * This file contains the main plugin file.
+ *
+ * @package WordPressdotorg\GlotPress\Bulk_Pretranslations
+ * @author WordPress.org
+ * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License
+ * @link https://wordpress.org/
+ */
+
+namespace WordPressdotorg\GlotPress\Bulk_Pretranslations;
+
+use GP;
+use GP_Locale;
+use GP_Project;
+use GP_Route;
+use GP_Translation;
+use GP_Translation_Set;
+
+/**
+ * Main plugin class.
+ */
+class Plugin extends GP_Route {
+
+ /**
+ * The instance of the class.
+ *
+ * @var Plugin
+ */
+ private static $instance = null;
+
+ /**
+ * Get the instance of the class.
+ *
+ * @return Plugin
+ */
+ public static function get_instance() {
+ if ( is_null( self::$instance ) ) {
+ self::$instance = new self();
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Plugin constructor.
+ */
+ public function __construct() {
+ parent::__construct();
+ add_action( 'gp_translation_set_bulk_action', array( $this, 'add_pretranslation_options' ) );
+ add_action( 'gp_translation_set_bulk_action_post', array( $this, 'store_pretanslations' ), 10, 4 );
+ }
+
+ /**
+ * Add pre-translation options to the bulk actions dropdown.
+ *
+ * It adds it only for the GTE and translation memory, OpenAI and DeepL.
+ *
+ * @param GP_Translation_Set $translation_set The translation set.
+ */
+ public function add_pretranslation_options( GP_Translation_Set $translation_set ):void {
+ $can_approve = $this->can( 'approve', 'translation-set', $translation_set->id );
+ if ( ! $can_approve ) {
+ return;
+ }
+
+ $gp_default_sort = get_user_option( 'gp_default_sort' );
+ $openai_key = gp_array_get( $gp_default_sort, 'openai_api_key', false );
+ $deepl_api_key = gp_array_get( $gp_default_sort, 'deepl_api_key', false );
+ $locale = $translation_set->locale;
+
+ echo '<optgroup label="Pre translate selected rows with">';
+ echo '<option value="bulk-pretranslation-tm">' . esc_html__( 'Translation Memory', 'glotpress' ) . '</option>';
+ if ( $openai_key ) {
+ echo '<option value="bulk-pretranslation-openai">' . esc_html__( 'OpenAI', 'glotpress' ) . '</option>';
+ }
+ $deepl = new DeepL();
+ if ( $deepl_api_key && $deepl->get_deepl_locale( $locale ) ) {
+ echo '<option value="bulk-pretranslation-deepl">' . esc_html__( 'DeepL', 'glotpress' ) . '</option>';
+ }
+ echo '</optgroup>';
+ }
+
+ /**
+ * Get the suggestions and store the pre-translations for the selected rows.
+ *
+ * @param GP_Project $project The project.
+ * @param GP_Locale $locale The locale.
+ * @param GP_Translation_Set $translation_set The translation set.
+ * @param array $bulk The bulk action data.
+ */
+ public function store_pretanslations( $project, $locale, $translation_set, $bulk ) {
+ $affected_actions = array( 'bulk-pretranslation-tm', 'bulk-pretranslation-openai', 'bulk-pretranslation-deepl' );
+ if ( ! in_array( $bulk['action'], $affected_actions, true ) ) {
+ return;
+ }
+
+ $can_approve = $this->can( 'approve', 'translation-set', $translation_set->id );
+ if ( ! $can_approve ) {
+ return;
+ }
+
+ $current_user_id = get_current_user_id();
+ $pretranslations_added = 0;
+ foreach ( $bulk['row-ids'] as $original_id ) {
+ $translation_0 = null;
+ if ( 'bulk-pretranslation-tm' === $bulk['action'] ) {
+ $tm = new Translation_Memory();
+ $translation_0 = $tm->get_suggestion_0( $original_id, $locale, $translation_set );
+ }
+ if ( 'bulk-pretranslation-openai' === $bulk['action'] ) {
+ $openai = new OpenAI();
+ $translation_0 = $openai->get_suggestion_0( $original_id, $locale, $translation_set );
+ $openai->update_openai_tokens_used();
+ }
+ if ( 'bulk-pretranslation-deepl' === $bulk['action'] ) {
+ $deepl = new DeepL();
+ $translation_0 = $deepl->get_suggestion_0( $original_id, $locale, $translation_set );
+ $deepl->update_deepl_chars_used();
+ }
+ if ( $translation_0 ) {
+ $translation_created = $this->store_pretranslation( $original_id, $translation_set->id, $translation_0, $current_user_id );
+ if ( $translation_created ) {
+ $pretranslations_added ++;
+ }
+ }
+ }
+ $this->set_notice( $pretranslations_added );
+ }
+
+ /**
+ * Store the pre-translation.
+ *
+ * @param int $original_id The original ID.
+ * @param int $translation_set_id The translation set ID.
+ * @param string $translation_0 The translation.
+ * @param int $current_user_id The current user ID.
+ *
+ * @return false|GP_Translation
+ */
+ private function store_pretranslation( int $original_id, int $translation_set_id, string $translation_0, int $current_user_id ) {
+ return GP::$translation->create(
+ array(
+ 'original_id' => $original_id,
+ 'translation_set_id' => $translation_set_id,
+ 'translation_0' => $translation_0,
+ 'status' => 'waiting',
+ 'user_id' => $current_user_id,
+ )
+ );
+ }
+
+ /**
+ * Set the notice with the number of pre-translations added.
+ *
+ * @param int $pretranslations_added The number of pre-translations added.
+ */
+ private function set_notice( int $pretranslations_added ):void {
+ $notice = sprintf(
+ /* translators: %s: Pretranslations count. */
+ _n( '%s pretranslation was added', '%s pretranslations were added', $pretranslations_added, 'glotpress' ),
+ $pretranslations_added
+ );
+ gp_notice_set( $notice );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-plugin.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclasspretranslationphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-pretranslation.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-pretranslation.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-pretranslation.php 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,81 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * This file contains the pre-translation class, which is the base class for all pre-translation classes.
+ *
+ * @package WordPressdotorg\GlotPress\Bulk_Pretranslations
+ * @author WordPress.org
+ * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License
+ * @link https://wordpress.org/
+ */
+
+namespace WordPressdotorg\GlotPress\Bulk_Pretranslations;
+
+use GP;
+use GP_Locale;
+use GP_Original;
+use GP_Translation_Set;
+
+/**
+ * Pre-translation class.
+ */
+abstract class Pretranslation {
+
+ /**
+ * The original object.
+ *
+ * @var GP_Original
+ */
+ protected ?GP_Original $original = null;
+
+ /**
+ * The suggestion for the translation (singular).
+ *
+ * @var string
+ */
+ protected string $suggestion_0 = '';
+
+ /**
+ * Gets the suggestion for the translation.
+ *
+ * Only works for strings with no plural forms.
+ *
+ * @param int $original_id The original ID.
+ * @param GP_Locale $locale The locale.
+ * @param GP_Translation_Set $translation_set The translation set.
+ *
+ * @return false|string
+ */
+ abstract public function get_suggestion_0( int $original_id, GP_Locale $locale, GP_Translation_Set $translation_set );
+
+ /**
+ * Check if a string should be pre-translated.
+ *
+ * @param int $original_id The original ID.
+ * @param GP_Translation_Set $translation_set The translation set.
+ *
+ * @return bool
+ */
+ protected function should_pretranslate( int $original_id, GP_Translation_Set $translation_set ): bool {
+ $this->original = GP::$original->get( $original_id );
+ if ( ! $this->original ) {
+ return false;
+ }
+ if ( ! is_null( $this->original->plural ) ) {
+ return false;
+ }
+
+ // We don't pre translate string with a current translation.
+ $translations = GP::$translation->find(
+ array(
+ 'original_id' => $original_id,
+ 'translation_set_id' => $translation_set->id,
+ 'status' => 'current',
+ )
+ );
+ if ( ! empty( $translations ) ) {
+ return false;
+ }
+
+ return true;
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-pretranslation.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsincclasstranslationmemoryphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-translation-memory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-translation-memory.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-translation-memory.php 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,58 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * This file contains the translation memory pre-translation class.
+ *
+ * @package WordPressdotorg\GlotPress\Bulk_Pretranslations
+ * @author WordPress.org
+ * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License
+ * @link https://wordpress.org/
+ */
+
+namespace WordPressdotorg\GlotPress\Bulk_Pretranslations;
+
+use GP_Locale;
+use GP_Translation_Set;
+use WordPressdotorg\GlotPress\TranslationSuggestions\Translation_Memory_Client;
+
+/**
+ * Translation memory pre-translation class.
+ */
+class Translation_Memory extends Pretranslation {
+
+ /**
+ * Similarity threshold for the translation memory.
+ * If the similarity score is below this threshold, the suggestion is not used.
+ * Value between 0 and 1.
+ *
+ * @var float
+ */
+ private $threshold = 1;
+
+ /**
+ * Gets the suggestion for the translation from the translation memory.
+ *
+ * Only works for strings with no plural forms.
+ *
+ * @param int $original_id The original ID.
+ * @param GP_Locale $locale The locale.
+ * @param GP_Translation_Set $translation_set The translation set.
+ *
+ * @return false|string
+ */
+ public function get_suggestion_0( int $original_id, GP_Locale $locale, GP_Translation_Set $translation_set ) {
+ if ( ! $this->should_pretranslate( $original_id, $translation_set ) ) {
+ return false;
+ }
+ $suggestions = Translation_Memory_Client::query( $this->original->singular, $this->original->plural, $locale->slug );
+ if ( empty( $suggestions ) ) {
+ return false;
+ }
+ if ( is_wp_error( $suggestions ) ) {
+ return false;
+ }
+ if ( $suggestions[0]['similarity_score'] < $this->threshold ) {
+ return false;
+ }
+ return $suggestions[0]['translation'];
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/inc/class-translation-memory.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationsvendorwordpressdotorgautoloadclassautoloaderphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/wordpressdotorg/autoload/class-autoloader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/wordpressdotorg/autoload/class-autoloader.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/wordpressdotorg/autoload/class-autoloader.php 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,100 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Autoloader for WordPress.org projects.
+ *
+ * @package WordPressdotorg\Autoload
+ */
+
+namespace WordPressdotorg\Autoload;
+
+/**
+ * An Autoloader which respects WordPress's filename standards.
+ *
+ * @package WordPressdotorg\Autoload
+ */
+class Autoloader {
+
+ /**
+ * Namespace separator.
+ */
+ const NS_SEPARATOR = '\\';
+
+ /**
+ * The prefix to compare classes against.
+ *
+ * @var string
+ * @access protected
+ */
+ protected $prefix;
+
+ /**
+ * Length of the prefix string.
+ *
+ * @var int
+ * @access protected
+ */
+ protected $prefix_length;
+
+ /**
+ * Path to the file to be loaded.
+ *
+ * @var string
+ * @access protected
+ */
+ protected $path;
+
+ /**
+ * Constructor.
+ *
+ * @param string $prefix Prefix all classes have in common.
+ * @param string $path Path to the files to be loaded.
+ */
+ public function __construct( $prefix, $path ) {
+ $this->prefix = $prefix;
+ $this->prefix_length = strlen( $prefix );
+ $this->path = trailingslashit( $path );
+ }
+
+ /**
+ * Loads a class if it starts with `$this->prefix`.
+ *
+ * @param string $class The class to be loaded.
+ */
+ public function load( $class ) {
+ if ( strpos( $class, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
+ return;
+ }
+
+ // Strip prefix from the start (ala PSR-4).
+ $class = substr( $class, $this->prefix_length + 1 );
+ $class = strtolower( $class );
+ $file = '';
+
+ // phpcs:ignore
+ if ( false !== ( $last_ns_pos = strripos( $class, self::NS_SEPARATOR ) ) ) {
+ $namespace = substr( $class, 0, $last_ns_pos );
+ $namespace = str_replace( '_', '-', $namespace );
+ $class = substr( $class, $last_ns_pos + 1 );
+ $file = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
+ }
+
+ $file .= 'class-' . str_replace( '_', '-', $class ) . '.php';
+
+ $path = $this->path . $file;
+
+ if ( file_exists( $path ) ) {
+ require $path;
+ }
+ }
+}
+
+/**
+ * Registers Autoloader's autoload function.
+ *
+ * @param string $prefix Prefix all classes have in common.
+ * @param string $path Path to the files to be loaded.
+ */
+function register_class_path( $prefix, $path ) {
+ $loader = new Autoloader( $prefix, $path );
+ spl_autoload_register( array( $loader, 'load' ) );
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/vendor/wordpressdotorg/autoload/class-autoloader.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpbulkpretranslationswporggpbulkpretranslationsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/wporg-gp-bulk-pretranslations.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/wporg-gp-bulk-pretranslations.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/wporg-gp-bulk-pretranslations.php 2024-11-26 11:06:04 UTC (rev 14205)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,29 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Plugin Name: WordPress.org bulk pre-translations
+ * Description: Pre-translate strings in GlotPress projects using internal and external tools.
+ * Version: 0.1.0
+ * Author: WordPress.org
+ * Author URI: https://wordpress.org/
+ * License: GPLv2 or later
+ * Text Domain: wporg-gp-bulk-pretranslations
+ *
+ * @package WordPressdotorg\GlotPress\Bulk_Pretranslations
+ */
+
+namespace WordPressdotorg\GlotPress\Bulk_Pretranslations;
+
+use WordPressdotorg\Autoload;
+
+// Store the root plugin file for usage with functions which use the plugin basename.
+define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ );
+
+if ( ! class_exists( '\WordPressdotorg\Autoload\Autoloader', false ) ) {
+ include __DIR__ . '/vendor/wordpressdotorg/autoload/class-autoloader.php';
+}
+
+// Register an Autoloader for all files.
+Autoload\register_class_path( __NAMESPACE__, __DIR__ . '/inc' );
+// Instantiate the Plugin.
+Plugin::get_instance();
+
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-bulk-pretranslations/wporg-gp-bulk-pretranslations.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>