r113023 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113022‎ | r113023 | r113024 >
Date:12:10, 5 March 2012
Author:nikerabbit
Status:ok
Tags:i18nreview 
Comment:
Support for remote TTMServers
Modified paths:
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -356,6 +356,17 @@
357357 'type' => 'apertium',
358358 'codemap' => array( 'no' => 'nb' ),
359359 );
 360+/* Example configuration for remote TTMServer
 361+$wgTranslateTranslationServices['example'] = array(
 362+ 'url' => 'http://example.com/w/api.php',
 363+ 'viewurl' => '//example.com/wiki/',
 364+ 'displayname' => 'example.com',
 365+ 'cutoff' => 0.75,
 366+ 'timeout-sync' => 4,
 367+ 'timeout-async' => 4,
 368+ 'type' => 'remote-ttmserver',
 369+);
 370+*/
360371
361372 /**
362373 * List of tasks in Special:Translate. If you are only using page translation
Index: trunk/extensions/Translate/README
@@ -31,6 +31,7 @@
3232
3333 == Change log ==
3434 * 2012-03-05
 35+- Support for using remote TTMServers via API interface added.
3536 - Support for tmserver was removed. Translate comes with TTMServer enabled by default.
3637 To bootstrap it with current translations, run php scripts/ttmserver-export.php.
3738 * 2012-02-19
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -260,29 +260,91 @@
261261 $definition = $this->getDefinition();
262262 $suggestions = TTMServer::primary()->query( $source, $code, $definition );
263263 if ( count( $suggestions ) === 0 ) {
 264+ throw new TranslationHelperExpection( 'No suggestions' );
 265+ }
 266+
 267+ return $this->formatTTMServerSuggestions( $suggestions, $code, $config );
 268+ }
 269+
 270+ /**
 271+ * Returns suggestions from a translation memory.
 272+ * @param $serviceName
 273+ * @param $config
 274+ * @return string Html snippet which contains the suggestions.
 275+ */
 276+ protected function getRemoteTTMServerBox( $serviceName, $config ) {
 277+ $this->mustHaveDefinition();
 278+ $this->mustBeTranslation();
 279+
 280+ self::checkTranslationServiceFailure( $serviceName );
 281+
 282+ $source = $this->group->getSourceLanguage();
 283+ $code = $this->handle->getCode();
 284+ $definition = $this->getDefinition();
 285+ $params = array(
 286+ 'format' => 'json',
 287+ 'action' => 'ttmserver',
 288+ 'sourcelanguage' => $source,
 289+ 'targetlanguage' => $code,
 290+ 'text' => $definition,
 291+ );
 292+
 293+ $json = Http::get( wfAppendQuery( $config['url'], $params ) );
 294+ $response = FormatJson::decode( $json, true );
 295+
 296+ if ( $json === false ) {
 297+ // Most likely a timeout or other general error
 298+ self::reportTranslationServiceFailure( $serviceName );
 299+ } elseif ( !is_array( $response ) ) {
 300+ error_log( __METHOD__ . ': Unable to parse reply: ' . strval( $json ) );
264301 return null;
265302 }
266303
267 - foreach ( $suggestions as $s ) {
 304+ $suggestions = $response['ttmserver'];
 305+ if ( count( $suggestions ) === 0 ) {
 306+ throw new TranslationHelperExpection( 'No suggestions' );
 307+ }
 308+
 309+ return $this->formatTTMServerSuggestions( $suggestions, $code, $config );
 310+ }
 311+
 312+ /// Since 2012-03-05
 313+ protected function formatTTMServerSuggestions( $data, $code, $config ) {
 314+ foreach ( $data as $s ) {
268315 $accuracy = wfMsgHtml( 'translate-edit-tmmatch' , sprintf( '%.2f', $s['quality'] * 100 ) );
269316 $legend = array( $accuracy => array() );
270317
271 - $sourceTitle = Title::newFromText( $s['context'] );
272 - $handle = new MessageHandle( $sourceTitle );
273 - $targetTitle = Title::makeTitle( $sourceTitle->getNamespace(), $handle->getKey() . "/$code" );
274 - if ( $targetTitle ) {
275 - $legend[$accuracy][] = self::ajaxEditLink( $targetTitle, '•' );
 318+ if ( $config['type'] === 'remote-ttmserver' ) {
 319+ $params = array(
 320+ 'href' => $url = $config['viewurl'] . $s['context'],
 321+ 'target' => '_blank',
 322+ 'title' => $config['displayname'],
 323+ );
 324+ $legend[$accuracy][] = Html::element( 'a', $params, '‣' );
 325+ } else {
 326+ $sourceTitle = Title::newFromText( $s['context'] );
 327+ $handle = new MessageHandle( $sourceTitle );
 328+ $targetTitle = Title::makeTitle( $sourceTitle->getNamespace(), $handle->getKey() . "/$code" );
 329+ if ( $targetTitle ) {
 330+ $legend[$accuracy][] = self::ajaxEditLink( $targetTitle, '•' );
 331+ }
276332 }
277333
278334 // Show the source text in a tooltip
279 - $text = $this->suggestionField( $s['target'] );
 335+ if ( isset( $s['target'] ) ) {
 336+ $suggestion = $s['target'];
 337+ } else {
 338+ $suggestion = $s['*'];
 339+
 340+ }
 341+ $text = $this->suggestionField( $suggestion );
280342 $params = array( 'class' => 'mw-sp-translate-edit-tmsug', 'title' => $s['source'] );
281343
282344 // Group identical suggestions together
283 - if ( isset( $sugFields[$s['target']] ) ) {
284 - $sugFields[$s['target']][2] = array_merge_recursive( $sugFields[$s['target']][2], $legend );
 345+ if ( isset( $sugFields[$suggestion] ) ) {
 346+ $sugFields[$suggestion][2] = array_merge_recursive( $sugFields[$suggestion][2], $legend );
285347 } else {
286 - $sugFields[$s['target']] = array( $text, $params, $legend );
 348+ $sugFields[$suggestion] = array( $text, $params, $legend );
287349 }
288350 }
289351

Follow-up revisions

RevisionCommit summaryAuthorDate
r113027Ping r113023, also register the handlernikerabbit12:44, 5 March 2012

Status & tagging log