<!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>[GlotPress][861] trunk/plugins/google-translate/google-translate.php: Stop using split inside Google Translate.</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 { 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">
<dt>Revision</dt> <dd><a href="https://glotpress.trac.wordpress.org/changeset/861">861</a></dd>
<dt>Author</dt> <dd>markoheijnen</dd>
<dt>Date</dt> <dd>2014-03-08 21:38:33 +0000 (Sat, 08 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Stop using split inside Google Translate. Also cleans up the code to hide possible notices. See <a href="http://glotpress.trac.wordpress.org/ticket/170">#170</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkpluginsgoogletranslategoogletranslatephp">trunk/plugins/google-translate/google-translate.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkpluginsgoogletranslategoogletranslatephp"></a>
<div class="modfile"><h4>Modified: trunk/plugins/google-translate/google-translate.php (860 => 861)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/plugins/google-translate/google-translate.php      2014-03-08 20:22:09 UTC (rev 860)
+++ trunk/plugins/google-translate/google-translate.php 2014-03-08 21:38:33 UTC (rev 861)
</span><span class="lines">@@ -3,13 +3,17 @@
</span><span class="cx"> class GP_Google_Translate extends GP_Plugin {
</span><span class="cx">  public $id = 'google-translate';
</span><span class="cx"> 
</span><ins>+       public $errors  = array();
+       public $notices = array();
+
</ins><span class="cx">   private $key;
</span><span class="cx"> 
</span><span class="cx">  public function __construct() {
</span><span class="cx">          $this->key = gp_const_get('GP_GOOGLE_TRANSLATE_KEY');
</span><span class="cx"> 
</span><del>-               if( ! $this->key )
-                       return; 
</del><ins>+                if ( ! $this->key ) {
+                       return;
+               }
</ins><span class="cx"> 
</span><span class="cx">          parent::__construct();
</span><span class="cx"> 
</span><span class="lines">@@ -17,17 +21,20 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  public function load_script( $template, $args ) {
</span><del>-               if( 'translations' != $template )
</del><ins>+                if ( 'translations' != $template ) {
</ins><span class="cx">                   return;
</span><ins>+               }
</ins><span class="cx"> 
</span><del>-               if( ! $args['locale']->google_code )
</del><ins>+                if ( ! $args['locale']->google_code ) {
</ins><span class="cx">                   return;
</span><ins>+               }
</ins><span class="cx"> 
</span><del>-               if( is_ssl() )
-                       $url = gp_url_ssl( gp_url_public_root() );
-               else
-                       $url = gp_url_public_root();
</del><ins>+                $url = gp_url_public_root();
</ins><span class="cx"> 
</span><ins>+               if ( is_ssl() ) {
+                       $url = gp_url_ssl( $url );
+               }
+
</ins><span class="cx">           $options = array(
</span><span class="cx">                  'key'    => $this->key,
</span><span class="cx">                  'locale' => $args['locale']->google_code
</span><span class="lines">@@ -53,9 +60,10 @@
</span><span class="cx">          echo '<option value="gtranslate">' . __('Translate via Google') . '</option>';
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       function gp_translation_set_bulk_action_post( $project, $locale, $translation_set, $bulk ) {
-               if( 'gtranslate' != $bulk['action'] )
</del><ins>+        public function gp_translation_set_bulk_action_post( $project, $locale, $translation_set, $bulk ) {
+               if ( 'gtranslate' != $bulk['action'] ) {
</ins><span class="cx">                   return;
</span><ins>+               }
</ins><span class="cx"> 
</span><span class="cx">          $google_errors = 0;
</span><span class="cx">          $insert_errors = 0;
</span><span class="lines">@@ -65,16 +73,16 @@
</span><span class="cx">          $singulars = array();
</span><span class="cx">          $original_ids = array();
</span><span class="cx"> 
</span><del>-               foreach( $bulk['row-ids'] as $row_id ) {
-                       if ( gp_in( '-', $row_id) ) {
</del><ins>+                foreach ( $bulk['row-ids'] as $row_id ) {
+                       if ( gp_in( '-', $row_id ) ) {
</ins><span class="cx">                           $skipped++;
</span><span class="cx">                          continue;
</span><span class="cx">                  }
</span><span class="cx"> 
</span><del>-                       $original_id = gp_array_get( split( '-', $row_id ), 0 );
</del><ins>+                        $original_id = gp_array_get( explode( '-', $row_id ), 0 );
</ins><span class="cx">                   $original    = GP::$original->get( $original_id );
</span><span class="cx"> 
</span><del>-                       if ( !$original || $original->plural ) {
</del><ins>+                        if ( ! $original || $original->plural ) {
</ins><span class="cx">                           $skipped++;
</span><span class="cx">                          continue;
</span><span class="cx">                  }
</span><span class="lines">@@ -91,7 +99,14 @@
</span><span class="cx">                  return;
</span><span class="cx"> 
</span><span class="cx">          }
</span><del>-               foreach( gp_array_zip( $original_ids, $singulars, $results )  as $item ) {
</del><ins>+
+               $items = gp_array_zip( $original_ids, $singulars, $results );
+
+               if ( ! $items ) {
+                       return;
+               }
+
+               foreach ( $items as $item ) {
</ins><span class="cx">                   list( $original_id, $singular, $translation ) = $item;
</span><span class="cx"> 
</span><span class="cx">                  if ( is_wp_error( $translation ) ) {
</span><span class="lines">@@ -114,17 +129,21 @@
</span><span class="cx">          if ( $google_errors > 0 || $insert_errors > 0 ) {
</span><span class="cx">                  $message = array();
</span><span class="cx"> 
</span><del>-                       if ( $ok )
</del><ins>+                        if ( $ok ) {
</ins><span class="cx">                           $message[] = sprintf( __('Added: %d.' ), $ok );
</span><ins>+                       }
</ins><span class="cx"> 
</span><del>-                       if ( $google_errors )
</del><ins>+                        if ( $google_errors ) {
</ins><span class="cx">                           $message[] = sprintf( __('Error from Google Translate: %d.' ), $google_errors );
</span><ins>+                       }
</ins><span class="cx"> 
</span><del>-                       if ( $insert_errors )
</del><ins>+                        if ( $insert_errors ) {
</ins><span class="cx">                           $message[] = sprintf( __('Error adding: %d.' ), $insert_errors );
</span><ins>+                       }
</ins><span class="cx"> 
</span><del>-                       if ( $skipped )
</del><ins>+                        if ( $skipped ) {
</ins><span class="cx">                           $message[] = sprintf( __('Skipped: %d.' ), $skipped );
</span><ins>+                       }
</ins><span class="cx"> 
</span><span class="cx">                  $this->errors[] = implode( '', $message );
</span><span class="cx">          }
</span><span class="lines">@@ -133,9 +152,10 @@
</span><span class="cx">          }
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       function google_translate_batch( $locale, $strings ) {
-               if ( ! $locale->google_code )
</del><ins>+        public function google_translate_batch( $locale, $strings ) {
+               if ( ! $locale->google_code ) {
</ins><span class="cx">                   return new WP_Error( 'google_translate', sprintf( "The locale %s isn't supported by Google Translate.", $locale->slug ) );
</span><ins>+               }
</ins><span class="cx"> 
</span><span class="cx">          $url = 'https://www.googleapis.com/language/translate/v2?key=' . $this->key . '&source=en&target=' . urlencode( $locale->google_code );
</span><span class="cx"> 
</span><span class="lines">@@ -143,37 +163,48 @@
</span><span class="cx">                  $url .= '&q=' . urlencode( $string );
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               if ( count( $strings ) == 1 )
</del><ins>+                if ( count( $strings ) == 1 ) {
</ins><span class="cx">                   $url .= '&q=';
</span><ins>+               }
</ins><span class="cx"> 
</span><span class="cx">          $response = wp_remote_get( $url );
</span><span class="cx"> 
</span><del>-               if ( is_wp_error( $response ) )
</del><ins>+                if ( is_wp_error( $response ) ) {
</ins><span class="cx">                   return $response;
</span><ins>+               }
</ins><span class="cx"> 
</span><span class="cx">          $json = json_decode( wp_remote_retrieve_body( $response ) );
</span><span class="cx"> 
</span><del>-               if ( ! $json )
</del><ins>+                if ( ! $json ) {
</ins><span class="cx">                   return new WP_Error( 'google_translate', 'Error decoding JSON from Google Translate.' );
</span><ins>+               }
</ins><span class="cx"> 
</span><del>-               if ( isset( $json->error ) )
</del><ins>+                if ( isset( $json->error ) ) {
</ins><span class="cx">                   return new WP_Error( 'google_translate', sprintf( 'Error auto-translating: %1$s', $json->error->errors[0]->message ) );
</span><ins>+               }
</ins><span class="cx"> 
</span><span class="cx">          $translations = array();
</span><span class="cx"> 
</span><del>-               if ( ! is_array( $json->data->translations ) )
</del><ins>+                if ( ! is_array( $json->data->translations ) ) {
</ins><span class="cx">                   $json->data->translations = array( $json->data->translations );
</span><ins>+               }
</ins><span class="cx"> 
</span><del>-               foreach( gp_array_zip( $strings, $json->data->translations ) as $item ) {
</del><ins>+                $items = gp_array_zip( $strings, $json->data->translations );
+
+               if ( ! $items ) {
+                       return new WP_Error( 'google_translate', 'Error merging arrays' );
+               }
+
+               foreach ( $items as $item ) {
</ins><span class="cx">                   list( $string, $translation ) = $item;
</span><span class="cx"> 
</span><del>-                               $translations[] = $this->google_translate_fix( $translation->translatedText );
</del><ins>+                        $translations[] = $this->google_translate_fix( $translation->translatedText );
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          return $translations;
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       function google_translate_fix( $string ) {
</del><ins>+        public function google_translate_fix( $string ) {
</ins><span class="cx">           $string = preg_replace_callback( '/% (s|d)/i', lambda( '$m', '"%".strtolower($m[1])' ), $string );
</span><span class="cx">          $string = preg_replace_callback( '/% (\d+) \$ (s|d)/i', lambda( '$m', '"%".$m[1]."\\$".strtolower($m[2])' ), $string );
</span><span class="cx">          return $string;
</span></span></pre>
</div>
</div>

</body>
</html>