8000 minor #53421 Add .github/sync-translations.php (nicolas-grekas) · symfony/symfony@b68e8a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b68e8a5

Browse files
committed
minor #53421 Add .github/sync-translations.php (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- Add .github/sync-translations.php | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT That's the script I used in #53404 With current branch numbers as example: when adding a new message to translate on 7.1, we would ask the author to add it to the "en.xlf" file on 5.4, to run this script, and only then to send the PR on 5.4 This would ensure new messages are duplicated in all languages. We could also run chatgpt on that PR to pre-fill all translations, building on #53417 Commits ------- 9c4cb74 Add .github/sync-translations.php
2 parents 22dc6a7 + 9c4cb74 commit b68e8a5

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

.github/sync-translations.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
// This script should be run after adding a new message to translate.
4+
// It will ensure that all messages in "*.en.xlf" files are propagated to all languages.
5+
// The resulting diff should then be submitted as a PR on the lowest maintained branch,
6+
// possibly after using GPT to translate all the targets it contains
7+
// (state="needs-review-translation" should then be used on corresponding target tags.)
8+
9+
use Symfony\Component\Finder\Finder;
10+
use Symfony\Component\Translation\Loader\XliffFileLoader;
11+
use Symfony\Component\Translation\MessageCatalogue;
12+
13+
require __DIR__.'/../vendor/autoload.php';
14+
15+
function dumpXliff1(string $defaultLocale, MessageCatalogue $messages, string $domain)
16+
{
17+
$dom = new \DOMDocument('1.0', 'utf-8');
18+
$dom->formatOutput = true;
19+
20+
$xliff = $dom->appendChild($dom->createElement('xliff'));
21+
$xliff->setAttribute('version', '1.2');
22+
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
23+
24+
$xliffFile = $xliff->appendChild($dom->createElement('file'));
25+
$xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
26+
$xliffFile->setAttribute('target-language', 'no' === $messages->getLocale() ? 'nb' : str_replace('_', '-', $messages->getLocale()));
27+
$xliffFile->setAttribute('datatype', 'plaintext');
28+
$xliffFile->setAttribute('original', 'file.ext');
29+
30+
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
31+
foreach ($messages->all($domain) as $source => $target) {
32+
$translation = $dom->createElement('trans-unit');
33+
$metadata = $messages->getMetadata($source, $domain);
34+
35+
$translation->setAttribute('id', $metadata['id']);
36+
37+
$s = $translation->appendChild($dom->createElement('source'));
38+
$s->appendChild($dom->createTextNode($source));
39+
40+
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);
41+
42+
$targetElement = $dom->createElement('target');
43+
44+
if ('en' !== $messages->getLocale() && $target === $source && 'Error' !== $source) {
45+
$targetElement->setAttribute('state', 'needs-translation');
46+
}
47+
if (isset($metadata['target-attributes'])) {
48+
foreach ($metadata['target-attributes'] as $key => $value) {
49+
$targetElement->setAttribute($key, $value);
50+
}
51+
}
52+
53+
$t = $translation->appendChild($targetElement);
54+
$t->appendChild($text);
55+
56+
$xliffBody->appendChild($translation);
57+
}
58+
59+
return preg_replace('/^ +/m', '$0$0', $dom->saveXML());
60+
}
61+
62+
63+
foreach (['Security/Core' => 'security', 'Form' => 'validators', 'Validator' => 'validators'] as $component => $domain) {
64+
$dir = __DIR__.'/../src/Symfony/Component/'.$component.'/Resources/translations';
65+
66+
$enCatalogue = (new XliffFileLoader())->load($dir.'/'.$domain.'.en.xlf', 'en', $domain);
67+
$finder = new Finder();
68+
69+
foreach ($finder->files()->in($dir)->name('*.xlf') as $file) {
70+
$locale = substr($file->getBasename(), 1 + strlen($domain), -4);
71+
72+
$catalogue = (new XliffFileLoader())->load($file, $locale, $domain);
73+
$localeCatalogue = new MessageCatalogue($locale);
74+
75+
foreach ($enCatalogue->all($domain) as $id => $translation) {
76+
$metadata = [];
77+
if ($catalogue->defines($id, $domain)) {
78+
$translation = $catalogue->get($id, $domain);
79+
$metadata = $catalogue->getMetadata($id, $domain);
80+
}
81+
$metadata['id'] = $enCatalogue->getMetadata($id, $domain)['id&# 5C9A 39;];
82+
$localeCatalogue->set($id, $translation, $domain);
83+
$localeCatalogue->setMetadata($id, $metadata, $domain);
84+
}
85+
86+
file_put_contents($file, dumpXliff1('en', $localeCatalogue, $domain));
87+
}
88+
}

0 commit comments

Comments
 (0)
0