[BUGFIX] Remove XCLASS inclusion blocks from t3lib
[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 $parsedData[(string)$translationElement['id']][0] = array(
61 'source' => (string)$translationElement->source,
62 'target' => (string)$translationElement->target,
63 );
64 }
65 } elseif ($translationElement->getName() === 'group' && isset($translationElement['restype']) && (string)$translationElement['restype'] === 'x-gettext-plurals') {
66 // This is a translation with plural forms
67 $parsedTranslationElement = array();
68
69 foreach ($translationElement->children() as $translationPluralForm) {
70 if ($translationPluralForm->getName() === 'trans-unit') {
71 // When using plural forms, ID looks like this: 1[0], 1[1] etc
72 $formIndex = substr((string)$translationPluralForm['id'], strpos((string)$translationPluralForm['id'], '[') + 1, -1);
73
74 if ($this->languageKey === 'default') {
75 // Default language come from XLIFF template (no target element)
76 $parsedTranslationElement[(int)$formIndex] = array(
77 'source' => (string)$translationPluralForm->source,
78 'target' => (string)$translationPluralForm->source,
79 );
80 } else {
81 // @todo Support "approved" attribute
82 $parsedTranslationElement[(int)$formIndex] = array(
83 'source' => (string)$translationPluralForm->source,
84 'target' => (string)$translationPluralForm->target,
85 );
86 }
87 }
88 }
89
90 if (!empty($parsedTranslationElement)) {
91 if (isset($translationElement['id'])) {
92 $id = (string)$translationElement['id'];
93 } else {
94 $id = (string)($translationElement->{'trans-unit'}[0]['id']);
95 $id = substr($id, 0, strpos($id, '['));
96 }
97
98 $parsedData[$id] = $parsedTranslationElement;
99 }
100 }
101 }
102 }
103 return $parsedData;
104 }
105 }
106
107 ?>