Index: trunk/extensions/LiveTranslate/LiveTranslate.php |
— | — | @@ -53,7 +53,7 @@ |
54 | 54 | $incDirIP = $egLiveTranslateIP . '/includes/'; |
55 | 55 | $wgAutoloadClasses['LiveTranslateFunctions'] = $incDirIP . 'LiveTranslate_Functions.php'; |
56 | 56 | $wgAutoloadClasses['LTGCSVParser'] = $incDirIP . 'LT_GCSVParser.php'; |
57 | | -$wgAutoloadClasses['LTLTFParser'] = $incDirIP . 'LT_LFTParser.php'; |
| 57 | +$wgAutoloadClasses['LTLTFParser'] = $incDirIP . 'LT_LTFParser.php'; |
58 | 58 | $wgAutoloadClasses['LTTMParser'] = $incDirIP . 'LT_TMParser.php'; |
59 | 59 | $wgAutoloadClasses['LTTMUnit'] = $incDirIP . 'LT_TMUnit.php'; |
60 | 60 | $wgAutoloadClasses['LTTMXParser'] = $incDirIP . 'LT_TMXParser.php'; |
Index: trunk/extensions/LiveTranslate/specials/SpecialLiveTranslate.php |
— | — | @@ -66,7 +66,11 @@ |
67 | 67 | $this->handleSubmission(); |
68 | 68 | } |
69 | 69 | |
70 | | - $this->displayTMConfig( $this->getTMConfigItems() ); |
| 70 | + $tms = $this->getTMConfigItems(); |
| 71 | + |
| 72 | + $this->displayTMConfig( $tms ); |
| 73 | + |
| 74 | + $this->updateTranslationMemory( $tms ); |
71 | 75 | } |
72 | 76 | |
73 | 77 | /** |
— | — | @@ -119,6 +123,28 @@ |
120 | 124 | } |
121 | 125 | |
122 | 126 | /** |
| 127 | + * Updates the translation memory stored in the db. |
| 128 | + * |
| 129 | + * @since 0.4 |
| 130 | + * |
| 131 | + * @param array $tms The current translation memories |
| 132 | + */ |
| 133 | + protected function updateTranslationMemory( array $tms ) { |
| 134 | + foreach ( $tms as $tm ) { |
| 135 | + $requestData = array( |
| 136 | + 'action' => 'importtms', |
| 137 | + 'format' => 'json', |
| 138 | + 'source' => $tm->memory_location, |
| 139 | + 'type' => $tm->memory_type, |
| 140 | + ); |
| 141 | + |
| 142 | + $api = new ApiMain( new FauxRequest( $requestData, true ), true ); |
| 143 | + $api->execute(); |
| 144 | + $response = $api->getResultData(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
123 | 149 | * Displays the translation memories config table. |
124 | 150 | * |
125 | 151 | * @since 0.4 |
Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php |
— | — | @@ -31,21 +31,23 @@ |
32 | 32 | $currentLang = LiveTranslateFunctions::getCurrentLang( $title ); |
33 | 33 | |
34 | 34 | if ( $title->getFullText() == $egLiveTranslateDirPage ) { |
35 | | - $wordSets = LiveTranslateFunctions::parseTranslations( $article->getContent() ); |
| 35 | + $parser = new LTLTFParser(); |
| 36 | + $tm = $parser->parse( $article->getContent() ); |
| 37 | + $tus = $tm->getTranslationUnits(); |
36 | 38 | |
37 | | - if ( count( $wordSets ) == 0 ) { |
| 39 | + if ( count( $tus ) == 0 ) { |
38 | 40 | $wgOut->addWikiMsg( 'livetranslate-dictionary-empty' ); |
39 | 41 | } |
40 | 42 | else { |
41 | 43 | $wgOut->addWikiMsg( |
42 | 44 | 'livetranslate-dictionary-count', |
43 | | - $wgLang->formatNum( count( $wordSets ) ) , |
44 | | - $wgLang->formatNum( count( $wordSets[0] ) ) |
| 45 | + $wgLang->formatNum( count( $tus ) ) , |
| 46 | + $wgLang->formatNum( count( $tus[0]->getVariants() ) ) |
45 | 47 | ); |
46 | 48 | |
47 | 49 | $notAllowedLanguages = array(); |
48 | 50 | |
49 | | - foreach ( $wordSets[0] as $languageCode => $translations ) { |
| 51 | + foreach ( $tus[0]->getVariants() as $languageCode => $translations ) { |
50 | 52 | if ( !in_array( $languageCode, $egLiveTranslateLanguages ) ) { |
51 | 53 | $notAllowedLanguages[] = $languageCode; |
52 | 54 | } |
— | — | @@ -195,7 +197,16 @@ |
196 | 198 | $title = $article->getTitle(); |
197 | 199 | |
198 | 200 | if ( $title->getFullText() == $egLiveTranslateDirPage ) { |
199 | | - LiveTranslateFunctions::saveTranslations( LiveTranslateFunctions::parseTranslations( $text ) ); |
| 201 | + $requestData = array( |
| 202 | + 'action' => 'importtms', |
| 203 | + 'format' => 'json', |
| 204 | + 'source' => $title->getFullText(), |
| 205 | + 'type' => 0, |
| 206 | + ); |
| 207 | + |
| 208 | + $api = new ApiMain( new FauxRequest( $requestData, true ), true ); |
| 209 | + $api->execute(); |
| 210 | + $response = $api->getResultData(); |
200 | 211 | } |
201 | 212 | |
202 | 213 | return true; |
Index: trunk/extensions/LiveTranslate/includes/LT_GCSVParser.php |
— | — | @@ -21,4 +21,4 @@ |
22 | 22 | return new LTTranslationMemory(); // TODO |
23 | 23 | } |
24 | 24 | |
25 | | -} |
\ No newline at end of file |
| 25 | +} |
Index: trunk/extensions/LiveTranslate/includes/LT_LTFParser.php |
— | — | @@ -18,8 +18,28 @@ |
19 | 19 | * @see LTTMParser::parse() |
20 | 20 | */ |
21 | 21 | public function parse( $text ) { |
22 | | - // LiveTranslateFunctions::parseTranslations |
23 | | - return new LTTranslationMemory(); // TODO |
| 22 | + $tm = new LTTranslationMemory(); |
| 23 | + |
| 24 | + $translationSets = array(); |
| 25 | + |
| 26 | + $lines = explode( "\n", $text ); |
| 27 | + $languages = array_map( 'trim', explode( ',', array_shift( $lines ) ) ); |
| 28 | + |
| 29 | + foreach ( $lines as $line ) { |
| 30 | + $values = array_map( 'trim', explode( ',', $line ) ); |
| 31 | + $tu = new LTTMUnit(); |
| 32 | + |
| 33 | + foreach ( $values as $nr => $value ) { |
| 34 | + if ( array_key_exists( $nr, $languages ) ) { |
| 35 | + // Add the translation (or translations) (value, array) of the word in the language (key). |
| 36 | + $tu->addVariants( array( $languages[$nr] => array_map( 'trim', explode( '|', $value ) ) ) ); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + $tm->addTranslationUnit( $tu ); |
| 41 | + } |
| 42 | + |
| 43 | + return $tm; |
24 | 44 | } |
25 | 45 | |
26 | 46 | } |
\ No newline at end of file |
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php |
— | — | @@ -169,75 +169,8 @@ |
170 | 170 | |
171 | 171 | return $words; |
172 | 172 | } |
173 | | - |
174 | | - /** |
175 | | - * Parses the provided dictionary content and returns it as an |
176 | | - * array of associative arrays. |
177 | | - * |
178 | | - * @since 0.1 |
179 | | - * |
180 | | - * @param string $content |
181 | | - * |
182 | | - * @return array |
183 | | - */ |
184 | | - public static function parseTranslations( $content ) { |
185 | | - $translationSets = array(); |
186 | | - |
187 | | - $lines = explode( "\n", $content ); |
188 | | - $languages = array_map( 'trim', explode( ',', array_shift( $lines ) ) ); |
189 | | - |
190 | | - foreach ( $lines as $line ) { |
191 | | - $values = array_map( 'trim', explode( ',', $line ) ); |
192 | | - |
193 | | - $translations = array(); |
194 | | - |
195 | | - foreach ( $values as $nr => $value ) { |
196 | | - if ( array_key_exists( $nr, $languages ) ) { |
197 | | - // Add the translation (or translations) (value, array) of the word in the language (key). |
198 | | - $translations[$languages[$nr]] = array_map( 'trim', explode( '|', $value ) ); |
199 | | - } |
200 | | - } |
201 | | - |
202 | | - $translationSets[] = $translations; |
203 | | - } |
204 | | - |
205 | | - return $translationSets; |
206 | | - } |
207 | 173 | |
208 | 174 | /** |
209 | | - * Replaces the current translations with the provided ones. |
210 | | - * |
211 | | - * @since 0.1 |
212 | | - * |
213 | | - * @param array $translationSets |
214 | | - */ |
215 | | - public static function saveTranslations( array $translationSets ) { |
216 | | - $dbw = wfGetDB( DB_MASTER ); |
217 | | - |
218 | | - $dbw->query( 'TRUNCATE TABLE ' . $dbw->tableName( 'live_translate' ) ); |
219 | | - |
220 | | - foreach ( $translationSets as $wordId => $translationSet ) { |
221 | | - foreach ( $translationSet as $language => $translations ) { |
222 | | - $primary = 1; |
223 | | - |
224 | | - foreach ( $translations as $translation ) { |
225 | | - $dbw->insert( |
226 | | - 'live_translate', |
227 | | - array( |
228 | | - 'word_id' => $wordId, |
229 | | - 'word_language' => $language, |
230 | | - 'word_translation' => $translation, |
231 | | - 'word_primary' => $primary |
232 | | - ) |
233 | | - ); |
234 | | - |
235 | | - $primary = 0; |
236 | | - } |
237 | | - } |
238 | | - } |
239 | | - } |
240 | | - |
241 | | - /** |
242 | 175 | * Returns a PHP version of the JavaScript google.language.Languages enum of the Google Translate v1 API. |
243 | 176 | * @see https://code.google.com/apis/language/translate/v1/getting_started.html#LangNameArray |
244 | 177 | * |
Index: trunk/extensions/LiveTranslate/includes/LT_TranslationMemory.php |
— | — | @@ -39,7 +39,7 @@ |
40 | 40 | * @param LTTMUnit $tu |
41 | 41 | */ |
42 | 42 | public function addTranslationUnit( LTTMUnit $tu ) { |
43 | | - $translationUnits[] = $tu; |
| 43 | + $this->translationUnits[] = $tu; |
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
Index: trunk/extensions/LiveTranslate/api/ApiImportTranslationMemories.php |
— | — | @@ -55,8 +55,6 @@ |
56 | 56 | break; |
57 | 57 | } |
58 | 58 | |
59 | | - |
60 | | - |
61 | 59 | if ( $tm !== false ) { |
62 | 60 | $this->doTMImport( $tm ); |
63 | 61 | } |
— | — | @@ -64,7 +62,34 @@ |
65 | 63 | } |
66 | 64 | |
67 | 65 | protected function doTMImport( LTTranslationMemory $tm ) { |
| 66 | + $dbw = wfGetDB( DB_MASTER ); |
68 | 67 | |
| 68 | + // TODO: move |
| 69 | + $dbw->query( 'TRUNCATE TABLE ' . $dbw->tableName( 'live_translate' ) ); |
| 70 | + |
| 71 | + $wordId = 0; |
| 72 | + |
| 73 | + foreach ( $tm->getTranslationUnits() as $tu ) { |
| 74 | + foreach ( $tu->getVariants() as $language => $translations ) { |
| 75 | + $primary = 1; |
| 76 | + |
| 77 | + foreach ( $translations as $translation ) { |
| 78 | + $dbw->insert( |
| 79 | + 'live_translate', |
| 80 | + array( |
| 81 | + 'word_id' => $wordId, |
| 82 | + 'word_language' => $language, |
| 83 | + 'word_translation' => $translation, |
| 84 | + 'word_primary' => $primary |
| 85 | + ) |
| 86 | + ); |
| 87 | + |
| 88 | + $primary = 0; |
| 89 | + } |
| 90 | + |
| 91 | + $wordId++; |
| 92 | + } |
| 93 | + } |
69 | 94 | } |
70 | 95 | |
71 | 96 | public function getAllowedParams() { |