Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
JavaScriptFFS.php
1<?php
2
4
9abstract class JavaScriptFFS extends SimpleFFS {
10 public function getFileExtensions() {
11 return [ '.js' ];
12 }
13
21 abstract protected function transformKey( $key );
22
29 abstract protected function header( $code, array $authors );
30
34 abstract protected function footer();
35
40 public function readFromVariable( $data ) {
41 /* Parse authors list */
42 $authors = preg_replace( "#/\* Translators\:\n(.*?)\n \*/(.*)#s", '$1', $data );
43 if ( $authors === $data ) {
44 $authors = [];
45 } else {
46 $authors = array_map(
47 static function ( $author ) {
48 // Each line should look like " * - Translatorname"
49 return substr( $author, 6 );
50 },
51 explode( "\n", $authors )
52 );
53 }
54
55 /* Pre-processing of messages */
56
60 $dataStart = strpos( $data, '{' );
61 $dataEnd = strrpos( $data, '}' );
62
66 $data = substr( $data, $dataStart + 1, $dataEnd - $dataStart - 1 );
67
71 $data = preg_replace( '#^(\s*?)//(.*?)$#m', '', $data );
72
76 $data = preg_replace( "#\'\,\n#", "\",\n", $data );
77
81 $data = trim( $data );
82
90 $data = explode( "\",\n", $data );
91
92 $messages = [];
93 foreach ( $data as $segment ) {
97 $segment .= '"';
98
102 $segment = preg_replace( '/"\s*\+\s*"/', '', $segment );
103
104 list( $key, $value ) = preg_split( '/:\s*[\'"]/', $segment, 2 );
105
109 $key = trim( trim( $key ), "'\"" );
110 $value = trim( trim( $value ), "'\"" );
111
115 $messages[$key] = self::unescapeJsString( $value );
116 }
117
118 $messages = $this->group->getMangler()->mangleArray( $messages );
119
120 return [
121 'AUTHORS' => $authors,
122 'MESSAGES' => $messages
123 ];
124 }
125
130 public function writeReal( MessageCollection $collection ) {
131 $header = $this->header( $collection->code, $collection->getAuthors() );
132
133 $mangler = $this->group->getMangler();
134
138 $body = '';
140 foreach ( $collection as $message ) {
141 if ( strlen( $message->translation() ) === 0 ) {
142 continue;
143 }
144
145 $key = $mangler->unmangle( $message->key() );
146 $key = $this->transformKey( self::escapeJsString( $key ) );
147
148 $translation = self::escapeJsString( $message->translation() );
149
150 $body .= "\t{$key}: \"{$translation}\",\n";
151 }
152
153 if ( strlen( $body ) === 0 ) {
154 return false;
155 }
156
160 $body = substr( $body, 0, -2 );
161 $body .= "\n";
162
163 return $header . $body . $this->footer();
164 }
165
170 protected function authorsList( array $authors ) {
171 if ( $authors === [] ) {
172 return '';
173 }
174
175 $authorsList = '';
176 foreach ( $authors as $author ) {
177 $authorsList .= " * - $author\n";
178 }
179
180 // Remove trailing newline, and return.
181 return substr( " * Translators:\n$authorsList", 0, -1 );
182 }
183
184 // See ECMA 262 section 7.8.4 for string literal format
185 private static $pairs = [
186 "\\" => "\\\\",
187 "\"" => "\\\"",
188 "'" => "\\'",
189 "\n" => "\\n",
190 "\r" => "\\r",
191
192 // To avoid closing the element or CDATA section.
193 '<' => "\\x3c",
194 '>' => "\\x3e",
195
196 // To avoid any complaints about bad entity refs.
197 '&' => "\\x26",
198
199 /*
200 * Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
201 * Encode certain Unicode formatting chars so affected
202 * versions of Gecko do not misinterpret our strings;
203 * this is a common problem with Farsi text.
204 */
205 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
206 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
207 ];
208
213 protected static function escapeJsString( $string ) {
214 return strtr( $string, self::$pairs );
215 }
216
221 protected static function unescapeJsString( $string ) {
222 return strtr( $string, array_flip( self::$pairs ) );
223 }
224}
Generic file format support for JavaScript formatted files.
transformKey( $key)
Message keys format.
getFileExtensions()
Return the commonly used file extensions for these formats.
static escapeJsString( $string)
header( $code, array $authors)
Header of message file.
authorsList(array $authors)
static unescapeJsString( $string)
writeReal(MessageCollection $collection)
footer()
Footer of message file.
readFromVariable( $data)
This file contains the class for core message collections implementation.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.