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