r78939 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78938‎ | r78939 | r78940 >
Date:00:21, 24 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changed the way notranslate tags are added - this is now done when the first translation is requested by obtaining the special words via a new API module and replacing them in the page content using a jQuery replaceText plugin
Modified paths:
  • /trunk/extensions/LiveTranslate/LiveTranslate.hooks.php (modified) (history)
  • /trunk/extensions/LiveTranslate/LiveTranslate.i18n.php (modified) (history)
  • /trunk/extensions/LiveTranslate/LiveTranslate.php (modified) (history)
  • /trunk/extensions/LiveTranslate/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/LiveTranslate/api/ApiQueryLiveTranslate.php (added) (history)
  • /trunk/extensions/LiveTranslate/includes/ext.livetranslate.js (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/LiveTranslate.i18n.php
@@ -22,6 +22,7 @@
2323 'livetranslate-button-translate' => 'Translate',
2424 'livetranslate-button-translating' => 'Translating...',
2525 'livetranslate-button-revert' => 'Show original',
 26+ 'livetranslate-dictionary-error' => 'Could not obtain the live translate dictionary. No words will be treated as special during the translation process.',
2627
2728 // Special words dictionary
2829 'livetranslate-dictionary-empty' => 'There are no words in the dictionary yet. Click the "edit" tab to add some.',
Index: trunk/extensions/LiveTranslate/LiveTranslate.php
@@ -47,18 +47,20 @@
4848
4949 $wgAutoloadClasses['LiveTranslateHooks'] = $egLiveTranslateIP . '/LiveTranslate.hooks.php';
5050 $wgAutoloadClasses['ApiLiveTranslate'] = $egLiveTranslateIP . '/api/ApiLiveTranslate.php';
 51+$wgAutoloadClasses['ApiQueryLiveTranslate'] = $egLiveTranslateIP . '/api/ApiQueryLiveTranslate.php';
5152 $wgAutoloadClasses['LiveTranslateFunctions'] = $egLiveTranslateIP . '/includes/LiveTranslate_Functions.php';
5253
5354 $wgAPIModules['livetranslate'] = 'ApiLiveTranslate';
 55+$wgAPIListModules['livetranslate'] = 'ApiQueryLiveTranslate';
5456
5557 $wgHooks['ArticleViewHeader'][] = 'LiveTranslateHooks::onArticleViewHeader';
5658 $wgHooks['LoadExtensionSchemaUpdates'][] = 'LiveTranslateHooks::onSchemaUpdate';
5759 $wgHooks['ArticleSaveComplete'][] = 'LiveTranslateHooks::onArticleSaveComplete';
58 -$wgHooks['OutputPageBeforeHTML'][] = 'LiveTranslateHooks::onOutputPageBeforeHTML';
5960
6061 $egLTJSMessages = array(
6162 'livetranslate-button-translate',
6263 'livetranslate-button-translating',
 64+ 'livetranslate-dictionary-error',
6365 );
6466
6567 // For backward compatibility with MW < 1.17.
@@ -70,7 +72,7 @@
7173 );
7274
7375 $wgResourceModules['ext.livetranslate'] = $moduleTemplate + array(
74 - 'scripts' => 'includes/ext.livetranslate.js',
 76+ 'scripts' => array( 'includes/ext.livetranslate.js' ),
7577 'dependencies' => array(),
7678 'messages' => $egLTJSMessages
7779 );
Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php
@@ -172,47 +172,5 @@
173173
174174 return true;
175175 }
176 -
177 - /**
178 - * Called every time wikitext is added to the OutputPage, after it is parsed but before it is added.
179 - * Wraps the words with special translations into no-translate tags.
180 - *
181 - * @since 0.1
182 - *
183 - * @param OutputPage $out
184 - * @param string $text
185 - *
186 - * @return true
187 - */
188 - public static function onOutputPageBeforeHTML( OutputPage &$out, &$text ) {
189 - global $wgTitle;
190176
191 - $currentLang = LiveTranslateFunctions::getCurrentLang( $wgTitle );
192 - $specialWords = LiveTranslateFunctions::getSpecialWordsForLang( $currentLang );
193 -
194 - $toggeledSpecials = array();
195 -
196 - foreach ( $specialWords as $word ) {
197 - $toggledWord = LiveTranslateFunctions::getToggledCase( $word );
198 -
199 - if ( $toggledWord ) {
200 - $toggeledSpecials[] = $toggledWord;
201 - }
202 - }
203 -
204 - foreach ( array_merge( $specialWords, $toggeledSpecials ) as $specialWord ) {
205 - $text = str_replace(
206 - $specialWord ,
207 - Html::element(
208 - 'span',
209 - array( 'class' => 'notranslate', 'original' => $specialWord ),
210 - $specialWord
211 - ),
212 - $text
213 - );
214 - }
215 -
216 - return true;
217 - }
218 -
219177 }
\ No newline at end of file
Index: trunk/extensions/LiveTranslate/RELEASE-NOTES
@@ -9,6 +9,7 @@
1010
1111 * Fixed installation compatibility issue with MW 1.16.
1212 * Fixed display of translation control to only happen on existing pages.
 13+* Fixed issues with inserting notranslatre tags into the page source.
1314
1415 === Version 0.1 ===
1516 2010-12-22
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -6,6 +6,16 @@
77 */
88
99 (function($) { $( document ).ready( function() {
 10+
 11+ /*
 12+ * jQuery replaceText - v1.1 - 11/21/2009
 13+ * http://benalman.com/projects/jquery-replacetext-plugin/
 14+ *
 15+ * Copyright (c) 2009 "Cowboy" Ben Alman
 16+ * Dual licensed under the MIT and GPL licenses.
 17+ * http://benalman.com/about/license/
 18+ */
 19+ $.fn.replaceText=function(b,a,c){return this.each(function(){var f=this.firstChild,g,e,d=[];if(f){do{if(f.nodeType===3){g=f.nodeValue;e=g.replace(b,a);if(e!==g){if(!c&&/</.test(e)){$(f).before(e);d.push(f)}else{f.nodeValue=e}}}}while(f=f.nextSibling)}d.length&&$(d).remove()})}
1020
1121 var currentLang = window.sourceLang;
1222
@@ -31,13 +41,44 @@
3242 }
3343 }
3444
35 - initiateTranslating = function() {
 45+ setupTranslationFeatures = function() {
 46+ $( this ).attr( "disabled", true ).text( mediaWiki.msg( 'livetranslate-button-translating' ) );
 47+
3648 if ( originalHtml === false ) {
37 - originalHtml = $( '#bodyContent' ).html();
38 - }
39 -
40 - $( this ).attr( "disabled", true ).text( mediaWiki.msg( 'livetranslate-button-translating' ) );
41 -
 49+ $.getJSON(
 50+ wgScriptPath + '/api.php',
 51+ {
 52+ 'action': 'query',
 53+ 'format': 'json',
 54+ 'list': 'livetranslate',
 55+ 'ltlanguage': currentLang,
 56+ },
 57+ function( data ) {
 58+ if ( data.words ) {
 59+ insertNoTranslateTags( data.words );
 60+ }
 61+ else if ( data.error && data.error.info ) {
 62+ alert( data.error.info );
 63+ }
 64+ else {
 65+ for ( i in data ) {
 66+ alert( mediaWiki.msg( 'livetranslate-dictionary-error' ) );
 67+ break;
 68+ }
 69+ }
 70+
 71+ originalHtml = $( '#bodyContent' ).html();
 72+
 73+ initiateTranslating();
 74+ }
 75+ );
 76+ }
 77+ else {
 78+ initiateTranslating();
 79+ }
 80+ }
 81+
 82+ function initiateTranslating() {
4283 var words = getSpecialWords();
4384 var newLang = $( '#livetranslatelang' ).val();
4485
@@ -67,13 +108,20 @@
68109 showOriginal = function() {
69110 currentLang = window.sourceLang;
70111 $( '#bodyContent' ).html( originalHtml );
71 - $('#livetranslatebutton').click( initiateTranslating );
72 - $('#ltrevertbutton').click( showOriginal );
 112+ $( '#livetranslatebutton' ).attr( "disabled", false ).text( mediaWiki.msg( 'livetranslate-button-translate' ) );
 113+ $( '#livetranslatebutton' ).click( setupTranslationFeatures );
 114+ $( '#ltrevertbutton' ).click( showOriginal );
73115 }
74116
75 - $('#livetranslatebutton').click( initiateTranslating );
76 - $('#ltrevertbutton').click( showOriginal );
 117+ $( '#livetranslatebutton' ).click( setupTranslationFeatures );
 118+ $( '#ltrevertbutton' ).click( showOriginal );
77119
 120+ function insertNoTranslateTags( words ) {
 121+ for ( i in words ) {
 122+ $( '#bodyContent *' ).replaceText( words[i], function( str ) { return '<span class="notranslate">' + str + '</span>' } );
 123+ }
 124+ }
 125+
78126 function getSpecialWords() {
79127 var words = [];
80128
Index: trunk/extensions/LiveTranslate/api/ApiQueryLiveTranslate.php
@@ -0,0 +1,105 @@
 2+<?php
 3+
 4+/**
 5+ * API module to get special words for a language stored by the Live Translate extension.
 6+ *
 7+ * @since 0.2
 8+ *
 9+ * @file ApiQueryLiveTranslate.php
 10+ * @ingroup LiveTranslate
 11+ *
 12+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 13+ */
 14+class ApiQueryLiveTranslate extends ApiQueryBase {
 15+ public function __construct( $main, $action ) {
 16+ parent :: __construct( $main, $action, 'lt' );
 17+ }
 18+
 19+ /**
 20+ * Retrieve the specil words from the database.
 21+ */
 22+ public function execute() {
 23+ // Get the requests parameters.
 24+ $params = $this->extractRequestParams();
 25+
 26+ if ( !isset( $params['language'] ) ) {
 27+ $this->dieUsageMsg( array( 'missingparam', 'language' ) );
 28+ }
 29+
 30+ $specialWords = LiveTranslateFunctions::getSpecialWordsForLang( $params['language'] );
 31+
 32+ $toggeledSpecials = array();
 33+
 34+ foreach ( $specialWords as $word ) {
 35+ $toggledWord = LiveTranslateFunctions::getToggledCase( $word );
 36+
 37+ if ( $toggledWord ) {
 38+ $toggeledSpecials[] = $toggledWord;
 39+ }
 40+ }
 41+
 42+ foreach ( array_unique( array_merge( $specialWords, $toggeledSpecials ) ) as $word ) {
 43+ $this->getResult()->addValue(
 44+ 'words',
 45+ null,
 46+ $word
 47+ );
 48+ }
 49+ }
 50+
 51+ /**
 52+ * (non-PHPdoc)
 53+ * @see includes/api/ApiBase#getAllowedParams()
 54+ */
 55+ public function getAllowedParams() {
 56+ return array (
 57+ 'language' => array(
 58+ ApiBase::PARAM_TYPE => 'string',
 59+ //ApiBase::PARAM_REQUIRED => true,
 60+ ),
 61+ );
 62+ }
 63+
 64+ /**
 65+ * (non-PHPdoc)
 66+ * @see includes/api/ApiBase#getParamDescription()
 67+ */
 68+ public function getParamDescription() {
 69+ return array (
 70+ 'language' => 'The language for which to return special words',
 71+ );
 72+ }
 73+
 74+ /**
 75+ * (non-PHPdoc)
 76+ * @see includes/api/ApiBase#getDescription()
 77+ */
 78+ public function getDescription() {
 79+ return 'This module returns all special words defined in the wikis Live Translate Dictionary for a given language';
 80+ }
 81+
 82+ /**
 83+ * (non-PHPdoc)
 84+ * @see includes/api/ApiBase#getPossibleErrors()
 85+ */
 86+ public function getPossibleErrors() {
 87+ return array_merge( parent::getPossibleErrors(), array(
 88+ array( 'missingparam', 'language' ),
 89+ ) );
 90+ }
 91+
 92+ /**
 93+ * (non-PHPdoc)
 94+ * @see includes/api/ApiBase#getExamples()
 95+ */
 96+ protected function getExamples() {
 97+ return array (
 98+ 'api.php?action=query&list=livetranslate&ltlanguage',
 99+ );
 100+ }
 101+
 102+ public function getVersion() {
 103+ return __CLASS__ . ': $Id$';
 104+ }
 105+
 106+}
Property changes on: trunk/extensions/LiveTranslate/api/ApiQueryLiveTranslate.php
___________________________________________________________________
Added: svn:eol-style
1107 + native
Added: svn:keywords
2108 + Id

Status & tagging log