Revert "[BUGFIX] Invalid fallback for non-localized labels"
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / Parser / XliffParser.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization\Parser;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Parser for XLIFF file.
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 * @author Dominique Feyer <dfeyer@reelpeek.net>
35 */
36 class XliffParser extends \TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser {
37
38 /**
39 * Returns array representation of XML data, starting from a root node.
40 *
41 * @param SimpleXMLElement $root A root node
42 * @return array An array representing the parsed XML file
43 */
44 protected function doParsingFromRoot(\SimpleXMLElement $root) {
45 $parsedData = array();
46 $bodyOfFileTag = $root->file->body;
47 if ($bodyOfFileTag instanceof \SimpleXMLElement) {
48 foreach ($bodyOfFileTag->children() as $translationElement) {
49 if ($translationElement->getName() === 'trans-unit' && !isset($translationElement['restype'])) {
50 // If restype would be set, it could be metadata from Gettext to XLIFF conversion (and we don't need this data)
51 if ($this->languageKey === 'default') {
52 // Default language coming from an XLIFF template (no target element)
53 $parsedData[(string) $translationElement['id']][0] = array(
54 'source' => (string) $translationElement->source,
55 'target' => (string) $translationElement->source
56 );
57 } else {
58 // @todo Support "approved" attribute
59 $parsedData[(string) $translationElement['id']][0] = array(
60 'source' => (string) $translationElement->source,
61 'target' => (string) $translationElement->target
62 );
63 }
64 } elseif (($translationElement->getName() === 'group' && isset($translationElement['restype'])) && (string) $translationElement['restype'] === 'x-gettext-plurals') {
65 // This is a translation with plural forms
66 $parsedTranslationElement = array();
67 foreach ($translationElement->children() as $translationPluralForm) {
68 if ($translationPluralForm->getName() === 'trans-unit') {
69 // When using plural forms, ID looks like this: 1[0], 1[1] etc
70 $formIndex = substr((string) $translationPluralForm['id'], strpos((string) $translationPluralForm['id'], '[') + 1, -1);
71 if ($this->languageKey === 'default') {
72 // Default language come from XLIFF template (no target element)
73 $parsedTranslationElement[(int) $formIndex] = array(
74 'source' => (string) $translationPluralForm->source,
75 'target' => (string) $translationPluralForm->source
76 );
77 } else {
78 // @todo Support "approved" attribute
79 $parsedTranslationElement[(int) $formIndex] = array(
80 'source' => (string) $translationPluralForm->source,
81 'target' => (string) $translationPluralForm->target
82 );
83 }
84 }
85 }
86 if (!empty($parsedTranslationElement)) {
87 if (isset($translationElement['id'])) {
88 $id = (string) $translationElement['id'];
89 } else {
90 $id = (string) $translationElement->{'trans-unit'}[0]['id'];
91 $id = substr($id, 0, strpos($id, '['));
92 }
93 $parsedData[$id] = $parsedTranslationElement;
94 }
95 }
96 }
97 }
98 return $parsedData;
99 }
100
101 }
102
103
104 ?>