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