<!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>[8732] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions: Translate: Add plugin to provide translation suggestions from translation memory and other languages.</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/8732">8732</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/8732","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-05-06 13:41:27 +0000 (Mon, 06 May 2019)</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 plugin to provide translation suggestions from translation memory and other languages.
Props yoavf, ocean90.
See <a href="http://meta.trac.wordpress.org/ticket/4430">#4430</a>.</pre>
<h3>Added Paths</h3>
<ul>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/</li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/css/</li>
<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>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/</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_htmlwpcontentpluginswporggptranslationsuggestionsincclasstranslationmemoryclientphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-translation-memory-client.php</a></li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclassotherlanguagesphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-other-languages.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>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/</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>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionstemplatesotherlanguagessuggestionsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/other-languages-suggestions.php</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>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/vendor/</li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/vendor/wordpressdotorg/</li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/vendor/wordpressdotorg/autoload/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsvendorwordpressdotorgautoloadclassautoloaderphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/vendor/wordpressdotorg/autoload/class-autoloader.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionswporggptranslationsuggestionsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/wporg-gp-translation-suggestions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionscsstranslationsuggestionscss"></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-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 (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/css/translation-suggestions.css 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,84 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+.suggestions-wrapper .suggestions-list {
+ list-style: none;
+ padding: 0;
+}
+
+.suggestions__other-languages .suggestions-list {
+ max-height: 400px;
+ overflow: auto;
+ -webkit-overflow-scrolling: touch;
+}
+
+.suggestions-list li + li {
+ margin-top: 10px;
+}
+
+.translation-suggestion {
+ display: flex;
+ align-items: center;
+ cursor: pointer;
+ -webkit-tap-highlight-color: transparent;
+ padding: 3px;
+}
+
+.copy-suggestion {
+ visibility: hidden;
+}
+
+.translation-suggestion:focus-within,
+.translation-suggestion:focus,
+.translation-suggestion:hover {
+ background: #F2F0F7;
+ outline: none;
+}
+
+.translation-suggestion:focus-within .copy-suggestion,
+.translation-suggestion:focus .copy-suggestion,
+.translation-suggestion:hover .copy-suggestion {
+ visibility: visible;
+}
+
+.translation-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: 3em;
+}
+
+.translation-suggestion__translation {
+ flex: 1;
+ color: #333;
+ white-space: pre-wrap;
+}
+
+.translation-suggestion__translation-raw {
+ display: none;
+}
+
+.translation-suggestion__original-diff,
+.translation-suggestion__translation-meta {
+ display: block;
+ color: #666;
+ font-size: 12px;
+}
+
+.translation-suggestion__original-diff del,
+.translation-suggestion__original-diff ins {
+ text-decoration: none;
+}
+
+.translation-suggestion__original-diff del {
+ color: #9A2323;
+ background: #F1ADAD;
+}
+
+.translation-suggestion__original-diff ins {
+ color: #31843F;
+ background: #B5E1B9;
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincclasspluginphp"></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-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 (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,122 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\TranslationSuggestions;
+
+use GP;
+use GP_Locales;
+use Text_Diff;
+use WP_Error;
+use WP_Http;
+use WP_Text_Diff_Renderer_inline;
+
+require_once ABSPATH . '/wp-includes/wp-diff.php' ;
+
+class Plugin {
+
+ /**
+ * @var Plugin The singleton instance.
+ */
+ private static $instance;
+
+ /**
+ * Returns always the same instance of this plugin.
+ *
+ * @return Plugin
+ */
+ public static function get_instance() {
+ if ( ! ( self::$instance instanceof Plugin ) ) {
+ self::$instance = new Plugin();
+ }
+ return self::$instance;
+ }
+
+ /**
+ * Instantiates a new Plugin object.
+ */
+ private function __construct() {
+ add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
+ }
+
+ /**
+ * Initializes the plugin.
+ */
+ public function plugins_loaded() {
+ add_action( 'template_redirect', [ $this, 'register_routes' ], 5 );
+ add_action( 'gp_pre_tmpl_load', [ $this, 'pre_tmpl_load' ], 10, 2 );
+ add_action( 'wporg_translate_suggestions', [ $this, 'extend_translation_suggestions' ] );
+ }
+
+ /**
+ * Registers custom routes.
+ */
+ public function register_routes() {
+ $dir = '([^_/][^/]*)';
+ $path = '(.+?)';
+ $projects = 'projects';
+ $project = $projects . '/' . $path;
+ $locale = '(' . implode( '|', wp_list_pluck( GP_Locales::locales(), 'slug' ) ) . ')';
+ $set = "$project/$locale/$dir";
+
+ GP::$router->prepend( "/$set/-get-tm-suggestions", [ __NAMESPACE__ . '\Routes\Translation_Memory', 'get_suggestions' ] );
+ GP::$router->prepend( "/$set/-get-other-language-suggestions", [ __NAMESPACE__ . '\Routes\Other_Languages', 'get_suggestions' ] );
+ }
+
+ /**
+ * Enqueue custom styles and scripts.
+ */
+ public function pre_tmpl_load( $template, $args ) {
+ if ( 'translations' !== $template || ! is_user_logged_in() ) {
+ return;
+ }
+
+ wp_register_style(
+ 'gp-translation-suggestions',
+ plugins_url( 'css/translation-suggestions.css', PLUGIN_FILE ),
+ [],
+ '20190506'
+ );
+ gp_enqueue_style( 'gp-translation-suggestions' );
+
+ wp_register_script(
+ 'gp-translation-suggestions',
+ plugins_url( './js/translation-suggestions.js', PLUGIN_FILE ),
+ [ 'gp-editor' ],
+ '20190506'
+ );
+
+ gp_enqueue_script( 'gp-translation-suggestions' );
+
+ wp_add_inline_script(
+ 'gp-translation-suggestions',
+ sprintf(
+ "window.WPORG_TRANSLATION_MEMORY_API_URL = %s;\nwindow.WPORG_OTHER_LANGUAGES_API_URL = %s;",
+ wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-suggestions' ) ) ),
+ wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-other-language-suggestions' ) ) )
+ )
+ );
+ }
+
+ /**
+ * Extends the suggestions container for Translation Memory and
+ * Other Languages.
+ *
+ * @param object $entry Current translation row entry.
+ */
+ public function extend_translation_suggestions( $entry ) {
+ if ( ! is_user_logged_in() ) {
+ return;
+ }
+
+ ?>
+ <details open class="suggestions__translation-memory" data-nonce="<?php echo esc_attr( wp_create_nonce( 'translation-memory-suggestions-' . $entry->original_id ) ); ?>">
+ <summary>Suggestions from Translation Memory</summary>
+ <p class="suggestions__loading-indicator">Loading <span aria-hidden="true" class="suggestions__loading-indicator__icon"><span></span><span></span><span></span></span></p>
+ </details>
+
+ <details class="suggestions__other-languages" data-nonce="<?php echo esc_attr( wp_create_nonce( 'other-languages-suggestions-' . $entry->original_id ) ); ?>">
+ <summary>Other Languages</summary>
+ <p class="suggestions__loading-indicator">Loading <span aria-hidden="true" class="suggestions__loading-indicator__icon"><span></span><span></span><span></span></span></p>
+ </details>
+ <?php
+ }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincclasstranslationmemoryclientphp"></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-translation-suggestions/inc/class-translation-memory-client.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-translation-memory-client.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-translation-memory-client.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,86 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\TranslationSuggestions;
+
+use Text_Diff;
+use WP_Error;
+use WP_Http;
+use WP_Text_Diff_Renderer_inline;
+
+require_once ABSPATH . '/wp-includes/wp-diff.php' ;
+
+class Translation_Memory_Client {
+
+ const API_ENDPOINT = 'https://translate.wordpress.com/api/tm/';
+
+ /**
+ * Queries translation memory for a string.
+ *
+ * @param string $text Text to search translations for.
+ * @param string $target_locale Locale to search in.
+ * @return array|\WP_Error List of suggestions on success, WP_Error on failure.
+ */
+ public static function query( string $text, string $target_locale ) {
+ if ( ! defined ( 'WPCOM_TM_TOKEN' ) ) {
+ return new WP_Error( 'no_token' );
+ }
+
+ $url = add_query_arg( urlencode_deep( [
+ 'text' => $text,
+ 'target' => $target_locale,
+ 'token' => WPCOM_TM_TOKEN,
+ ] ) , self::API_ENDPOINT );
+
+ $request = wp_remote_get(
+ $url,
+ [
+ 'timeout' => 2,
+ 'user-agent' => 'WordPress.org Translate',
+ ]
+ );
+
+ if ( is_wp_error( $request ) ) {
+ return $request;
+ }
+
+ if ( WP_Http::OK !== wp_remote_retrieve_response_code( $request ) ) {
+ return new WP_Error( 'response_code_not_ok' );
+ }
+
+ $body = wp_remote_retrieve_body( $request );
+ $result = json_decode( $body, true );
+
+ if ( JSON_ERROR_NONE !== json_last_error() ) {
+ return new WP_Error( 'json_parse_error' );
+ }
+
+ if ( empty( $result['matches'] ) ) {
+ return [];
+ }
+
+ $suggestions = [];
+ foreach ( $result['matches'] as $match ) {
+ $suggestions[] = [
+ 'similarity_score' => $match['score'],
+ 'source' => $match['source'],
+ 'translation' => $match['text'],
+ 'diff' => ( 1 === $match['score'] ) ? null : self::diff( $text, $match['source'] ),
+ ];
+ }
+
+ return $suggestions;
+ }
+
+ /**
+ * Generates the differences between two sequences of strings.
+ *
+ * @param string $previous_text Previous text.
+ * @param string $text New text.
+ * @return string HTML markup for the differences between the two texts.
+ */
+ protected static function diff( $previous_text, $text ) {
+ $diff = new Text_Diff( 'auto', [ [ $previous_text ], [ $text ] ] );
+ $renderer = new WP_Text_Diff_Renderer_inline();
+ return $renderer->render( $diff );
+ }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclassotherlanguagesphp"></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-translation-suggestions/inc/routes/class-other-languages.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-other-languages.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-other-languages.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,82 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\TranslationSuggestions\Routes;
+
+use GP;
+use GP_Route;
+use WordPressdotorg\GlotPress\TranslationSuggestions\Translation_Memory_Client;
+use const WordPressdotorg\GlotPress\TranslationSuggestions\PLUGIN_DIR;
+
+class Other_Languages extends GP_Route {
+
+ public function get_suggestions( $project_path, $locale_slug, $translation_set_slug ) {
+ $original_id = gp_get( 'original' );
+ $nonce = gp_get( 'nonce' );
+
+ if ( ! wp_verify_nonce( $nonce, 'other-languages-suggestions-' . $original_id ) ) {
+ wp_send_json_error( 'invalid_nonce' );
+ }
+
+ if ( empty( $original_id ) ) {
+ wp_send_json_error( 'no_original' );
+ }
+
+ $original = GP::$original->get( $original_id );
+ if ( ! $original ) {
+ wp_send_json_error( 'invalid_original' );
+ }
+
+ $project = GP::$project->by_path( $project_path );
+ if ( ! $project ) {
+ wp_send_json_error( 'project_not_found' );
+ }
+
+ $translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
+ if ( ! $translation_set ) {
+ wp_send_json_error( 'translation_set_found' );
+ }
+
+ $suggestions = $this->query( $original, $translation_set );
+
+ wp_send_json_success( gp_tmpl_get_output( 'other-languages-suggestions', [ 'suggestions' => $suggestions ], PLUGIN_DIR . '/templates/' ) );
+ }
+
+ protected function query( $original, $set_to_exclude ) {
+ global $wpdb;
+
+ $sql = "
+SELECT
+ translation.id AS translation_id,
+ translation.translation_0 AS translation,
+ translation.status,
+ translation.user_id,
+ translation.date_added,
+ translation_set.id AS translation_set_id,
+ translation_set.locale,
+ translation_set.slug
+FROM {$wpdb->gp_translations} AS translation
+ JOIN {$wpdb->gp_translation_sets} AS translation_set
+ ON translation.translation_set_id = translation_set.id
+WHERE
+ translation.original_id = %d AND
+ translation.translation_set_id != %d AND
+ translation.status = 'current'
+ORDER BY
+ translation_set.locale LIKE %s DESC,
+ translation_set.locale,
+ translation_set.slug
+";
+
+ $results = $wpdb->get_results(
+ $wpdb->prepare(
+ $sql,
+ $original->id,
+ $set_to_exclude->id,
+ $wpdb->esc_like( $set_to_exclude->locale ) . '%'
+ ),
+ ARRAY_A
+ );
+
+ return $results;
+ }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclasstranslationmemoryphp"></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-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 (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,37 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\TranslationSuggestions\Routes;
+
+use GP;
+use GP_Route;
+use WordPressdotorg\GlotPress\TranslationSuggestions\Translation_Memory_Client;
+use const WordPressdotorg\GlotPress\TranslationSuggestions\PLUGIN_DIR;
+
+class Translation_Memory extends GP_Route {
+
+ public function get_suggestions( $project_path, $locale_slug ) {
+ $original_id = gp_get( 'original' );
+ $nonce = gp_get( 'nonce' );
+
+ if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
+ wp_send_json_error( 'invalid_nonce' );
+ }
+
+ if ( empty( $original_id ) ) {
+ wp_send_json_error( 'no_original' );
+ }
+
+ $original = GP::$original->get( $original_id );
+ if ( ! $original ) {
+ wp_send_json_error( 'invalid_original' );
+ }
+
+ $suggestions = Translation_Memory_Client::query( $original->singular, $locale_slug );
+
+ if ( is_wp_error( $suggestions ) ) {
+ wp_send_json_error( $suggestions->get_error_code() );
+ }
+
+ wp_send_json_success( gp_tmpl_get_output( 'translation-memory-suggestions', [ 'suggestions' => $suggestions ], PLUGIN_DIR . '/templates/' ) );
+ }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsjstranslationsuggestionsjs"></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-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 (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/js/translation-suggestions.js 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,141 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+( function( $ ){
+ function fetchTranslationMemorySuggestions( $container, originalId, nonce ) {
+ var xhr = $.ajax( {
+ url: window.WPORG_TRANSLATION_MEMORY_API_URL,
+ data: {
+ 'original': originalId,
+ 'nonce': nonce
+ },
+ dataType: 'json'
+ } );
+
+ xhr.done( function( response ) {
+ $container.find( '.suggestions__loading-indicator' ).remove();
+ if ( response.success ) {
+ $container.append( response.data );
+ } else {
+ $container.append( $( '<span/>', { 'text': 'Error while loading suggestions.' } ) );
+ }
+ $container.addClass( 'initialized' );
+ } );
+
+ xhr.fail( function() {
+ $container.find( '.suggestions__loading-indicator' ).remove();
+ $container.append( $( '<span/>', { 'text': 'Error while loading suggestions.' } ) );
+ $container.addClass( 'initialized' );
+ } );
+
+ xhr.always( function() {
+ $container.removeClass( 'fetching' );
+ } );
+ }
+
+ function maybeFetchTranslationMemorySuggestions() {
+ var $container = $gp.editor.current.find( '.suggestions__translation-memory' );
+ if ( ! $container.length ) {
+ return;
+ }
+
+ if ( $container.hasClass( 'initialized' ) || $container.hasClass( 'fetching' ) ) {
+ return;
+ }
+
+ $container.addClass( 'fetching' );
+
+ var originalId = $gp.editor.current.original_id;
+ var nonce = $container.data( 'nonce' );
+
+ fetchTranslationMemorySuggestions( $container, originalId , nonce );
+ }
+
+ function fetchOtherLanguageSuggestions( $container, originalId, nonce ) {
+ var xhr = $.ajax( {
+ url: window.WPORG_OTHER_LANGUAGES_API_URL,
+ data: {
+ 'original': originalId,
+ 'nonce': nonce
+ },
+ dataType: 'json'
+ } );
+
+ xhr.done( function( response ) {
+ $container.find( '.suggestions__loading-indicator' ).remove();
+ if ( response.success ) {
+ $container.append( response.data );
+ } else {
+ $container.append( $( '<span/>', { 'text': 'Error while loading suggestions.' } ) );
+ }
+ $container.addClass( 'initialized' );
+ } );
+
+ xhr.fail( function() {
+ $container.find( '.suggestions__loading-indicator' ).remove();
+ $container.append( $( '<span/>', { 'text': 'Error while loading suggestions.' } ) );
+ $container.addClass( 'initialized' );
+ } );
+
+ xhr.always( function() {
+ $container.removeClass( 'fetching' );
+ } );
+ }
+
+ function maybeFetchOtherLanguageSuggestions() {
+ var $container = $gp.editor.current.find( '.suggestions__other-languages' );
+ if ( ! $container.length ) {
+ return;
+ }
+
+ if ( $container.hasClass( 'initialized' ) || $container.hasClass( 'fetching' ) ) {
+ return;
+ }
+
+ $container.addClass( 'fetching' );
+
+ var originalId = $gp.editor.current.original_id;
+ var nonce = $container.data( 'nonce' );
+
+ fetchOtherLanguageSuggestions( $container, originalId , nonce );
+ }
+
+ function copySuggestion( event ) {
+ if ( 'A' === event.target.tagName ) {
+ return;
+ }
+
+ var $el = $( this ).closest( '.translation-suggestion' );
+ var $translation = $el.find( '.translation-suggestion__translation-raw');
+ if ( ! $translation.length ) {
+ return;
+ }
+
+ var $activeTextarea = $gp.editor.current.find( '.textareas.active textarea' );
+ if ( ! $activeTextarea.length ) {
+ return;
+ }
+
+ $activeTextarea.val( $translation.text() ).focus();
+
+ // Trigger input event for autosize().
+ var event = new Event( 'input' );
+ $activeTextarea[0].dispatchEvent( event );
+ }
+
+ $gp.editor.show = ( function( original ) {
+ return function() {
+ original.apply( $gp.editor, arguments );
+
+ maybeFetchTranslationMemorySuggestions();
+ }
+ })( $gp.editor.show );
+
+ $gp.editor.install_hooks = ( function( original ) {
+ return function() {
+ original();
+
+ $( $gp.editor.table )
+ .on( 'click', '.translation-suggestion', copySuggestion )
+ .on( 'click', '.suggestions__other-languages summary', maybeFetchOtherLanguageSuggestions );
+ }
+ })( $gp.editor.install_hooks );
+
+})( jQuery );
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionstemplatesotherlanguagessuggestionsphp"></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-translation-suggestions/templates/other-languages-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/other-languages-suggestions.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/other-languages-suggestions.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,44 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+if ( empty( $suggestions ) ) {
+ echo '<p class="no-suggestions">No suggestions.</p>';
+} else {
+ echo '<ul class="suggestions-list">';
+ foreach ( $suggestions as $suggestion ) {
+ echo '<li>';
+ echo '<div class="translation-suggestion with-tooltip" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
+
+ echo '<span class="translation-suggestion__translation">';
+ echo esc_translation( $suggestion['translation'] );
+
+ echo '<span class="translation-suggestion__translation-meta">';
+ $gp_locale_slug = $suggestion['locale'];
+ if ( 'default' !== $suggestion['slug'] ) {
+ $gp_locale_slug .= '/' . $suggestion['slug'];
+ }
+ $gp_locale = GP_Locales::by_slug( $gp_locale_slug );
+
+ echo esc_html( $gp_locale->english_name );
+
+ if ( $suggestion['user_id'] ) {
+ $user = get_user_by( 'id', $suggestion['user_id'] );
+ if ( $user ) {
+ printf(
+ ' | By <a href="https://profiles.wordpress.org/%s">%s</a>',
+ $user->user_nicename,
+ esc_html( $user->display_name )
+ );
+ }
+ }
+
+ echo '</span>';
+ echo '</span>';
+
+ echo '<span aria-hidden="true" class="translation-suggestion__translation-raw">' . esc_translation( $suggestion['translation'] ) . '</span>';
+
+ echo '<button type="button" class="copy-suggestion">Copy</button>';
+ echo '</div>';
+ echo '</li>';
+ }
+ echo '</ul>';
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionstemplatestranslationmemorysuggestionsphp"></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-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 (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/templates/translation-memory-suggestions.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,27 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+if ( empty( $suggestions ) ) {
+ echo '<p class="no-suggestions">No suggestions.</p>';
+} else {
+ echo '<ul class="suggestions-list">';
+ foreach ( $suggestions as $suggestion ) {
+ echo '<li>';
+ echo '<div class="translation-suggestion with-tooltip" tabindex="0" role="button" aria-pressed="false" aria-label="Copy translation">';
+ echo '<span class="translation-suggestion__score">' . number_format( 100 * $suggestion['similarity_score'] ) . '%</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="copy-suggestion">Copy</button>';
+ echo '</div>';
+ echo '</li>';
+ }
+ echo '</ul>';
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsvendorwordpressdotorgautoloadclassautoloaderphp"></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-translation-suggestions/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-translation-suggestions/vendor/wordpressdotorg/autoload/class-autoloader.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/vendor/wordpressdotorg/autoload/class-autoloader.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,93 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+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 = '';
+
+ 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
+ * @param string $path
+ */
+function register_class_path( $prefix, $path ) {
+ $loader = new Autoloader( $prefix, $path );
+ spl_autoload_register( array( $loader, 'load' ) );
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionswporggptranslationsuggestionsphp"></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-translation-suggestions/wporg-gp-translation-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/wporg-gp-translation-suggestions.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/wporg-gp-translation-suggestions.php 2019-05-06 13:41:27 UTC (rev 8732)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,27 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Plugin name: GlotPress: Translation Suggestions
+ * Description: Provides suggestions from translation memory via translate.wordpress.com and from translations of other languages.
+ * Version: 1.0
+ * Author: WordPress.org
+ * Author URI: http://wordpress.org/
+ * License: GPLv2 or later
+ */
+
+namespace WordPressdotorg\GlotPress\TranslationSuggestions;
+
+use WordPressdotorg\Autoload;
+
+// Store the root plugin file for usage with functions which use the plugin basename.
+const PLUGIN_FILE = __FILE__;
+const PLUGIN_DIR = __DIR__;
+
+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></pre>
</div>
</div>
</body>
</html>