30 return is_array( FormatJson::decode( $data,
true ) );
35 parent::__construct( $group );
36 $this->flattener = $this->getFlattener();
48 $messages = (array)FormatJson::decode( $data,
true );
52 if ( isset( $messages[
'@metadata'][
'authors'] ) ) {
53 $authors = (array)$messages[
'@metadata'][
'authors'];
54 unset( $messages[
'@metadata'][
'authors'] );
57 if ( isset( $messages[
'@metadata'] ) ) {
58 $metadata = $messages[
'@metadata'];
61 unset( $messages[
'@metadata'] );
63 if ( $this->flattener ) {
64 $messages = $this->flattener->flatten( $messages );
67 $messages = $this->group->getMangler()->mangleArray( $messages );
70 'MESSAGES' => $messages,
71 'AUTHORS' => $authors,
72 'EXTRA' => [
'METADATA' => $metadata ],
81 $template = $this->
read( $collection->getLanguage() ) ?: [];
86 foreach ( $collection as $key => $m ) {
87 $value = $m->translation();
88 if ( $value ===
null ) {
92 if ( $m->hasTag(
'fuzzy' ) ) {
93 $value = str_replace( TRANSLATE_FUZZY,
'', $value );
96 $messages[$key] = $value;
100 if ( $messages === [] ) {
104 $template[
'MESSAGES'] = $messages;
105 $template[
'AUTHORS'] = $authors;
107 return $this->generateFile( $template );
110 public function generateFile( array $template ): string {
111 $messages = $template[
'MESSAGES'];
112 $authors = $template[
'AUTHORS'];
114 if ( $this->flattener ) {
115 $messages = $this->flattener->unflatten( $messages );
118 $mangler = $this->group->getMangler();
119 $messages = $mangler->unmangleArray( $messages );
121 if ( $this->extra[
'includeMetadata'] ??
true ) {
122 $metadata = $template[
'EXTRA'][
'METADATA'] ?? [];
123 $metadata[
'authors'] = $authors;
125 $messages = [
'@metadata' => $metadata ] + $messages;
128 return FormatJson::encode( $messages,
"\t", FormatJson::ALL_OK ) .
"\n";
131 protected function getFlattener() {
132 if ( !isset( $this->extra[
'nestingSeparator'] ) ) {
136 $parseCLDRPlurals = $this->extra[
'parseCLDRPlurals'] ??
false;
137 $flattener =
new ArrayFlattener( $this->extra[
'nestingSeparator'], $parseCLDRPlurals );
143 if ( $this->flattener ) {
144 return $this->flattener->compareContent( $a, $b );
146 return parent::isContentEqual( $a, $b );
150 public static function getExtraSchema() {
158 'nestingSeparator' => [
161 'parseCLDRPlurals' => [
162 '_type' =>
'boolean',
164 'includeMetadata' => [
165 '_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)
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...