@@ -25,12 +25,45 @@ class XliffFileDumper extends FileDumper
25
25
*/
26
26
protected function formatCatalogue (MessageCatalogue $ messages , $ domain , array $ options = array ())
27
27
{
28
+ $ xliffVersion = '1.2 ' ;
29
+ if (array_key_exists ('xliff_version ' , $ options )) {
30
+ $ xliffVersion = $ options ['xliff_version ' ];
31
+ }
32
+
28
33
if (array_key_exists ('default_locale ' , $ options )) {
29
34
$ defaultLocale = $ options ['default_locale ' ];
30
35
} else {
31
36
$ defaultLocale = \Locale::getDefault ();
32
37
}
33
38
39
+ if ('1.2 ' === $ xliffVersion ) {
40
+ return $ this ->dumpXliff1 ($ defaultLocale , $ messages , $ domain , $ options );
41
+ }
42
+ if ('2.0 ' === $ xliffVersion ) {
43
+ return $ this ->dumpXliff2 ($ defaultLocale , $ messages , $ domain , $ options );
44
+ }
45
+
46
+ throw new \InvalidArgumentException (sprintf ('No support implemented for dumping XLIFF version "%s". ' , $ xliffVersion ));
47
+ }
48
+
49
+ /**
50
+ * {@inheritdoc}
51
+ */
52
+ protected function format (MessageCatalogue $ messages , $ domain )
53
+ {
54
+ return $ this ->formatCatalogue ($ messages , $ domain );
55
+ }
56
+
57
+ /**
58
+ * {@inheritdoc}
59
+ */
60
+ protected function getExtension ()
61
+ {
62
+ return 'xlf ' ;
63
+ }
64
+
65
+ private function dumpXliff1 ($ defaultLocale , MessageCatalogue $ messages , $ domain , array $ options = array ())
66
+ {
34
67
$ toolInfo = array ('tool-id ' => 'symfony ' , 'tool-name ' => 'Symfony ' );
35
68
if (array_key_exists ('tool_info ' , $ options )) {
36
69
$ toolInfo = array_merge ($ toolInfo , $ options ['tool_info ' ]);
@@ -103,20 +136,46 @@ protected function formatCatalogue(MessageCatalogue $messages, $domain, array $o
103
136
return $ dom ->saveXML ();
104
137
}
105
138
106
- /**
107
- * {@inheritdoc}
108
- */
109
- protected function format (MessageCatalogue $ messages , $ domain )
139
+ private function dumpXliff2 ($ defaultLocale , MessageCatalogue $ messages , $ domain , array $ options = array ())
110
140
{
111
- return $ this -> formatCatalogue ( $ messages , $ domain );
112
- }
141
+ $ dom = new \ DOMDocument ( ' 1.0 ' , ' utf-8 ' );
142
+ $ dom -> formatOutput = true ;
113
143
114
- /**
115
- * {@inheritdoc}
116
- */
117
- protected function getExtension ()
118
- {
119
- return 'xlf ' ;
144
+ $ xliff = $ dom ->appendChild ($ dom ->createElement ('xliff ' ));
145
+ $ xliff ->setAttribute ('xmlns ' , 'urn:oasis:names:tc:xliff:document:2.0 ' );
146
+ $ xliff ->setAttribute ('version ' , '2.0 ' );
147
+ $ xliff ->setAttribute ('srcLang ' , str_replace ('_ ' , '- ' , $ defaultLocale ));
148
+ $ xliff ->setAttribute ('trgLang ' , str_replace ('_ ' , '- ' , $ messages ->getLocale ()));
149
+
150
+ $ xliffFile = $ xliff ->appendChild ($ dom ->createElement ('file ' ));
151
+ $ xliffFile ->setAttribute ('id ' , $ domain .'. ' .$ messages ->getLocale ());
152
+
153
+ foreach ($ messages ->all ($ domain ) as $ source => $ target ) {
154
+ $ translation = $ dom ->createElement ('unit ' );
155
+ $ translation ->setAttribute ('id ' , md5 ($ source ));
156
+
157
+ $ segment = $ translation ->appendChild ($ dom ->createElement ('segment ' ));
158
+
159
+ $ s = $ segment ->appendChild ($ dom ->createElement ('source ' ));
160
+ $ s ->appendChild ($ dom ->createTextNode ($ source ));
161
+
162
+ // Does the target contain characters requiring a CDATA section?
163
+ $ text = 1 === preg_match ('/[&<>]/ ' , $ target ) ? $ dom ->createCDATASection ($ target ) : $ dom ->createTextNode ($ target );
164
+
165
+ $ targetElement = $ dom ->createElement ('target ' );
166
+ $ metadata = $ messages ->getMetadata ($ source , $ domain );
167
+ if ($ this ->hasMetadataArrayInfo ('target-attributes ' , $ metadata )) {
168
+ foreach ($ metadata ['target-attributes ' ] as $ name => $ value ) {
169
+ $ targetElement ->setAttribute ($ name , $ value );
170
+ }
171
+ }
172
+ $ t = $ segment ->appendChild ($ targetElement );
173
+ $ t ->appendChild ($ text );
174
+
175
+ $ xliffFile ->appendChild ($ translation );
176
+ }
177
+
178
+ return $ dom ->saveXML ();
120
179
}
121
180
122
181
/**
0 commit comments