TYPO3 CMS  TYPO3_7-6
XliffParser.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
28  protected function doParsingFromRoot(\SimpleXMLElement $root)
29  {
30  $parsedData = [];
31  $bodyOfFileTag = $root->file->body;
32  if ($bodyOfFileTag instanceof \SimpleXMLElement) {
33  foreach ($bodyOfFileTag->children() as $translationElement) {
35  if ($translationElement->getName() === 'trans-unit' && !isset($translationElement['restype'])) {
36  // If restype would be set, it could be metadata from Gettext to XLIFF conversion (and we don't need this data)
37  if ($this->languageKey === 'default') {
38  // Default language coming from an XLIFF template (no target element)
39  $parsedData[(string)$translationElement['id']][0] = [
40  'source' => (string)$translationElement->source,
41  'target' => (string)$translationElement->source
42  ];
43  } else {
44  // @todo Support "approved" attribute
45  $parsedData[(string)$translationElement['id']][0] = [
46  'source' => (string)$translationElement->source,
47  'target' => (string)$translationElement->target
48  ];
49  }
50  } elseif ($translationElement->getName() === 'group' && isset($translationElement['restype']) && (string)$translationElement['restype'] === 'x-gettext-plurals') {
51  // This is a translation with plural forms
52  $parsedTranslationElement = [];
53  foreach ($translationElement->children() as $translationPluralForm) {
55  if ($translationPluralForm->getName() === 'trans-unit') {
56  // When using plural forms, ID looks like this: 1[0], 1[1] etc
57  $formIndex = substr((string)$translationPluralForm['id'], strpos((string)$translationPluralForm['id'], '[') + 1, -1);
58  if ($this->languageKey === 'default') {
59  // Default language come from XLIFF template (no target element)
60  $parsedTranslationElement[(int)$formIndex] = [
61  'source' => (string)$translationPluralForm->source,
62  'target' => (string)$translationPluralForm->source
63  ];
64  } else {
65  // @todo Support "approved" attribute
66  $parsedTranslationElement[(int)$formIndex] = [
67  'source' => (string)$translationPluralForm->source,
68  'target' => (string)$translationPluralForm->target
69  ];
70  }
71  }
72  }
73  if (!empty($parsedTranslationElement)) {
74  if (isset($translationElement['id'])) {
75  $id = (string)$translationElement['id'];
76  } else {
77  $id = (string)$translationElement->{'trans-unit'}[0]['id'];
78  $id = substr($id, 0, strpos($id, '['));
79  }
80  $parsedData[$id] = $parsedTranslationElement;
81  }
82  }
83  }
84  }
85  return $parsedData;
86  }
87 }