Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
131 lines (105 loc) · 5.26 KB

File metadata and controls

131 lines (105 loc) · 5.26 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// This script should be run after adding a new message to translate.
// It will ensure that all messages in "*.en.xlf" files are propagated to all languages.
// The resulting diff should then be submitted as a PR on the lowest maintained branch,
// possibly after using GPT to translate all the targets it contains
// (state="needs-review-translation" should then be used on corresponding target tags.)
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\MessageCatalogue;
require __DIR__.'/../vendor/autoload.php';
function dumpXliff1(string $defaultLocale, MessageCatalogue $messages, string $domain, ?\DOMElement $header = null)
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$xliff = $dom->appendChild($dom->createElement('xliff'));
$xliff->setAttribute('version', '1.2');
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
$xliffFile = $xliff->appendChild($dom->createElement('file'));
$xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
$xliffFile->setAttribute('target-language', 'no' === $messages->getLocale() ? 'nb' : str_replace('_', '-', $messages->getLocale()));
$xliffFile->setAttribute('datatype', 'plaintext');
$xliffFile->setAttribute('original', 'file.ext');
if (null !== $header) {
mergeDom($dom, $xliffFile, $header);
}
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
foreach ($messages->all($domain) as $source => $target) {
$translation = $dom->createElement('trans-unit');
$metadata = $messages->getMetadata($source, $domain);
$translation->setAttribute('id', $metadata['id']);
if (isset($metadata['resname'])) {
$translation->setAttribute('resname', $metadata['resname']);
}
$s = $translation->appendChild($dom->createElement('source'));
$s->appendChild($dom->createTextNode($source));
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
$targetElement = $dom->createElement('target');
if ('en' !== $messages->getLocale() && $target === $source && 'Error' !== $source) {
$targetElement->setAttribute('state', 'needs-translation');
}
if (isset($metadata['target-attributes'])) {
foreach ($metadata['target-attributes'] as $key => $value) {
$targetElement->setAttribute($key, $value);
}
}
$t = $translation->appendChild($targetElement);
$t->appendChild($text);
$xliffBody->appendChild($translation);
}
return preg_replace('/^ +/m', '$0$0', $dom->saveXML());
}
function mergeDom(\DOMDocument $dom, \DOMNode $tree, \DOMNode $input)
{
$new = $dom->createElement($input->tagName);
foreach ($input->attributes as $key => $value) {
$new->setAttribute($key, $value);
}
$tree->appendChild($new);
foreach ($input->childNodes as $child) {
if ($child instanceof \DOMText) {
$new->appendChild($dom->createTextNode(str_replace(' ', ' ', $child->textContent)));
} elseif ($child instanceof \DOMNode) {
mergeDom($dom, $new, $child);
} else {
// We just need to update our script to handle this node types
throw new \LogicException('Unsupported node type: '.get_class($child));
}
}
}
foreach (['Security/Core' => 'security', 'Form' => 'validators', 'Validator' => 'validators'] as $component => $domain) {
$dir = __DIR__.'/../src/Symfony/Component/'.$component.'/Resources/translations';
$enCatalogue = (new XliffFileLoader())->load($dir.'/'.$domain.'.en.xlf', 'en', $domain);
file_put_contents($dir.'/'.$domain.'.en.xlf', dumpXliff1('en', $enCatalogue, $domain));
$finder = new Finder();
foreach ($finder->files()->in($dir)->name('*.xlf') as $file) {
$locale = substr($file->getBasename(), 1 + strlen($domain), -4);
if ('en' === $locale) {
continue;
}
$catalogue = (new XliffFileLoader())->load($file, $locale, $domain);
$localeCatalogue = new MessageCatalogue($locale);
foreach ($enCatalogue->all($domain) as $resname => $source) {
$metadata = [];
if ($catalogue->defines($resname, $domain)) {
$translation = $catalogue->get($resname, $domain);
$metadata = $catalogue->getMetadata($resname, $domain);
} else {
$translation = $source;
}
$metadata['id'] = $enCatalogue->getMetadata($resname, $domain)['id'];
if ($resname !== $source) {
$metadata['resname'] = $resname;
}
$localeCatalogue->set($source, $translation, $domain);
$localeCatalogue->setMetadata($source, $metadata, $domain);
}
$inputDom = new \DOMDocument();
$inputDom->loadXML(file_get_contents($file->getRealPath()));
$header = null;
if (1 === $inputDom->getElementsByTagName('header')->count()) {
$header = $inputDom->getElementsByTagName('header')->item(0);
}
file_put_contents($file, dumpXliff1('en', $localeCatalogue, $domain, $header));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.