29 return is_array( FormatJson::decode( $data,
true ) );
34 parent::__construct( $group );
35 $this->flattener = $this->getFlattener();
47 $messages = (array)FormatJson::decode( $data,
true );
51 if ( isset( $messages[
'@metadata'][
'authors'] ) ) {
52 $authors = (array)$messages[
'@metadata'][
'authors'];
53 unset( $messages[
'@metadata'][
'authors'] );
56 if ( isset( $messages[
'@metadata'] ) ) {
57 $metadata = $messages[
'@metadata'];
60 unset( $messages[
'@metadata'] );
62 if ( $this->flattener ) {
63 $messages = $this->flattener->flatten( $messages );
66 $messages = $this->group->getMangler()->mangleArray( $messages );
69 'MESSAGES' => $messages,
70 'AUTHORS' => $authors,
71 'EXTRA' => [
'METADATA' => $metadata ],
85 foreach ( $collection as $key => $m ) {
86 $value = $m->translation();
87 if ( $value ===
null ) {
91 if ( $m->hasTag(
'fuzzy' ) ) {
92 $value = str_replace( TRANSLATE_FUZZY,
'', $value );
95 $messages[$key] = $value;
99 if ( $messages === [] ) {
103 $template[
'MESSAGES'] = $messages;
104 $template[
'AUTHORS'] = $authors;
106 return $this->generateFile( $template );
109 public function generateFile( array $template ): string {
110 $messages = $template[
'MESSAGES'];
111 $authors = $template[
'AUTHORS'];
113 if ( $this->flattener ) {
114 $messages = $this->flattener->unflatten( $messages );
117 $mangler = $this->group->getMangler();
118 $messages = $mangler->unmangleArray( $messages );
120 if ( $this->extra[
'includeMetadata'] ??
true ) {
121 $metadata = $template[
'EXTRA'][
'METADATA'] ?? [];
122 $metadata[
'authors'] = $authors;
124 $messages = [
'@metadata' => $metadata ] + $messages;
127 return FormatJson::encode( $messages,
"\t", FormatJson::ALL_OK ) .
"\n";
130 protected function getFlattener() {
131 if ( !isset( $this->extra[
'nestingSeparator'] ) ) {
135 $parseCLDRPlurals = $this->extra[
'parseCLDRPlurals'] ??
false;
136 $flattener =
new ArrayFlattener( $this->extra[
'nestingSeparator'], $parseCLDRPlurals );
142 if ( $this->flattener ) {
143 return $this->flattener->compareContent( $a, $b );
145 return parent::isContentEqual( $a, $b );
149 public static function getExtraSchema() {
157 'nestingSeparator' => [
160 'parseCLDRPlurals' => [
161 '_type' =>
'boolean',
163 'includeMetadata' => [
164 '_type' =>
'boolean',
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.
getFileExtensions()
Return the commonly used file extensions for these formats.
__construct(FileBasedMessageGroup $group)
isContentEqual( $a, $b)
Checks whether two strings are equal.
writeReal(MessageCollection $collection)
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...