Index: trunk/extensions/Translate/Translate.php |
— | — | @@ -356,6 +356,17 @@ |
357 | 357 | 'type' => 'apertium', |
358 | 358 | 'codemap' => array( 'no' => 'nb' ), |
359 | 359 | ); |
| 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 | +*/ |
360 | 371 | |
361 | 372 | /** |
362 | 373 | * List of tasks in Special:Translate. If you are only using page translation |
Index: trunk/extensions/Translate/README |
— | — | @@ -31,6 +31,7 @@ |
32 | 32 | |
33 | 33 | == Change log == |
34 | 34 | * 2012-03-05 |
| 35 | +- Support for using remote TTMServers via API interface added. |
35 | 36 | - Support for tmserver was removed. Translate comes with TTMServer enabled by default. |
36 | 37 | To bootstrap it with current translations, run php scripts/ttmserver-export.php. |
37 | 38 | * 2012-02-19 |
Index: trunk/extensions/Translate/utils/TranslationHelpers.php |
— | — | @@ -260,29 +260,91 @@ |
261 | 261 | $definition = $this->getDefinition(); |
262 | 262 | $suggestions = TTMServer::primary()->query( $source, $code, $definition ); |
263 | 263 | 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 ) ); |
264 | 301 | return null; |
265 | 302 | } |
266 | 303 | |
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 ) { |
268 | 315 | $accuracy = wfMsgHtml( 'translate-edit-tmmatch' , sprintf( '%.2f', $s['quality'] * 100 ) ); |
269 | 316 | $legend = array( $accuracy => array() ); |
270 | 317 | |
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 | + } |
276 | 332 | } |
277 | 333 | |
278 | 334 | // 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 ); |
280 | 342 | $params = array( 'class' => 'mw-sp-translate-edit-tmsug', 'title' => $s['source'] ); |
281 | 343 | |
282 | 344 | // 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 ); |
285 | 347 | } else { |
286 | | - $sugFields[$s['target']] = array( $text, $params, $legend ); |
| 348 | + $sugFields[$suggestion] = array( $text, $params, $legend ); |
287 | 349 | } |
288 | 350 | } |
289 | 351 | |