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