Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
AppleFFS.php
1<?php
2
5
17class AppleFFS extends SimpleFFS {
18 public function supportsFuzzy() {
19 return 'write';
20 }
21
22 public function getFileExtensions() {
23 return [ '.strings' ];
24 }
25
31 public function readFromVariable( $data ) {
32 $lines = explode( "\n", $data );
33 $authors = $messages = [];
34 $linecontinuation = false;
35
36 $value = '';
37 foreach ( $lines as $line ) {
38 $line = trim( $line );
39 if ( $linecontinuation ) {
40 if ( strpos( $line, '*/' ) !== false ) {
41 $linecontinuation = false;
42 }
43 } else {
44 if ( $line === '' ) {
45 continue;
46 }
47
48 if ( substr( $line, 0, 2 ) === '//' ) {
49 // Single-line comment
50 $match = [];
51 $ok = preg_match( '~//\s*Author:\s*(.*)~', $line, $match );
52 if ( $ok ) {
53 $authors[] = $match[1];
54 }
55 continue;
56 }
57
58 if ( substr( $line, 0, 2 ) === '/*' ) {
59 if ( strpos( $line, '*/', 2 ) === false ) {
60 $linecontinuation = true;
61 }
62 continue;
63 }
64
65 list( $key, $value ) = static::readRow( $line );
66 $messages[$key] = $value;
67 }
68 }
69
70 $messages = $this->group->getMangler()->mangleArray( $messages );
71
72 return [
73 'AUTHORS' => $authors,
74 'MESSAGES' => $messages,
75 ];
76 }
77
85 public static function readRow( $line ) {
86 $match = [];
87 if ( preg_match( '/^"((?:\\\"|[^"])*)"\s*=\s*"((?:\\\"|[^"])*)"\s*;\s*$/', $line, $match ) ) {
88 $key = self::unescapeString( $match[1] );
89 $value = self::unescapeString( $match[2] );
90 if ( $key === '' ) {
91 throw new MWException( "Empty key in line $line" );
92 }
93 return [ $key, $value ];
94 } else {
95 throw new MWException( "Unrecognized line format: $line" );
96 }
97 }
98
103 protected function writeReal( MessageCollection $collection ) {
104 $header = $this->doHeader( $collection );
105 $header .= $this->doAuthors( $collection );
106 $header .= "\n";
107
108 $output = '';
109 $mangler = $this->group->getMangler();
110
112 foreach ( $collection as $key => $m ) {
113 $value = $m->translation();
114 $value = str_replace( TRANSLATE_FUZZY, '', $value );
115
116 if ( $value === '' ) {
117 continue;
118 }
119
120 // Just to give an overview of translation quality.
121 if ( $m->hasTag( 'fuzzy' ) ) {
122 $output .= "// Fuzzy\n";
123 }
124
125 $key = $mangler->unmangle( $key );
126 $output .= static::writeRow( $key, $value );
127 }
128
129 if ( $output ) {
130 $data = $header . $output;
131 } else {
132 $data = $header;
133 }
134
135 return $data;
136 }
137
145 public static function writeRow( $key, $value ) {
146 return self::quoteString( $key ) . ' = ' . self::quoteString( $value ) . ';' . "\n";
147 }
148
155 protected static function quoteString( $str ) {
156 return '"' . self::escapeString( $str ) . '"';
157 }
158
165 protected static function escapeString( $str ) {
166 $str = addcslashes( $str, '\\"' );
167 $str = str_replace( "\n", '\\n', $str );
168 return $str;
169 }
170
179 protected static function unescapeString( $str ) {
180 return stripcslashes( $str );
181 }
182
187 protected function doHeader( MessageCollection $collection ) {
188 if ( isset( $this->extra['header'] ) ) {
189 $output = $this->extra['header'];
190 } else {
191 global $wgSitename;
192
193 $code = $collection->code;
194 $name = Utilities::getLanguageName( $code );
195 $native = Utilities::getLanguageName( $code, $code );
196 $output = "// Messages for $name ($native)\n";
197 $output .= "// Exported from $wgSitename\n";
198 }
199
200 return $output;
201 }
202
207 protected function doAuthors( MessageCollection $collection ) {
208 $output = '';
209 $authors = $collection->getAuthors();
210 $authors = $this->filterAuthors( $authors, $collection->code );
211
212 foreach ( $authors as $author ) {
213 $output .= "// Author: $author\n";
214 }
215
216 return $output;
217 }
218
219 public static function getExtraSchema() {
220 $schema = [
221 'root' => [
222 '_type' => 'array',
223 '_children' => [
224 'FILES' => [
225 '_type' => 'array',
226 '_children' => [
227 'header' => [
228 '_type' => 'text',
229 ],
230 ]
231 ]
232 ]
233 ]
234 ];
235
236 return $schema;
237 }
238}
AppleFFS class implements support for Apple .strings files.
Definition AppleFFS.php:17
static readRow( $line)
Parses non-empty strings file row to key and value.
Definition AppleFFS.php:85
doAuthors(MessageCollection $collection)
Definition AppleFFS.php:207
writeReal(MessageCollection $collection)
Definition AppleFFS.php:103
static writeRow( $key, $value)
Writes well-formed properties file row with key and value.
Definition AppleFFS.php:145
static escapeString( $str)
Escape Obj-C-style strings; use backslash-escapes etc.
Definition AppleFFS.php:165
static unescapeString( $str)
Unescape Obj-C-style strings; can include backslash-escapes.
Definition AppleFFS.php:179
supportsFuzzy()
Query the capabilities of this FFS.
Definition AppleFFS.php:18
static quoteString( $str)
Quote and escape Obj-C-style strings for .strings format.
Definition AppleFFS.php:155
getFileExtensions()
Return the commonly used file extensions for these formats.
Definition AppleFFS.php:22
doHeader(MessageCollection $collection)
Definition AppleFFS.php:187
readFromVariable( $data)
Definition AppleFFS.php:31
This file contains the class for core message collections implementation.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30
filterAuthors(array $authors, $code)
Remove excluded authors.