blob: b8a109a41ffe76bdab49d543502cdd18124b2c5e [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Translation\Dumper;
13
14use Symfony\Component\Translation\Exception\InvalidArgumentException;
15use Symfony\Component\Translation\MessageCatalogue;
16
17/**
18 * XliffFileDumper generates xliff files from a message catalogue.
19 *
20 * @author Michel Salib <michelsalib@hotmail.com>
21 */
22class XliffFileDumper extends FileDumper
23{
24 /**
25 * {@inheritdoc}
26 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010027 public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020028 {
29 $xliffVersion = '1.2';
30 if (\array_key_exists('xliff_version', $options)) {
31 $xliffVersion = $options['xliff_version'];
32 }
33
34 if (\array_key_exists('default_locale', $options)) {
35 $defaultLocale = $options['default_locale'];
36 } else {
37 $defaultLocale = \Locale::getDefault();
38 }
39
40 if ('1.2' === $xliffVersion) {
41 return $this->dumpXliff1($defaultLocale, $messages, $domain, $options);
42 }
43 if ('2.0' === $xliffVersion) {
44 return $this->dumpXliff2($defaultLocale, $messages, $domain);
45 }
46
47 throw new InvalidArgumentException(sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion));
48 }
49
50 /**
51 * {@inheritdoc}
52 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010053 protected function getExtension(): string
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020054 {
55 return 'xlf';
56 }
57
58 private function dumpXliff1(string $defaultLocale, MessageCatalogue $messages, ?string $domain, array $options = [])
59 {
60 $toolInfo = ['tool-id' => 'symfony', 'tool-name' => 'Symfony'];
61 if (\array_key_exists('tool_info', $options)) {
62 $toolInfo = array_merge($toolInfo, $options['tool_info']);
63 }
64
65 $dom = new \DOMDocument('1.0', 'utf-8');
66 $dom->formatOutput = true;
67
68 $xliff = $dom->appendChild($dom->createElement('xliff'));
69 $xliff->setAttribute('version', '1.2');
70 $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
71
72 $xliffFile = $xliff->appendChild($dom->createElement('file'));
73 $xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
74 $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
75 $xliffFile->setAttribute('datatype', 'plaintext');
76 $xliffFile->setAttribute('original', 'file.ext');
77
78 $xliffHead = $xliffFile->appendChild($dom->createElement('header'));
79 $xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
80 foreach ($toolInfo as $id => $value) {
81 $xliffTool->setAttribute($id, $value);
82 }
83
84 $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
85 foreach ($messages->all($domain) as $source => $target) {
86 $translation = $dom->createElement('trans-unit');
87
88 $translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
89 $translation->setAttribute('resname', $source);
90
91 $s = $translation->appendChild($dom->createElement('source'));
92 $s->appendChild($dom->createTextNode($source));
93
94 // Does the target contain characters requiring a CDATA section?
95 $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
96
97 $targetElement = $dom->createElement('target');
98 $metadata = $messages->getMetadata($source, $domain);
99 if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
100 foreach ($metadata['target-attributes'] as $name => $value) {
101 $targetElement->setAttribute($name, $value);
102 }
103 }
104 $t = $translation->appendChild($targetElement);
105 $t->appendChild($text);
106
107 if ($this->hasMetadataArrayInfo('notes', $metadata)) {
108 foreach ($metadata['notes'] as $note) {
109 if (!isset($note['content'])) {
110 continue;
111 }
112
113 $n = $translation->appendChild($dom->createElement('note'));
114 $n->appendChild($dom->createTextNode($note['content']));
115
116 if (isset($note['priority'])) {
117 $n->setAttribute('priority', $note['priority']);
118 }
119
120 if (isset($note['from'])) {
121 $n->setAttribute('from', $note['from']);
122 }
123 }
124 }
125
126 $xliffBody->appendChild($translation);
127 }
128
129 return $dom->saveXML();
130 }
131
132 private function dumpXliff2(string $defaultLocale, MessageCatalogue $messages, ?string $domain)
133 {
134 $dom = new \DOMDocument('1.0', 'utf-8');
135 $dom->formatOutput = true;
136
137 $xliff = $dom->appendChild($dom->createElement('xliff'));
138 $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
139 $xliff->setAttribute('version', '2.0');
140 $xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
141 $xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
142
143 $xliffFile = $xliff->appendChild($dom->createElement('file'));
144 if (str_ends_with($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
145 $xliffFile->setAttribute('id', substr($domain, 0, -\strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX)).'.'.$messages->getLocale());
146 } else {
147 $xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
148 }
149
150 foreach ($messages->all($domain) as $source => $target) {
151 $translation = $dom->createElement('unit');
152 $translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
153
154 if (\strlen($source) <= 80) {
155 $translation->setAttribute('name', $source);
156 }
157
158 $metadata = $messages->getMetadata($source, $domain);
159
160 // Add notes section
161 if ($this->hasMetadataArrayInfo('notes', $metadata)) {
162 $notesElement = $dom->createElement('notes');
163 foreach ($metadata['notes'] as $note) {
164 $n = $dom->createElement('note');
165 $n->appendChild($dom->createTextNode($note['content'] ?? ''));
166 unset($note['content']);
167
168 foreach ($note as $name => $value) {
169 $n->setAttribute($name, $value);
170 }
171 $notesElement->appendChild($n);
172 }
173 $translation->appendChild($notesElement);
174 }
175
176 $segment = $translation->appendChild($dom->createElement('segment'));
177
178 $s = $segment->appendChild($dom->createElement('source'));
179 $s->appendChild($dom->createTextNode($source));
180
181 // Does the target contain characters requiring a CDATA section?
182 $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
183
184 $targetElement = $dom->createElement('target');
185 if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
186 foreach ($metadata['target-attributes'] as $name => $value) {
187 $targetElement->setAttribute($name, $value);
188 }
189 }
190 $t = $segment->appendChild($targetElement);
191 $t->appendChild($text);
192
193 $xliffFile->appendChild($translation);
194 }
195
196 return $dom->saveXML();
197 }
198
199 private function hasMetadataArrayInfo(string $key, array $metadata = null): bool
200 {
201 return is_iterable($metadata[$key] ?? null);
202 }
203}