Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
YamlFFS.php
1<?php
2
7
15class YamlFFS extends SimpleFFS implements MetaYamlSchemaExtender {
17 private $flattener;
18
20 public function __construct( FileBasedMessageGroup $group ) {
21 parent::__construct( $group );
22 $this->flattener = $this->getFlattener();
23 }
24
25 public function getFileExtensions() {
26 return [ '.yaml', '.yml' ];
27 }
28
33 public function readFromVariable( $data ) {
34 // Authors first.
35 $matches = [];
36 preg_match_all( '/^#\s*Author:\s*(.*)$/m', $data, $matches );
37 $authors = $matches[1];
38
39 // Then messages.
40 $messages = TranslateYaml::loadString( $data ) ?? [];
41
42 // Some groups have messages under language code
43 if ( isset( $this->extra['codeAsRoot'] ) ) {
44 $messages = array_shift( $messages ) ?? [];
45 }
46
47 $messages = $this->flatten( $messages );
48 $messages = $this->group->getMangler()->mangleArray( $messages );
49 foreach ( $messages as &$value ) {
50 $value = rtrim( $value, "\n" );
51 }
52
53 return [
54 'AUTHORS' => $authors,
55 'MESSAGES' => $messages,
56 ];
57 }
58
63 protected function writeReal( MessageCollection $collection ) {
64 $output = $this->doHeader( $collection );
65 $output .= $this->doAuthors( $collection );
66
67 $mangler = $this->group->getMangler();
68
69 $messages = [];
70
72 foreach ( $collection as $key => $m ) {
73 $key = $mangler->unmangle( $key );
74 $value = $m->translation();
75 $value = str_replace( TRANSLATE_FUZZY, '', $value );
76
77 if ( $value === '' ) {
78 continue;
79 }
80
81 $messages[$key] = $value;
82 }
83
84 if ( !count( $messages ) ) {
85 return false;
86 }
87
88 $messages = $this->unflatten( $messages );
89
90 // Some groups have messages under language code.
91 if ( isset( $this->extra['codeAsRoot'] ) ) {
92 $code = $this->group->mapCode( $collection->code );
93 $messages = [ $code => $messages ];
94 }
95
96 $output .= TranslateYaml::dump( $messages );
97
98 return $output;
99 }
100
105 protected function doHeader( MessageCollection $collection ) {
106 global $wgSitename;
107 global $wgTranslateYamlLibrary;
108
109 $code = $collection->code;
110 $name = Utilities::getLanguageName( $code );
111 $native = Utilities::getLanguageName( $code, $code );
112 $output = "# Messages for $name ($native)\n";
113 $output .= "# Exported from $wgSitename\n";
114
115 if ( isset( $wgTranslateYamlLibrary ) ) {
116 $output .= "# Export driver: $wgTranslateYamlLibrary\n";
117 }
118
119 return $output;
120 }
121
126 protected function doAuthors( MessageCollection $collection ) {
127 $output = '';
128 $authors = $collection->getAuthors();
129 $authors = $this->filterAuthors( $authors, $collection->code );
130
131 foreach ( $authors as $author ) {
132 $output .= "# Author: $author\n";
133 }
134
135 return $output;
136 }
137
144 protected function getFlattener() {
145 $nestingSeparator = $this->extra['nestingSeparator'] ?? '.';
146 $parseCLDRPlurals = $this->extra['parseCLDRPlurals'] ?? false;
147
148 // Instantiate helper class for flattening and unflattening nested arrays
149 return new ArrayFlattener( $nestingSeparator, $parseCLDRPlurals );
150 }
151
160 protected function flatten( $messages ) {
161 return $this->flattener->flatten( $messages );
162 }
163
172 protected function unflatten( $messages ) {
173 return $this->flattener->unflatten( $messages );
174 }
175
176 public function isContentEqual( $a, $b ) {
177 return $this->flattener->compareContent( $a, $b );
178 }
179
180 public static function getExtraSchema(): array {
181 $schema = [
182 'root' => [
183 '_type' => 'array',
184 '_children' => [
185 'FILES' => [
186 '_type' => 'array',
187 '_children' => [
188 'codeAsRoot' => [
189 '_type' => 'boolean',
190 ],
191 'nestingSeparator' => [
192 '_type' => 'text',
193 ],
194 'parseCLDRPlurals' => [
195 '_type' => 'boolean',
196 ]
197 ]
198 ]
199 ]
200 ]
201 ];
202
203 return $schema;
204 }
205}
This class implements default behavior for file based message groups.
This file contains the class for core message collections implementation.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.
Flattens message arrays for further processing.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30
filterAuthors(array $authors, $code)
Remove excluded authors.
Implements support for message storage in YAML format.
Definition YamlFFS.php:15
doAuthors(MessageCollection $collection)
Definition YamlFFS.php:126
__construct(FileBasedMessageGroup $group)
Definition YamlFFS.php:20
writeReal(MessageCollection $collection)
Definition YamlFFS.php:63
getFlattener()
Obtains object used to flatten and unflatten arrays.
Definition YamlFFS.php:144
isContentEqual( $a, $b)
Checks whether two strings are equal.
Definition YamlFFS.php:176
doHeader(MessageCollection $collection)
Definition YamlFFS.php:105
getFileExtensions()
Return the commonly used file extensions for these formats.
Definition YamlFFS.php:25
readFromVariable( $data)
Definition YamlFFS.php:33
flatten( $messages)
Flattens multidimensional array by using the path to the value as key with each individual key separa...
Definition YamlFFS.php:160
unflatten( $messages)
Performs the reverse operation of flatten.
Definition YamlFFS.php:172
static getExtraSchema()
Return a data structure that will be merged with the base schema.
Definition YamlFFS.php:180
Message groups are usually configured in YAML, though the actual storage format does not matter,...