-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathExportTranslationsCommand.php
More file actions
213 lines (176 loc) · 7.38 KB
/
Copy pathExportTranslationsCommand.php
File metadata and controls
213 lines (176 loc) · 7.38 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Lexik\Bundle\TranslationBundle\Command;
use Lexik\Bundle\TranslationBundle\Manager\FileInterface;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
use Lexik\Bundle\TranslationBundle\Translation\Exporter\ExporterCollector;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Export translations from the database in to files.
*
* @author Cédric Girard <c.girard@lexik.fr>
*/
#[AsCommand(
name: 'lexik:translations:export',
description: 'Export translations from the database to files.',
help: <<<'HELP'
The <info>%command.name%</info> command exports translations from the database back to translation files.
You can filter the export by locales and domains:
<info>php %command.full_name% --locales=en,fr --domains=messages</info>
You can also specify a custom export path:
<info>php %command.full_name% --export-path=/path/to/translations</info>
By default, the command exports all translations. Use <comment>--override</comment> to export only modified translations.
HELP
)]
class ExportTranslationsCommand extends Command
{
private InputInterface $input;
private OutputInterface $output;
public function __construct(
private readonly StorageInterface $storage,
private readonly TranslatorInterface $translator,
private readonly FileSystem $fileSystem,
private readonly ExporterCollector $exporterCollector,
private readonly string $projectDir,
) {
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this->addOption(
'locales',
'l',
InputOption::VALUE_OPTIONAL,
'Only export files for given locales. e.g. "--locales=en,de"',
null
);
$this->addOption(
'domains',
'd',
InputOption::VALUE_OPTIONAL,
'Only export files for given domains. e.g. "--domains=messages,validators"',
null
);
$this->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Force the output format.', null);
$this->addOption(
'override',
'o',
InputOption::VALUE_NONE,
'Only export modified phrases (app/Resources/translations are exported fully anyway)'
);
$this->addOption('export-path', 'p', InputOption::VALUE_REQUIRED, 'Export files to given path.');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = $output;
$filesToExport = $this->getFilesToExport();
if (count($filesToExport) > 0) {
foreach ($filesToExport as $file) {
$this->exportFile($file);
}
return 0;
}
$this->output->writeln('<comment>No translation\'s files in the database.</comment>');
return 1;
}
/**
* Returns all file to export.
*
* @return array
*/
protected function getFilesToExport()
{
$locales = $this->input->getOption('locales') ? explode(',', (string)$this->input->getOption('locales')) : [];
$domains = $this->input->getOption('domains') ? explode(',', (string)$this->input->getOption('domains')) : [];
return $this->storage->getFilesByLocalesAndDomains($locales, $domains);
}
/**
* Get translations to export and export translations into a file.
*/
protected function exportFile(FileInterface $file)
{
$rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->projectDir;
$this->output->writeln(sprintf('<info># Exporting "%s/%s":</info>', $file->getPath(), $file->getName()));
$override = $this->input->getOption('override');
if (!$this->input->getOption('export-path')) {
// we only export updated translations in case of the file is located in vendor/
if ($override) {
$onlyUpdated = ('Resources/translations' !== $file->getPath());
} else {
$onlyUpdated = (str_contains((string)$file->getPath(), 'vendor/'));
}
} else {
$onlyUpdated = !$override;
}
$translations = $this->storage->getTranslationsFromFile($file, $onlyUpdated);
if (count($translations) < 1) {
$this->output->writeln('<comment>No translations to export.</comment>');
return;
}
$format = $this->input->getOption('format') ?: $file->getExtention();
// we don't write vendors file, translations will be exported in %kernel.root_dir%/Resources/translations
if (str_contains((string)$file->getPath(), 'vendor/') || $override) {
$outputPath = sprintf('%s/Resources/translations', $rootDir);
} else {
$outputPath = sprintf('%s/%s', $rootDir, $file->getPath());
}
$this->output->writeln(sprintf('<info># OutputPath "%s":</info>', $outputPath));
// ensure the path exists
if ($this->input->getOption('export-path')) {
if (!$this->fileSystem->exists($outputPath)) {
$this->fileSystem->mkdir($outputPath);
}
}
$outputFile = sprintf('%s/%s.%s.%s', $outputPath, $file->getDomain(), $file->getLocale(), $format);
$this->output->writeln(sprintf('<info># OutputFile "%s":</info>', $outputFile));
$translations = $this->mergeExistingTranslations($file, $outputFile, $translations);
$this->doExport($outputFile, $translations, $format);
}
/**
* If the output file exists we merge existing translations with those from the database.
*/
protected function mergeExistingTranslations(FileInterface $file, string $outputFile, array $translations): array
{
if (file_exists($outputFile) && method_exists($this->translator, 'getLoader')) {
$extension = pathinfo($outputFile, PATHINFO_EXTENSION);
$loader = $this->translator->getLoader($extension);
$messageCatalogue = $loader->load($outputFile, $file->getLocale(), $file->getDomain());
$translations = array_merge($messageCatalogue->all($file->getDomain()), $translations);
}
return $translations;
}
/**
* Export translations.
*
* @param string $outputFile
* @param array $translations
* @param string $format
*/
protected function doExport($outputFile, $translations, $format)
{
$this->output->writeln(sprintf('<comment>Output file: %s</comment>', $outputFile));
$this->output->write(sprintf('<comment>%d translations to export: </comment>', count($translations)));
try {
$exported = $this->exporterCollector->export(
$format,
$outputFile,
$translations
);
$this->output->writeln($exported ? '<comment>success</comment>' : '<error>fail</error>');
} catch (\Exception $e) {
$this->output->writeln(sprintf('<error>"%s"</error>', $e->getMessage()));
}
}
}