Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
AmdFFS.php
Go to the documentation of this file.
1<?php
42
50class AmdFFS extends SimpleFFS {
51
56 public static function isValid( $data ) {
57 $data = self::extractMessagePart( $data );
58 return is_array( FormatJson::decode( $data, /*as array*/true ) );
59 }
60
61 public function getFileExtensions() {
62 return [ '.js' ];
63 }
64
69 public function readFromVariable( $data ) {
70 $authors = self::extractAuthors( $data );
71 $data = self::extractMessagePart( $data );
72 $messages = (array)FormatJson::decode( $data, /*as array*/true );
73 $metadata = [];
74
75 // Take care of regular language bundles, as well as the root bundle.
76 if ( isset( $messages['root'] ) ) {
77 $messages = $this->group->getMangler()->mangleArray( $messages['root'] );
78 } else {
79 $messages = $this->group->getMangler()->mangleArray( $messages );
80 }
81
82 return [
83 'MESSAGES' => $messages,
84 'AUTHORS' => $authors,
85 'METADATA' => $metadata,
86 ];
87 }
88
93 protected function writeReal( MessageCollection $collection ) {
94 $messages = [];
95 $mangler = $this->group->getMangler();
96
98 foreach ( $collection as $key => $m ) {
99 $value = $m->translation();
100 if ( $value === null ) {
101 continue;
102 }
103
104 if ( $m->hasTag( 'fuzzy' ) ) {
105 $value = str_replace( TRANSLATE_FUZZY, '', $value );
106 }
107
108 $key = $mangler->unmangle( $key );
109 $messages[$key] = $value;
110 }
111
112 // Do not create empty files
113 if ( !count( $messages ) ) {
114 return '';
115 }
116 $header = $this->header( $collection->code, $collection->getAuthors() );
117 return $header . FormatJson::encode( $messages, "\t", FormatJson::UTF8_OK ) . ");\n";
118 }
119
124 private static function extractMessagePart( $data ) {
125 // Find the start and end of the data section (enclosed in the define function call).
126 $dataStart = strpos( $data, 'define(' ) + 6;
127 $dataEnd = strrpos( $data, ')' );
128
129 // Strip everything outside of the data section.
130 return substr( $data, $dataStart + 1, $dataEnd - $dataStart - 1 );
131 }
132
137 private static function extractAuthors( $data ) {
138 preg_match_all( '~\n \* - (.+)~', $data, $result );
139 return $result[1];
140 }
141
147 private function header( $code, $authors ) {
148 global $wgSitename;
149
150 $name = Utilities::getLanguageName( $code );
151 $authorsList = $this->authorsList( $authors );
152
153 return <<<EOT
160 define(
161 EOT;
162 }
163
168 private function authorsList( array $authors ) {
169 if ( $authors === [] ) {
170 return '';
171 }
172
173 $prefix = ' * - ';
174 $authorList = implode( "\n$prefix", $authors );
175 return " * Translators:\n$prefix$authorList";
176 }
177}
AmdFFS implements a message format where messages are encoded as key-value pairs in JSON objects wrap...
Definition AmdFFS.php:50
readFromVariable( $data)
Definition AmdFFS.php:69
getFileExtensions()
Return the commonly used file extensions for these formats.
Definition AmdFFS.php:61
static isValid( $data)
Definition AmdFFS.php:56
writeReal(MessageCollection $collection)
Definition AmdFFS.php:93
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