‪TYPO3CMS  9.5
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 
22 {
29  protected function ‪doParsingFromRoot(\SimpleXMLElement $root)
30  {
31  $parsedData = [];
32  $bodyOfFileTag = $root->file->body;
33  if ($bodyOfFileTag instanceof \SimpleXMLElement) {
34  foreach ($bodyOfFileTag->children() as $translationElement) {
36  if ($translationElement->getName() === 'trans-unit' && !isset($translationElement['restype'])) {
37  // If restype would be set, it could be metadata from Gettext to XLIFF conversion (and we don't need this data)
38  if ($this->languageKey === 'default') {
39  // Default language coming from an XLIFF template (no target element)
40  $parsedData[(string)$translationElement['id']][0] = [
41  'source' => (string)$translationElement->source,
42  'target' => (string)$translationElement->source
43  ];
44  } else {
45  // @todo Support "approved" attribute
46  $parsedData[(string)$translationElement['id']][0] = [
47  'source' => (string)$translationElement->source,
48  'target' => (string)$translationElement->target
49  ];
50  }
51  } elseif ($translationElement->getName() === 'group' && isset($translationElement['restype']) && (string)$translationElement['restype'] === 'x-gettext-plurals') {
52  // This is a translation with plural forms
53  $parsedTranslationElement = [];
54  foreach ($translationElement->children() as $translationPluralForm) {
56  if ($translationPluralForm->getName() === 'trans-unit') {
57  // When using plural forms, ID looks like this: 1[0], 1[1] etc
58  $formIndex = substr((string)$translationPluralForm['id'], strpos((string)$translationPluralForm['id'], '[') + 1, -1);
59  if ($this->languageKey === 'default') {
60  // Default language come from XLIFF template (no target element)
61  $parsedTranslationElement[(int)$formIndex] = [
62  'source' => (string)$translationPluralForm->source,
63  'target' => (string)$translationPluralForm->source
64  ];
65  } else {
66  // @todo Support "approved" attribute
67  $parsedTranslationElement[(int)$formIndex] = [
68  'source' => (string)$translationPluralForm->source,
69  'target' => (string)$translationPluralForm->target
70  ];
71  }
72  }
73  }
74  if (!empty($parsedTranslationElement)) {
75  if (isset($translationElement['id'])) {
76  $id = (string)$translationElement['id'];
77  } else {
78  $id = (string)$translationElement->{'trans-unit'}[0]['id'];
79  $id = substr($id, 0, strpos($id, '['));
80  }
81  $parsedData[$id] = $parsedTranslationElement;
82  }
83  }
84  }
85  }
86  return $parsedData;
87  }
88 }
‪TYPO3\CMS\Core\Localization\Parser\XliffParser\doParsingFromRoot
‪array doParsingFromRoot(\SimpleXMLElement $root)
Definition: XliffParser.php:29
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser
Definition: AbstractXmlParser.php:28
‪TYPO3\CMS\Core\Localization\Parser\XliffParser
Definition: XliffParser.php:22
‪TYPO3\CMS\Core\Localization\Parser
Definition: AbstractXmlParser.php:2