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