2222
2323/**
2424 * Conversion of locallang*.[xml|php] files to locallang.xlf.
25- *
26- * @author Xavier Perseguers <xavier@typo3.org>
2725 */
2826class Converter
2927{
@@ -156,7 +154,7 @@ protected function localizedFileRef(string $fileRef, string $lang): string
156154 */
157155 protected function writeNewXliffFile (string $ xmlFile , string $ newFileName , string $ langKey ): string
158156 {
159- $ xml = $ this ->generateFileContent ($ xmlFile , $ langKey );
157+ $ xml = $ this ->generateFileContent ($ xmlFile , $ langKey, $ newFileName );
160158
161159 $ result = '' ;
162160 if (!@file_exists ($ newFileName )) {
@@ -167,69 +165,37 @@ protected function writeNewXliffFile(string $xmlFile, string $newFileName, strin
167165 return $ result ;
168166 }
169167
170- protected function generateFileContent (string $ xmlFile , string $ langKey ): string
168+ protected function generateFileContent (string $ xmlFile , string $ langKey, string $ newFileName ): string
171169 {
172170 // Initialize variables:
173- $ xml = [];
174171 $ LOCAL_LANG = $ this ->getCombinedTranslationFileContent ($ xmlFile );
172+ $ originalFile = $ this ->getOriginalFileName ($ newFileName , $ langKey );
175173
174+ $ xml = [];
176175 $ xml [] = '<?xml version="1.0" encoding="UTF-8"?> ' ;
177176 $ xml [] = '<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> ' ;
178- $ xml [] = ' <file source-language="en" '
179- . ($ langKey !== 'default ' ? ' target-language=" ' . $ langKey . '" ' : '' )
180- . ' datatype="plaintext" original="EXT: ' . $ this ->extension
181- . '/Resources/Private/Language/locallang.xlf" date=" '
182- . gmdate ('Y-m-d\TH:i:s\Z ' )
177+ $ xml [] = ' <file source-language="en" ' . ($ langKey !== 'default ' ? ' target-language=" ' . $ langKey . '" ' : '' )
178+ . ' datatype="plaintext" original=" ' . $ originalFile . '" date=" ' . gmdate ('Y-m-d\TH:i:s\Z ' )
183179 . '" product-name=" ' . $ this ->extension . '"> ' ;
184- $ xml [] = ' <header/> ' ;
185- $ xml [] = ' <body> ' ;
180+ $ xml [] = ' <body> ' ;
186181
187182 foreach ($ LOCAL_LANG [$ langKey ] as $ key => $ data ) {
188- if (is_array ($ data )) {
189- $ target = $ data [0 ]['target ' ] ?? '' ;
190- $ source = $ data [0 ]['source ' ] ?? $ target ;
191- } else {
192- $ source = $ LOCAL_LANG ['default ' ][$ key ];
193- $ target = $ data ;
194- }
195-
196- if (str_contains ($ source , chr (10 ))) {
197- $ preserve = 'xml:space="preserve" ' ;
198- } else {
199- $ preserve = '' ;
200- }
201-
202- if (empty ($ source )) {
203- $ source = '<source/> ' ;
204- } else {
205- $ source = '<source> ' . htmlspecialchars ($ source ) . '</source> ' ;
206- }
207-
208- if (empty ($ target )) {
209- $ target = '<target/> ' ;
210- } else {
211- $ target = '<target> ' . htmlspecialchars ($ target ) . '</target> ' ;
212- }
213-
214- if ($ langKey === 'default ' ) {
215- $ xml [] = ' <trans-unit id=" ' . $ key . '" resname=" ' . $ key . '" ' . $ preserve . '> ' ;
216- $ xml [] = ' ' . $ source ;
217- } else {
218- $ xml [] = ' <trans-unit id=" ' . $ key . '" resname=" ' . $ key . '" ' . $ preserve
219- . ' approved="yes"> ' ;
220- $ xml [] = ' ' . $ source ;
221- $ xml [] = ' ' . $ target ;
222- }
223- $ xml [] = ' </trans-unit> ' ;
183+ $ xml [] = (string )(new TransUnit ($ data , $ key , $ langKey , $ LOCAL_LANG ));
224184 }
225185
226- $ xml [] = ' </body> ' ;
227- $ xml [] = ' </file> ' ;
186+ $ xml [] = ' </body> ' ;
187+ $ xml [] = ' </file> ' ;
228188 $ xml [] = '</xliff> ' ;
229189
230190 return implode (LF , $ xml );
231191 }
232192
193+ protected function getOriginalFileName (string $ filename , string $ langKey ): string
194+ {
195+ $ currentTargetName = str_replace ($ langKey . '. ' , '' , pathinfo ($ filename , PATHINFO_FILENAME ));
196+ return 'EXT: ' . $ this ->extension . '/Resources/Private/Language/ ' . $ currentTargetName . '.xlf ' ;
197+ }
198+
233199 /**
234200 * Reads/Requires locallang files and returns raw $LOCAL_LANG array
235201 *
@@ -265,23 +231,20 @@ protected function getCombinedTranslationFileContent(string $languageFile): arra
265231
266232 /**
267233 * Converts an XML string to a PHP array.
268- * This is the reverse function of array2xml()
234+ * This is the reverse function of GeneralUtility:: array2xml()
269235 * This is a wrapper for xml2arrayProcess that adds a two-level cache
270236 *
271237 * @param string $string XML content to convert into an array
272- * @param string $NSprefix The tag-prefix resolve, e.g. a namespace like "T3:"
238+ * @param string $namespacePrefix The tag-prefix resolve, e.g. a namespace like "T3:"
273239 * @param bool $reportDocTag If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
274240 *
275241 * @return array|string If the parsing had errors, a string with the error message is returned.
276242 * Otherwise, an array with the content.
277243 *
278- * @see array2xml(),xml2arrayProcess()
244+ * @see GeneralUtility:: array2xml(),GeneralUtility:: xml2arrayProcess()
279245 */
280- protected function xml2array (
281- string $ string ,
282- string $ NSprefix = '' ,
283- bool $ reportDocTag = false
284- ): array |string {
285- return GeneralUtility::xml2array ($ string , $ NSprefix , $ reportDocTag );
246+ protected function xml2array (string $ string , string $ namespacePrefix = '' , bool $ reportDocTag = false ): array |string
247+ {
248+ return GeneralUtility::xml2array ($ string , $ namespacePrefix , $ reportDocTag );
286249 }
287250}
0 commit comments