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