Index: trunk/extensions/LiveTranslate/RELEASE-NOTES |
— | — | @@ -7,7 +7,8 @@ |
8 | 8 | === Version 0.5 === |
9 | 9 | 2011-01-xx |
10 | 10 | |
11 | | -* |
| 11 | +* Modified the LT API to work with batches, so translation doesn't break for big translation memories. |
| 12 | +* Fixed regex escaping issue that caused translations to stop when encountering certain characters in special words. |
12 | 13 | |
13 | 14 | === Version 0.4 === |
14 | 15 | 2011-01-14 |
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js |
— | — | @@ -44,40 +44,15 @@ |
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | | - * Queries the special words in the source language, finds them in the page, |
49 | | - * and wraps the into notranslate spans. Then initiates the translation process. |
| 48 | + * Disables the translation button and then either kicks of insertion of |
| 49 | + * notranslate spans around special words, or when this already happened, |
| 50 | + * the actual translation process. |
50 | 51 | */ |
51 | 52 | setupTranslationFeatures = function() { |
52 | 53 | $( this ).attr( "disabled", true ).text( mediaWiki.msg( 'livetranslate-button-translating' ) ); |
53 | 54 | |
54 | 55 | if ( originalHtml === false ) { |
55 | | - $.getJSON( |
56 | | - wgScriptPath + '/api.php', |
57 | | - { |
58 | | - 'action': 'query', |
59 | | - 'format': 'json', |
60 | | - 'list': 'livetranslate', |
61 | | - 'ltlanguage': currentLang |
62 | | - }, |
63 | | - function( data ) { |
64 | | - if ( data.words ) { |
65 | | - insertNoTranslateTags( data.words ); |
66 | | - } |
67 | | - else if ( data.error && data.error.info ) { |
68 | | - alert( data.error.info ); |
69 | | - } |
70 | | - else { |
71 | | - for ( i in data ) { |
72 | | - alert( mediaWiki.msg( 'livetranslate-dictionary-error' ) ); |
73 | | - break; |
74 | | - } |
75 | | - } |
76 | | - |
77 | | - originalHtml = $( '#bodyContent' ).html(); |
78 | | - |
79 | | - initiateTranslating(); |
80 | | - } |
81 | | - ); |
| 56 | + obtainAndInsertTranslations( -1 ); |
82 | 57 | } |
83 | 58 | else { |
84 | 59 | initiateTranslating(); |
— | — | @@ -85,6 +60,52 @@ |
86 | 61 | } |
87 | 62 | |
88 | 63 | /** |
| 64 | + * Queries a batch of special words in the source language, finds them in the page, |
| 65 | + * and wraps the into notranslate spans. If there are no more words, the translation |
| 66 | + * process is initaiated, otherwise the function calls itself again. |
| 67 | + */ |
| 68 | + function obtainAndInsertTranslations( offset ) { |
| 69 | + var requestArgs = { |
| 70 | + 'action': 'query', |
| 71 | + 'format': 'json', |
| 72 | + 'list': 'livetranslate', |
| 73 | + 'ltlanguage': currentLang, |
| 74 | + }; |
| 75 | + |
| 76 | + if ( offset > 0 ) { |
| 77 | + requestArgs['ltcontinue'] = offset; |
| 78 | + } |
| 79 | + |
| 80 | + $.getJSON( |
| 81 | + wgScriptPath + '/api.php', |
| 82 | + requestArgs, |
| 83 | + function( data ) { |
| 84 | + if ( data.words ) { |
| 85 | + insertNoTranslateTags( data.words ); |
| 86 | + } |
| 87 | + else if ( data.error && data.error.info ) { |
| 88 | + alert( data.error.info ); |
| 89 | + } |
| 90 | + else { |
| 91 | + for ( i in data ) { |
| 92 | + alert( mediaWiki.msg( 'livetranslate-dictionary-error' ) ); |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + originalHtml = $( '#bodyContent' ).html(); |
| 98 | + |
| 99 | + if ( data['query-continue'] ) { |
| 100 | + obtainAndInsertTranslations( data['query-continue'].livetranslate.ltcontinue ); |
| 101 | + } |
| 102 | + else { |
| 103 | + initiateTranslating(); |
| 104 | + } |
| 105 | + } |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
89 | 110 | * Initiates the translation process. |
90 | 111 | * First all special words are found and send to the local API, |
91 | 112 | * and then replaced by their translation in the response. Then |
— | — | @@ -134,6 +155,18 @@ |
135 | 156 | $( '#ltrevertbutton' ).click( showOriginal ); |
136 | 157 | |
137 | 158 | /** |
| 159 | + * Regex text escaping function. |
| 160 | + * Borrowed from http://simonwillison.net/2006/Jan/20/escape/ |
| 161 | + */ |
| 162 | + RegExp.escape = function(text) { |
| 163 | + if (!arguments.callee.sRE) { |
| 164 | + var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\' ]; |
| 165 | + arguments.callee.sRE = new RegExp( '(\\' + specials.join('|\\') + ')', 'g' ); |
| 166 | + } |
| 167 | + return text.replace(arguments.callee.sRE, '\\$1'); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
138 | 171 | * Inserts notranslate spans around the words specified in the passed array in the page content. |
139 | 172 | * |
140 | 173 | * @param {Array} words |
— | — | @@ -141,7 +174,7 @@ |
142 | 175 | function insertNoTranslateTags( words ) { |
143 | 176 | for ( i in words ) { |
144 | 177 | $( '#bodyContent *' ).replaceText( |
145 | | - new RegExp( "\\b" + words[i] + "\\b", "g" ), |
| 178 | + new RegExp( "\\b" + RegExp.escape( words[i] ) + "\\b", "g" ), |
146 | 179 | function( str ) { |
147 | 180 | return '<span class="notranslate">' + str + '</span>' |
148 | 181 | } |
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php |
— | — | @@ -143,34 +143,6 @@ |
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
147 | | - * Gets a list of all special words in a language. |
148 | | - * |
149 | | - * @since 0.1 |
150 | | - * |
151 | | - * @param string $language |
152 | | - * |
153 | | - * @return array |
154 | | - */ |
155 | | - public static function getSpecialWordsForLang( $language ) { |
156 | | - $dbr = wfGetDB( DB_SLAVE ); |
157 | | - |
158 | | - $words = array(); |
159 | | - |
160 | | - // TODO: fix index |
161 | | - $res = $dbr->query( |
162 | | - 'SELECT DISTINCT word_translation FROM ' . |
163 | | - $dbr->tableName( 'live_translate' ) . |
164 | | - ' WHERE word_language = ' . $dbr->addQuotes( $language ) |
165 | | - ); |
166 | | - |
167 | | - while ( $translation = $dbr->fetchObject( $res ) ) { |
168 | | - $words[] = $translation->word_translation; |
169 | | - } |
170 | | - |
171 | | - return $words; |
172 | | - } |
173 | | - |
174 | | - /** |
175 | 147 | * Returns a PHP version of the JavaScript google.language.Languages enum of the Google Translate v1 API. |
176 | 148 | * @see https://code.google.com/apis/language/translate/v1/getting_started.html#LangNameArray |
177 | 149 | * |
Index: trunk/extensions/LiveTranslate/api/ApiQueryLiveTranslate.php |
— | — | @@ -26,8 +26,40 @@ |
27 | 27 | $this->dieUsageMsg( array( 'missingparam', 'language' ) ); |
28 | 28 | } |
29 | 29 | |
30 | | - $specialWords = LiveTranslateFunctions::getSpecialWordsForLang( $params['language'] ); |
| 30 | + $this->addTables( 'live_translate' ); |
31 | 31 | |
| 32 | + $this->addFields( array( |
| 33 | + 'word_id', |
| 34 | + 'word_translation' |
| 35 | + ) ); |
| 36 | + |
| 37 | + $this->addWhere( array( |
| 38 | + 'word_language' => $params['language'] |
| 39 | + ) ); |
| 40 | + |
| 41 | + if ( !is_null( $params['continue'] ) ) { |
| 42 | + $dbr = wfGetDB( DB_SLAVE ); |
| 43 | + $this->addWhere( 'word_id >= ' . $dbr->addQuotes( $params['continue'] ) ); |
| 44 | + } |
| 45 | + |
| 46 | + $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 47 | + $this->addOption( 'ORDER BY', 'word_id ASC' ); |
| 48 | + |
| 49 | + $words = $this->select( __METHOD__ ); |
| 50 | + $specialWords = array(); |
| 51 | + $count = 0; |
| 52 | + |
| 53 | + while ( $word = $words->fetchObject() ) { |
| 54 | + if ( ++$count > $params['limit'] ) { |
| 55 | + // We've reached the one extra which shows that |
| 56 | + // there are additional pages to be had. Stop here... |
| 57 | + $this->setContinueEnumParameter( 'continue', $word->word_id ); |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + $specialWords[] = $word->word_translation; |
| 62 | + } |
| 63 | + |
32 | 64 | $toggeledSpecials = array(); |
33 | 65 | |
34 | 66 | foreach ( $specialWords as $word ) { |
— | — | @@ -57,6 +89,14 @@ |
58 | 90 | ApiBase::PARAM_TYPE => 'string', |
59 | 91 | //ApiBase::PARAM_REQUIRED => true, |
60 | 92 | ), |
| 93 | + 'limit' => array( |
| 94 | + ApiBase :: PARAM_DFLT => 500, |
| 95 | + ApiBase :: PARAM_TYPE => 'limit', |
| 96 | + ApiBase :: PARAM_MIN => 1, |
| 97 | + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, |
| 98 | + ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 |
| 99 | + ), |
| 100 | + 'continue' => null, |
61 | 101 | ); |
62 | 102 | } |
63 | 103 | |
— | — | @@ -67,6 +107,8 @@ |
68 | 108 | public function getParamDescription() { |
69 | 109 | return array ( |
70 | 110 | 'language' => 'The language for which to return special words', |
| 111 | + 'continue' => 'Offset number from where to continue the query', |
| 112 | + 'limit' => 'Max amount of words to return', |
71 | 113 | ); |
72 | 114 | } |
73 | 115 | |
Index: trunk/extensions/LiveTranslate/api/ApiLiveTranslate.php |
— | — | @@ -99,7 +99,7 @@ |
100 | 100 | ApiBase::PARAM_TYPE => 'string', |
101 | 101 | ApiBase::PARAM_ISMULTI => true, |
102 | 102 | //ApiBase::PARAM_REQUIRED => true, |
103 | | - ), |
| 103 | + ), |
104 | 104 | ); |
105 | 105 | } |
106 | 106 | |