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