Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
DtdFFS.php
Go to the documentation of this file.
1<?php
18class DtdFFS extends SimpleFFS {
19 public function getFileExtensions() {
20 return [ '.dtd' ];
21 }
22
27 public function readFromVariable( $data ) {
28 preg_match_all( ',# Author: ([^\n]+)\n,', $data, $matches );
29 $authors = $matches[1];
30
31 preg_match_all( ',<!ENTITY[ ]+([^ ]+)\s+"([^"]+)"[^>]*>,', $data, $matches );
32 list( , $keys, $messages ) = $matches;
33 $messages = array_combine(
34 $keys,
35 array_map(
36 static function ( $message ) {
37 return html_entity_decode( $message, ENT_QUOTES );
38 },
39 $messages
40 )
41 );
42
43 $messages = $this->group->getMangler()->mangleArray( $messages );
44
45 return [
46 'AUTHORS' => $authors,
47 'MESSAGES' => $messages,
48 ];
49 }
50
51 protected function writeReal( MessageCollection $collection ) {
52 $collection->loadTranslations();
53
54 $header = "<!--\n";
55 $header .= $this->doHeader( $collection );
56 $header .= $this->doAuthors( $collection );
57 $header .= "-->\n";
58
59 $output = '';
60 $mangler = $this->group->getMangler();
61
63 foreach ( $collection as $key => $m ) {
64 $key = $mangler->unmangle( $key );
65 $trans = $m->translation();
66 $trans = str_replace( TRANSLATE_FUZZY, '', $trans );
67
68 if ( $trans === '' ) {
69 continue;
70 }
71
72 $trans = str_replace( '"', '&quot;', $trans );
73 $output .= "<!ENTITY $key \"$trans\">\n";
74 }
75
76 if ( $output ) {
77 return $header . $output;
78 }
79
80 return false;
81 }
82
83 protected function doHeader( MessageCollection $collection ) {
84 global $wgSitename;
85
86 $code = $collection->code;
87 $name = TranslateUtils::getLanguageName( $code );
88 $native = TranslateUtils::getLanguageName( $code, $code );
89
90 $output = "# Messages for $name ($native)\n";
91 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable
92 $output .= "# Exported from $wgSitename\n\n";
93
94 return $output;
95 }
96
97 protected function doAuthors( MessageCollection $collection ) {
98 $output = '';
99 $authors = $collection->getAuthors();
100 $authors = $this->filterAuthors( $authors, $collection->code );
101
102 foreach ( $authors as $author ) {
103 $output .= "# Author: $author\n";
104 }
105
106 return $output;
107 }
108}
File format support for DTD.
Definition DtdFFS.php:18
writeReal(MessageCollection $collection)
Definition DtdFFS.php:51
readFromVariable( $data)
Definition DtdFFS.php:27
getFileExtensions()
Return the commonly used file extensions for these formats.
Definition DtdFFS.php:19
Core message collection class.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.
loadTranslations()
Loads all message data.
filterAuthors(array $authors, $code)
Remove excluded authors.