Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
JsonFFS.php
Go to the documentation of this file.
1<?php
11
20class JsonFFS extends SimpleFFS {
22 private $flattener;
23
28 public static function isValid( $data ) {
29 return is_array( FormatJson::decode( $data, /*as array*/true ) );
30 }
31
33 public function __construct( FileBasedMessageGroup $group ) {
34 parent::__construct( $group );
35 $this->flattener = $this->getFlattener();
36 }
37
38 public function getFileExtensions() {
39 return [ '.json' ];
40 }
41
46 public function readFromVariable( $data ) {
47 $messages = (array)FormatJson::decode( $data, /*as array*/true );
48 $authors = [];
49 $metadata = [];
50
51 if ( isset( $messages['@metadata']['authors'] ) ) {
52 $authors = (array)$messages['@metadata']['authors'];
53 unset( $messages['@metadata']['authors'] );
54 }
55
56 if ( isset( $messages['@metadata'] ) ) {
57 $metadata = $messages['@metadata'];
58 }
59
60 unset( $messages['@metadata'] );
61
62 if ( $this->flattener ) {
63 $messages = $this->flattener->flatten( $messages );
64 }
65
66 $messages = $this->group->getMangler()->mangleArray( $messages );
67
68 return [
69 'MESSAGES' => $messages,
70 'AUTHORS' => $authors,
71 'EXTRA' => [ 'METADATA' => $metadata ],
72 ];
73 }
74
79 protected function writeReal( MessageCollection $collection ) {
80 $template = $this->read( $collection->getLanguage() ) ?: [];
81 $authors = $this->filterAuthors( $collection->getAuthors(), $collection->getLanguage() );
82 $messages = [];
83
85 foreach ( $collection as $key => $m ) {
86 $value = $m->translation();
87 if ( $value === null ) {
88 continue;
89 }
90
91 if ( $m->hasTag( 'fuzzy' ) ) {
92 $value = str_replace( TRANSLATE_FUZZY, '', $value );
93 }
94
95 $messages[$key] = $value;
96 }
97
98 // Do not create files without translations
99 if ( $messages === [] ) {
100 return '';
101 }
102
103 $template['MESSAGES'] = $messages;
104 $template['AUTHORS'] = $authors;
105
106 return $this->generateFile( $template );
107 }
108
109 public function generateFile( array $template ): string {
110 $messages = $template['MESSAGES'];
111 $authors = $template['AUTHORS'];
112
113 if ( $this->flattener ) {
114 $messages = $this->flattener->unflatten( $messages );
115 }
116
117 $mangler = $this->group->getMangler();
118 $messages = $mangler->unmangleArray( $messages );
119
120 if ( $this->extra['includeMetadata'] ?? true ) {
121 $metadata = $template['EXTRA']['METADATA'] ?? [];
122 $metadata['authors'] = $authors;
123
124 $messages = [ '@metadata' => $metadata ] + $messages;
125 }
126
127 return FormatJson::encode( $messages, "\t", FormatJson::ALL_OK ) . "\n";
128 }
129
130 protected function getFlattener() {
131 if ( !isset( $this->extra['nestingSeparator'] ) ) {
132 return null;
133 }
134
135 $parseCLDRPlurals = $this->extra['parseCLDRPlurals'] ?? false;
136 $flattener = new ArrayFlattener( $this->extra['nestingSeparator'], $parseCLDRPlurals );
137
138 return $flattener;
139 }
140
141 public function isContentEqual( $a, $b ) {
142 if ( $this->flattener ) {
143 return $this->flattener->compareContent( $a, $b );
144 } else {
145 return parent::isContentEqual( $a, $b );
146 }
147 }
148
149 public static function getExtraSchema() {
150 $schema = [
151 'root' => [
152 '_type' => 'array',
153 '_children' => [
154 'FILES' => [
155 '_type' => 'array',
156 '_children' => [
157 'nestingSeparator' => [
158 '_type' => 'text',
159 ],
160 'parseCLDRPlurals' => [
161 '_type' => 'boolean',
162 ],
163 'includeMetadata' => [
164 '_type' => 'boolean',
165 ]
166 ]
167 ]
168 ]
169 ]
170 ];
171
172 return $schema;
173 }
174}
This class implements default behavior for file based message groups.
JsonFFS implements a message format where messages are encoded as key-value pairs in JSON objects.
Definition JsonFFS.php:20
getFileExtensions()
Return the commonly used file extensions for these formats.
Definition JsonFFS.php:38
readFromVariable( $data)
Definition JsonFFS.php:46
static isValid( $data)
Definition JsonFFS.php:28
__construct(FileBasedMessageGroup $group)
Definition JsonFFS.php:33
isContentEqual( $a, $b)
Checks whether two strings are equal.
Definition JsonFFS.php:141
writeReal(MessageCollection $collection)
Definition JsonFFS.php:79
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.
read( $code)
Reads messages from the file in a given language and returns an array of AUTHORS, MESSAGES and possib...
Definition SimpleFFS.php:97