<!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>[12510] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions: Translate: Add suggestions from OpenAI and DeepL</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/12510">12510</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/12510","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>2023-03-29 14:12:17 +0000 (Wed, 29 Mar 2023)</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'>Translate: Add suggestions from OpenAI and DeepL</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionscsstranslationsuggestionscss">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/css/translation-suggestions.css</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclasstranslationmemoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsjstranslationsuggestionsjs">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionstemplatestranslationmemorysuggestionsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionscsstranslationsuggestionscss"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/css/translation-suggestions.css</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-translation-suggestions/css/translation-suggestions.css 2023-03-29 10:42:28 UTC (rev 12509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/css/translation-suggestions.css 2023-03-29 14:12:17 UTC (rev 12510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -42,6 +42,32 @@
</span><span class="cx" style="display: block; padding: 0 10px"> width: 3em;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+.openai-suggestion__score {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ margin-right: 10px;
+ border-radius: 100%;
+ background: #F2F0F7;
+ color: #4E426C;
+ font-size: 12px;
+ height: 3em;
+ width: 5em;
+}
+
+.deepl-suggestion__score {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ margin-right: 10px;
+ border-radius: 100%;
+ background: #F2F0F7;
+ color: #4E426C;
+ font-size: 12px;
+ height: 3em;
+ width: 4em;
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> .translation-suggestion__translation {
</span><span class="cx" style="display: block; padding: 0 10px"> flex: 1;
</span><span class="cx" style="display: block; padding: 0 10px"> min-width: 1%;
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincclasspluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/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-translation-suggestions/inc/class-plugin.php 2023-03-29 10:42:28 UTC (rev 12509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php 2023-03-29 14:12:17 UTC (rev 12510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -98,6 +98,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> GP::$router->prepend( "/$set/-get-tm-suggestions", [ __NAMESPACE__ . '\Routes\Translation_Memory', 'get_suggestions' ] );
</span><span class="cx" style="display: block; padding: 0 10px"> GP::$router->prepend( "/$set/-get-other-language-suggestions", [ __NAMESPACE__ . '\Routes\Other_Languages', 'get_suggestions' ] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ GP::$router->prepend( "/-save-external-suggestions", [ __NAMESPACE__ . '\Routes\Translation_Memory', 'update_external_translations' ], 'post' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclasstranslationmemoryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/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-translation-suggestions/inc/routes/class-translation-memory.php 2023-03-29 10:42:28 UTC (rev 12509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php 2023-03-29 14:12:17 UTC (rev 12510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3,6 +3,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> namespace WordPressdotorg\GlotPress\TranslationSuggestions\Routes;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> use GP;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+use GP_Locales;
</ins><span class="cx" style="display: block; padding: 0 10px"> use GP_Route;
</span><span class="cx" style="display: block; padding: 0 10px"> use WordPressdotorg\GlotPress\TranslationSuggestions\Translation_Memory_Client;
</span><span class="cx" style="display: block; padding: 0 10px"> use const WordPressdotorg\GlotPress\TranslationSuggestions\PLUGIN_DIR;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,8 +11,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class Translation_Memory extends GP_Route {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_suggestions( $project_path, $locale_slug, $set_slug ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $original_id = gp_get( 'original' );
- $nonce = gp_get( 'nonce' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $original_id = gp_get( 'original' );
+ $translation_id = gp_get( 'translation', 0 );
+ $nonce = gp_get( 'nonce' );
+ $gp_default_sort = get_user_option( 'gp_default_sort' );
+ $external_services_exclude_some_status = gp_array_get( $gp_default_sort, 'external_services_exclude_some_status', 0 );
+ $translation = null;
+ $openai_suggestions = array();
+ $deepl_suggestions = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_send_json_error( 'invalid_nonce' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,13 +38,359 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $locale .= '_' . $set_slug;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $suggestions = Translation_Memory_Client::query( $original->singular, $locale );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $suggestions = Translation_Memory_Client::query( $original->singular, $locale );
+ $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 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $external_services_exclude_some_status ) {
+ if ( $translation_id > 0 ) {
+ $translation = GP::$translation->get( $translation_id );
+ }
+ if ( ! $translation || ( 'current' != $translation->status && 'rejected' != $translation->status && 'old' != $translation->status ) ) {
+ $openai_suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
+ $deepl_suggestions = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
+ }
+ } else {
+ $openai_suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
+ $deepl_suggestions = $this->get_deepl_suggestion( $original->singular, $locale_slug, $set_slug );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $suggestions ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_send_json_error( $suggestions->get_error_code() );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_send_json_success( gp_tmpl_get_output( 'translation-memory-suggestions', [ 'suggestions' => $suggestions ], PLUGIN_DIR . '/templates/' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_send_json_success( gp_tmpl_get_output( 'translation-memory-suggestions', compact( 'suggestions', 'openai_suggestions', 'deepl_suggestions' ), PLUGIN_DIR . '/templates/' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Get suggestions from OpenAI (ChatGPT).
+ *
+ * @param string $original_singular The singular from the original string.
+ * @param string $locale The locale.
+ * @param \GP_Glossary $locale_glossary The glossary for the locale.
+ *
+ * @return array
+ */
+ private function get_openai_suggestion( $original_singular, $locale, $locale_glossary ): array {
+ $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 array();
+ }
+ $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 .= '.';
+ }
+
+ $gp_locale = GP_Locales::by_field( 'slug', $locale );
+ $openai_query .= ' Translate the following text to ' . $gp_locale->english_name . ": \n";
+ $openai_query .= '"' . $original_singular . '"';
+
+ $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' => 'gpt-3.5-turbo',
+ 'max_tokens' => 1000,
+ 'n' => 1,
+ 'messages' => $messages,
+ 'temperature' => $openai_temperature,
+ )
+ ),
+ )
+ );
+ if ( is_wp_error( $openai_response ) ) {
+ return array();
+ }
+ $response_status = wp_remote_retrieve_response_code( $openai_response );
+ if ( 200 !== $response_status ) {
+ return array();
+ }
+ $output = json_decode( wp_remote_retrieve_body( $openai_response ), true );
+ $this->update_openai_tokens_used( $output['usage']['total_tokens'] );
+
+ $message = $output['choices'][0]['message'];
+ $response['openai']['translation'] = trim( trim( $message['content'] ), '"' );
+ $response['openai']['diff'] = '';
+
+ return $response;
+ }
+
+ /**
+ * Updates the number of tokens used by OpenAI.
+ *
+ * @param int $tokens_used The number of tokens used.
+ */
+ private function update_openai_tokens_used( int $tokens_used ) {
+ $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 += $tokens_used;
+ $gp_external_translations['openai_tokens_used'] = $openai_tokens_used;
+ update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
+ }
+
+ /**
+ * Gets a translation suggestion from DeepL.
+ *
+ * @param string $original_singular The singular from the original string.
+ * @param string $locale The locale.
+ * @param string $set_slug The set slug.
+ *
+ * @return array
+ */
+ private function get_deepl_suggestion( string $original_singular, string $locale, string $set_slug ): array {
+ $free_url = 'https://api-free.deepl.com/v2/translate';
+ $gp_default_sort = get_user_option( 'gp_default_sort' );
+ $deepl_api_key = gp_array_get( $gp_default_sort, 'deepl_api_key' );
+ if ( empty( trim( $deepl_api_key ) ) ) {
+ return array();
+ }
+ $target_lang = $this->get_deepl_locale( $locale );
+ if ( empty( $target_lang ) ) {
+ return array();
+ }
+ $deepl_response = wp_remote_post(
+ $free_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, $set_slug ),
+ ),
+ ),
+ );
+ if ( is_wp_error( $deepl_response ) ) {
+ return array();
+ } else {
+ $body = wp_remote_retrieve_body( $deepl_response );
+ $response['deepl']['translation'] = json_decode( $body )->translations[0]->text;
+ $response['deepl']['diff'] = '';
+ $this->update_deepl_chars_used( $original_singular );
+ return $response;
+ }
+ }
+
+ /**
+ * Updates the number of characters used by DeepL.
+ *
+ * @param string $original_singular The singular from the original string.
+ */
+ private function update_deepl_chars_used( string $original_singular ) {
+ $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( $original_singular );
+ $gp_external_translations['deepl_chars_used'] = $deepl_chars_used;
+ update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
+ }
+
+ /**
+ * Gets the Deepl locale.
+ *
+ * @param string $locale The WordPress locale.
+ *
+ * @return string
+ */
+ private function get_deepl_locale( string $locale ): string {
+ $available_locales = array(
+ '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 external translations used by each user.
+ *
+ * @return void
+ */
+ public function update_external_translations() {
+ if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'wporg-editor-settings' ) ) {
+ wp_send_json_error( array( 'message' => esc_html__( 'Invalid nonce.', 'glotpress' ) ), 403 );
+ }
+ if ( ! isset( $_POST['translation'] ) ) {
+ wp_send_json_error( array( 'message' => esc_html__( 'Translation parameter is not present.', 'glotpress' ) ), 400 );
+ }
+ if ( ! isset( $_POST['openAITranslationsUsed'] ) && ! isset( $_POST['deeplTranslationsUsed'] ) ) {
+ wp_send_json_error( array( 'message' => esc_html__( 'Translation suggested parameter is not present.', 'glotpress' ) ), 400 );
+ }
+ if ( isset( $_POST['openAITranslationsUsed'] ) ) {
+ $this->update_one_external_translation(
+ $_POST['translation'],
+ $_POST['openAITranslationsUsed'],
+ 'openai_translations_used',
+ 'openai_same_translations_used'
+ );
+ }
+ if ( isset( $_POST['deeplTranslationsUsed'] ) ) {
+ $this->update_one_external_translation(
+ $_POST['translation'],
+ $_POST['deeplTranslationsUsed'],
+ 'deepl_translations_used',
+ 'deepl_same_translations_used'
+ );
+ }
+ wp_send_json_success();
+ }
+
+ /**
+ * Updates an external translation used by each user.
+ *
+ * @param string $translation The translation.
+ * @param string $suggestion The suggestion.
+ * @param string $external_translations_used The external translations used.
+ * @param string $external_same_translations_used The external same translations used.
+ *
+ * @return void
+ */
+ private function update_one_external_translation( string $translation, string $suggestion, string $external_translations_used, string $external_same_translations_used ) {
+ $sameTranslationUsed = $translation == $suggestion;
+ $gp_external_translations = get_user_option( 'gp_external_translations' );
+ $translations_used = gp_array_get( $gp_external_translations, $external_translations_used, 0 );
+ $same_translations_used = gp_array_get( $gp_external_translations, $external_same_translations_used, 0 );
+ if ( ! is_int( $translations_used ) || $translations_used < 0 ) {
+ $translations_used = 0;
+ }
+ $translations_used++;
+ $gp_external_translations[ $external_translations_used ] = $translations_used;
+ if ( $sameTranslationUsed ) {
+ if ( ! is_int( $same_translations_used ) || $same_translations_used < 0 ) {
+ $same_translations_used = 0;
+ }
+ $same_translations_used++;
+ $gp_external_translations[ $external_same_translations_used ] = $same_translations_used;
+ }
+ update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsjstranslationsuggestionsjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js</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-translation-suggestions/js/translation-suggestions.js 2023-03-29 10:42:28 UTC (rev 12509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js 2023-03-29 14:12:17 UTC (rev 12510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,9 +1,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ( function( $ ){
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function fetchSuggestions( $container, apiUrl, originalId, nonce ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function fetchSuggestions( $container, apiUrl, originalId, translationId, nonce ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> var xhr = $.ajax( {
</span><span class="cx" style="display: block; padding: 0 10px"> url: apiUrl,
</span><span class="cx" style="display: block; padding: 0 10px"> data: {
</span><span class="cx" style="display: block; padding: 0 10px"> 'original': originalId,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'translation': translationId,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'nonce': nonce
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px"> dataType: 'json',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -44,9 +45,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $container.addClass( 'fetching' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> var originalId = $gp.editor.current.original_id;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ var translationId = $gp.editor.current.translation_id;
</ins><span class="cx" style="display: block; padding: 0 10px"> var nonce = $container.data( 'nonce' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- fetchSuggestions( $container, window.WPORG_TRANSLATION_MEMORY_API_URL, originalId, nonce );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ fetchSuggestions( $container, window.WPORG_TRANSLATION_MEMORY_API_URL, originalId, translationId, nonce );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> function maybeFetchOtherLanguageSuggestions() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -62,9 +64,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $container.addClass( 'fetching' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> var originalId = $gp.editor.current.original_id;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ var translationId = $gp.editor.current.translation_id;
</ins><span class="cx" style="display: block; padding: 0 10px"> var nonce = $container.data( 'nonce' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- fetchSuggestions( $container, window.WPORG_OTHER_LANGUAGES_API_URL, originalId , nonce );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ fetchSuggestions( $container, window.WPORG_OTHER_LANGUAGES_API_URL, originalId , translationId, nonce );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> function copySuggestion( event ) {
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionstemplatestranslationmemorysuggestionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.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-translation-suggestions/templates/translation-memory-suggestions.php 2023-03-29 10:42:28 UTC (rev 12509)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.php 2023-03-29 14:12:17 UTC (rev 12510)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,6 +1,5 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-if ( empty( $suggestions ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( empty( $suggestions ) && empty( $openai_suggestions ) && empty( $deepl_suggestions ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> echo '<p class="no-suggestions">No suggestions.</p>';
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> echo '<ul class="suggestions-list">';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23,5 +22,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> echo '</div>';
</span><span class="cx" style="display: block; padding: 0 10px"> echo '</li>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $openai_suggestions as $suggestion ) {
+ echo '<li>';
+ echo '<div class="translation-suggestion with-tooltip openai" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
+ echo '<span class="openai-suggestion__score">OpenAI</span>';
+
+ echo '<span class="translation-suggestion__translation">';
+ echo esc_translation( $suggestion['translation'] );
+
+ if ( $suggestion['diff'] ) {
+ echo '<span class="translation-suggestion__original-diff">' . wp_kses_post( $suggestion['diff'] ) . '</span>';
+ }
+ echo '</span>';
+
+ echo '<span aria-hidden="true" class="translation-suggestion__translation-raw">' . esc_translation( $suggestion['translation'] ) . '</span>';
+
+ echo '<button type="button" class="button is-small copy-suggestion">Copy</button>';
+ echo '</div>';
+ echo '</li>';
+ }
+ foreach ( $deepl_suggestions as $suggestion ) {
+ echo '<li>';
+ echo '<div class="translation-suggestion with-tooltip deepl" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
+ echo '<span class="deepl-suggestion__score">DeepL</span>';
+
+ echo '<span class="translation-suggestion__translation">';
+ echo esc_translation( $suggestion['translation'] );
+
+ if ( $suggestion['diff'] ) {
+ echo '<span class="translation-suggestion__original-diff">' . wp_kses_post( $suggestion['diff'] ) . '</span>';
+ }
+ echo '</span>';
+
+ echo '<span aria-hidden="true" class="translation-suggestion__translation-raw">' . esc_translation( $suggestion['translation'] ) . '</span>';
+
+ echo '<button type="button" class="button is-small copy-suggestion">Copy</button>';
+ echo '</div>';
+ echo '</li>';
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> echo '</ul>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>