Index: trunk/extensions/Translate/ffs/Java.php |
— | — | @@ -9,8 +9,22 @@ |
10 | 10 | * @file |
11 | 11 | */ |
12 | 12 | |
| 13 | +/** |
| 14 | + * Reader for java property files. Not completely general, as it excepts two |
| 15 | + * comment sections at the top, separated by a blank line. |
| 16 | + * |
| 17 | + * Authors in the first section are detected, if prefixed with '# Author: '. |
| 18 | + * Second section (if any) is returned verbatim. |
| 19 | + */ |
13 | 20 | class JavaFormatReader extends SimpleFormatReader { |
14 | 21 | |
| 22 | + /** |
| 23 | + * Inherited from SimpleFormatReader, which parses whole header in one pass. |
| 24 | + * Basically the same, with different author prefix and separator between |
| 25 | + * headers and messages. |
| 26 | + * |
| 27 | + * FIXME: possible to refactor to reduce duplication? |
| 28 | + */ |
15 | 29 | protected function parseHeader() { |
16 | 30 | if ( $this->filename === false ) { |
17 | 31 | return; |
— | — | @@ -52,12 +66,20 @@ |
53 | 67 | $this->staticHeader = $staticHeader; |
54 | 68 | } |
55 | 69 | |
| 70 | + /** |
| 71 | + * Parses messages from lines key=value. Whitespace is trimmer around key and |
| 72 | + * values. New lines inside values have to be escaped as '\n'. Lines which do |
| 73 | + * not have = are ignored. Comments are designated by # at the start of the |
| 74 | + * line only. Values can have = characters, only the first one is considered |
| 75 | + * separator. |
| 76 | + */ |
56 | 77 | public function parseMessages( StringMangler $mangler ) { |
57 | 78 | if ( !file_exists( $this->filename ) ) { |
58 | 79 | return null; |
59 | 80 | } |
60 | 81 | |
61 | | - $lines = file( $this->filename ); |
| 82 | + # This format works nicely with line based parsing |
| 83 | + $lines = array_map( 'trim', file( $this->filename ) ); |
62 | 84 | if ( !$lines ) { return null; } |
63 | 85 | |
64 | 86 | $messages = array(); |
— | — | @@ -65,15 +87,20 @@ |
66 | 88 | foreach ( $lines as $line ) { |
67 | 89 | if ( $line === '' || !strpos( $line, '=' ) || $line[0] === '#' ) { continue; } |
68 | 90 | list( $key, $value ) = explode( '=', $line, 2 ); |
69 | | - $messages[$mangler->mangle($key)] = trim($value); |
| 91 | + $messages[$mangler->mangle(trim($key))] = trim($value); |
70 | 92 | } |
71 | 93 | return $messages; |
72 | 94 | } |
73 | 95 | } |
74 | 96 | |
75 | | - |
| 97 | +/** |
| 98 | + * Very simple writer for exporting messages to Java property files from wiki. |
| 99 | + */ |
76 | 100 | class JavaFormatWriter extends SimpleFormatWriter { |
77 | 101 | |
| 102 | + /** |
| 103 | + * Inherited. Very simplistic header with timestamp. |
| 104 | + */ |
78 | 105 | public function makeHeader( $handle, $code ) { |
79 | 106 | global $wgSitename; |
80 | 107 | list( $name, $native ) = $this->getLanguageNames($code); |
— | — | @@ -89,9 +116,14 @@ |
90 | 117 | ); |
91 | 118 | } |
92 | 119 | |
| 120 | + /** |
| 121 | + * Inherited. Exports messages as lines of format key=value. |
| 122 | + */ |
93 | 123 | protected function exportMessages( $handle, array $messages ) { |
94 | 124 | foreach ( $messages as $key => $value ) { |
| 125 | + # Make sure we don't slip newlines trough... it would be fatal |
95 | 126 | $value = str_replace( "\n", '\\n', $value ); |
| 127 | + # No pretty alignment here, sorry |
96 | 128 | fwrite( $handle, "$key=$value\n" ); |
97 | 129 | } |
98 | 130 | } |