Index: trunk/extensions/Translate/Exporters.php |
— | — | @@ -10,28 +10,23 @@ |
11 | 11 | */ |
12 | 12 | |
13 | 13 | interface MessageExporter { |
14 | | - public function __construct( $group, $languages, $target ); |
15 | | - public function export(); |
| 14 | + public function __construct( MessageGroup $group ); |
| 15 | + public function export( Array $languages, $target ); |
16 | 16 | } |
17 | 17 | |
18 | 18 | class CoreExporter implements MessageExporter { |
19 | 19 | protected $group = null; |
20 | | - protected $languages = array(); |
21 | | - protected $target = null; |
22 | | - |
23 | | - public function __construct( $group, $languages, $target ) { |
| 20 | + public function __construct( MessageGroup $group ) { |
24 | 21 | $this->group = $group; |
25 | | - $this->languages = $languages; |
26 | | - $this->target = $target; |
27 | 22 | } |
28 | 23 | |
29 | | - public function export() { |
30 | | - foreach ( $this->languages as $code ) { |
| 24 | + public function export( Array $languages, $target ) { |
| 25 | + foreach ( $languages as $code ) { |
31 | 26 | $taskOptions = new TaskOptions( $code, 0, 0, 0, null ); |
32 | 27 | $task = TranslateTasks::getTask( 'export-to-file' ); |
33 | 28 | $task->init( $this->group, $taskOptions ); |
34 | 29 | file_put_contents( |
35 | | - $this->target . '/'. $this->group->getMessageFile( $code ), |
| 30 | + $target . '/'. $this->group->getMessageFile( $code ), |
36 | 31 | $task->execute() |
37 | 32 | ); |
38 | 33 | } |
— | — | @@ -41,40 +36,35 @@ |
42 | 37 | |
43 | 38 | class StandardExtensionExporter implements MessageExporter { |
44 | 39 | protected $group = null; |
45 | | - protected $languages = array(); |
46 | | - protected $target = null; |
47 | | - |
48 | | - public function __construct( $group, $languages, $target ) { |
| 40 | + public function __construct( MessageGroup $group ) { |
49 | 41 | $this->group = $group; |
50 | | - $this->languages = $languages; |
51 | | - $this->target = $target; |
52 | 42 | } |
53 | 43 | |
54 | | - public function export() { |
| 44 | + public function export( Array $languages, $target ) { |
55 | 45 | global $wgTranslateExtensionDirectory; |
56 | 46 | $filename = $this->group->getMessageFile( '' /* Ignored */ ); |
57 | 47 | list( $header, $sections ) = $this->parse( $wgTranslateExtensionDirectory . '/' . $filename ); |
58 | 48 | $output = $header; |
59 | | - $output .= $this->exportLanguage( 'en', $sections ); |
60 | | - $output .= $this->exportLanguage( 'qqq', $sections ); |
| 49 | + $output .= $this->exportLanguage( 'en', $languages, $sections ); |
| 50 | + $output .= $this->exportLanguage( 'qqq', $languages, $sections ); |
61 | 51 | |
62 | | - $languages = Language::getLanguageNames( false ); |
63 | | - foreach ( array_keys( $languages ) as $code ) { |
| 52 | + $__languages = Language::getLanguageNames( false ); |
| 53 | + foreach ( array_keys( $__languages ) as $code ) { |
64 | 54 | if ( $code === 'en' || $code === 'qqq' ) continue; |
65 | | - $output .= $this->exportLanguage( $code, $sections ); |
| 55 | + $output .= $this->exportLanguage( $code, $languages, $sections ); |
66 | 56 | } |
67 | 57 | |
68 | 58 | // The hacks, aka copies of another languages |
69 | 59 | $output .= implode( '', $sections ); |
70 | 60 | |
71 | | - $targetFile = $this->target . '/' . $filename; |
| 61 | + $targetFile = $target . '/' . $filename; |
72 | 62 | wfMkdirParents( dirname( $targetFile ) ); |
73 | 63 | file_put_contents( $targetFile, $output ); |
74 | 64 | } |
75 | 65 | |
76 | | - protected function exportLanguage( $code, &$sections ) { |
| 66 | + private function exportLanguage( $code, Array $languages, &$sections ) { |
77 | 67 | $output = ''; |
78 | | - if ( in_array( $code, $this->languages ) ) { |
| 68 | + if ( in_array( $code, $languages ) ) { |
79 | 69 | $taskOptions = new TaskOptions( $code, 0, 0, 0, null ); |
80 | 70 | $task = TranslateTasks::getTask( 'export-to-file' ); |
81 | 71 | $task->init( $this->group, $taskOptions ); |
— | — | @@ -91,11 +81,13 @@ |
92 | 82 | } |
93 | 83 | |
94 | 84 | protected function parse( $filename ) { |
| 85 | + $var = $this->group->getVariableName(); |
| 86 | + |
95 | 87 | $data = file_get_contents( $filename ); |
96 | 88 | |
97 | | - $headerP = ' |
| 89 | + $headerP = " |
98 | 90 | .*? # Ungreedily eat header |
99 | | - \$messages \s* = \s* array\(\);'; |
| 91 | + \$$var \s* = \s* array\(\);"; |
100 | 92 | /* |
101 | 93 | * x to have nice syntax |
102 | 94 | * u for utf-8 |
— | — | @@ -111,7 +103,7 @@ |
112 | 104 | list( , $header, $data) = $matches; |
113 | 105 | |
114 | 106 | $sectionP = '(?: /\*\* .*? \*/ )? (?: ( [^\n]*? \S;\n ) | (?: .*? \n\);\n\n ) )'; |
115 | | - $codeP = '\$messages\[\' (.*?) \'\]'; |
| 107 | + $codeP = "\$$var\[' (.*?) '\]"; |
116 | 108 | |
117 | 109 | $sectionMatches = array(); |
118 | 110 | if ( !preg_match_all( "~$sectionP~xsu", $data, $sectionMatches, PREG_SET_ORDER ) ) { |
— | — | @@ -135,4 +127,31 @@ |
136 | 128 | |
137 | 129 | return array( $header, $sections ); |
138 | 130 | } |
| 131 | +} |
| 132 | + |
| 133 | +class MultipleFileExtensionExporter extends StandardExtensionExporter { |
| 134 | + protected $header = ''; |
| 135 | + |
| 136 | + public function export( Array $languages, $target ) { |
| 137 | + foreach ( $languages as $code ) { |
| 138 | + $output = "<?php\n"; |
| 139 | + $output .= $this->header; |
| 140 | + $output .= $this->exportLang( $code ); |
| 141 | + |
| 142 | + $filename = $this->group->getMessageFile( $code ); |
| 143 | + $targetFile = $target . '/' . $filename; |
| 144 | + wfMkdirParents( dirname( $targetFile ) ); |
| 145 | + file_put_contents( $targetFile, $output ); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private function exportLang( $code ) { |
| 150 | + $output = ''; |
| 151 | + $taskOptions = new TaskOptions( $code, 0, 0, 0, null ); |
| 152 | + $task = TranslateTasks::getTask( 'export-to-file' ); |
| 153 | + $task->init( $this->group, $taskOptions ); |
| 154 | + $output = $task->execute() . "\n"; |
| 155 | + return $output; |
| 156 | + } |
| 157 | + |
139 | 158 | } |
\ No newline at end of file |
Index: trunk/extensions/Translate/Translate.php |
— | — | @@ -36,7 +36,9 @@ |
37 | 37 | |
38 | 38 | $wgAutoloadClasses['CoreExporter'] = $dir . 'Exporters.php'; |
39 | 39 | $wgAutoloadClasses['StandardExtensionExporter'] = $dir . 'Exporters.php'; |
| 40 | +$wgAutoloadClasses['MultipleFileExtensionExporter'] = $dir . 'Exporters.php'; |
40 | 41 | |
| 42 | + |
41 | 43 | $wgAutoloadClasses['TranslateEditAddons'] = $dir . 'TranslateEditAddons.php'; |
42 | 44 | $wgAutoloadClasses['languages'] = $IP . '/maintenance/language/languages.inc'; |
43 | 45 | $wgAutoloadClasses['MessageWriter'] = $IP . '/maintenance/language/writeMessagesArray.inc'; |
Index: trunk/extensions/Translate/export.php |
— | — | @@ -50,10 +50,8 @@ |
51 | 51 | } |
52 | 52 | |
53 | 53 | if ( $group->canExportToFile() ) { |
54 | | - $class = $group->getFileExporter(); |
55 | | - $exporter = new $class( $group, $langs, $options['target'] ); |
| 54 | + $exporter = $group->getFileExporter(); |
| 55 | + $exporter->export( $langs, $options['target'] ); |
56 | 56 | } else { |
57 | 57 | echo "Cannot export to file\n\n"; |
58 | | -} |
59 | | - |
60 | | -$exporter->export(); |
\ No newline at end of file |
| 58 | +} |
\ No newline at end of file |
Index: trunk/extensions/Translate/MessageGroups.php |
— | — | @@ -63,7 +63,7 @@ |
64 | 64 | * Returns a class name of file exporter. |
65 | 65 | */ |
66 | 66 | public function getFileExporter() { |
67 | | - return $this->fileExporter; |
| 67 | + return new $this->fileExporter( $this ); |
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
— | — | @@ -340,6 +340,10 @@ |
341 | 341 | |
342 | 342 | public function getMessageFile( $code ) { return $this->messageFile; } |
343 | 343 | |
| 344 | + public function getVariableName() { |
| 345 | + return $this->arrName ? $this->arrName : 'messages'; |
| 346 | + } |
| 347 | + |
344 | 348 | public function getMessage( $key, $code ) { |
345 | 349 | $this->load( $code ); |
346 | 350 | return isset( $this->mcache[$code][$key] ) ? $this->mcache[$code][$key] : null; |
— | — | @@ -1081,7 +1085,7 @@ |
1082 | 1086 | } |
1083 | 1087 | |
1084 | 1088 | class FlaggedRevsMessageGroup extends MultipleFileMessageGroup { |
1085 | | - protected $fileExporter = null; |
| 1089 | + protected $fileExporter = 'MultipleFileExtensionExporter'; |
1086 | 1090 | protected $label = 'Flagged Revs'; |
1087 | 1091 | protected $id = 'ext-flaggedrevs'; |
1088 | 1092 | |