Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
AmdFFS.php
Go to the documentation of this file.
1<?php
47class AmdFFS extends SimpleFFS {
48
53 public static function isValid( $data ) {
54 $data = self::extractMessagePart( $data );
55 return is_array( FormatJson::decode( $data, /*as array*/true ) );
56 }
57
58 public function getFileExtensions() {
59 return [ '.js' ];
60 }
61
66 public function readFromVariable( $data ) {
67 $authors = self::extractAuthors( $data );
68 $data = self::extractMessagePart( $data );
69 $messages = (array)FormatJson::decode( $data, /*as array*/true );
70 $metadata = [];
71
72 // Take care of regular language bundles, as well as the root bundle.
73 if ( isset( $messages['root'] ) ) {
74 $messages = $this->group->getMangler()->mangleArray( $messages['root'] );
75 } else {
76 $messages = $this->group->getMangler()->mangleArray( $messages );
77 }
78
79 return [
80 'MESSAGES' => $messages,
81 'AUTHORS' => $authors,
82 'METADATA' => $metadata,
83 ];
84 }
85
90 protected function writeReal( MessageCollection $collection ) {
91 $messages = [];
92 $mangler = $this->group->getMangler();
93
95 foreach ( $collection as $key => $m ) {
96 $value = $m->translation();
97 if ( $value === null ) {
98 continue;
99 }
100
101 if ( $m->hasTag( 'fuzzy' ) ) {
102 $value = str_replace( TRANSLATE_FUZZY, '', $value );
103 }
104
105 $key = $mangler->unmangle( $key );
106 $messages[$key] = $value;
107 }
108
109 // Do not create empty files
110 if ( !count( $messages ) ) {
111 return '';
112 }
113 $header = $this->header( $collection->code, $collection->getAuthors() );
114 return $header . FormatJson::encode( $messages, "\t", FormatJson::UTF8_OK ) . ");\n";
115 }
116
121 private static function extractMessagePart( $data ) {
122 // Find the start and end of the data section (enclosed in the define function call).
123 $dataStart = strpos( $data, 'define(' ) + 6;
124 $dataEnd = strrpos( $data, ')' );
125
126 // Strip everything outside of the data section.
127 return substr( $data, $dataStart + 1, $dataEnd - $dataStart - 1 );
128 }
129
134 private static function extractAuthors( $data ) {
135 preg_match_all( '~\n \* - (.+)~', $data, $result );
136 return $result[1];
137 }
138
144 private function header( $code, $authors ) {
145 global $wgSitename;
146
147 $name = TranslateUtils::getLanguageName( $code );
148 $authorsList = $this->authorsList( $authors );
149
150 return <<<EOT
157define(
158EOT;
159 }
160
165 private function authorsList( array $authors ) {
166 if ( $authors === [] ) {
167 return '';
168 }
169
170 $prefix = ' * - ';
171 $authorList = implode( "\n$prefix", $authors );
172 return " * Translators:\n$prefix$authorList";
173 }
174}
AmdFFS implements a message format where messages are encoded as key-value pairs in JSON objects wrap...
Definition AmdFFS.php:47
readFromVariable( $data)
Definition AmdFFS.php:66
getFileExtensions()
Return the commonly used file extensions for these formats.
Definition AmdFFS.php:58
static isValid( $data)
Definition AmdFFS.php:53
writeReal(MessageCollection $collection)
Definition AmdFFS.php:90
Core message collection class.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.
static getLanguageName( $code, $language='en')
Returns a localised language name.