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