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