[英]Symfony convert XLIFF files to another format (e.g. JSON or CSV)
I am using XLIFF files to handle Symfony 5.4 translations but my client would like to convert them to CSV or JSON.我正在使用 XLIFF 文件来处理 Symfony 5.4 翻译,但我的客户想将它们转换为 CSV 或 JSON。
Is it possible to convert my existing files to another format?是否可以将我现有的文件转换为另一种格式? I don't want to use the
extract
or update
command because this would re-generate the translations.我不想使用
extract
或update
命令,因为这会重新生成翻译。 I would prefer to convert my existing ones in another format.我更愿意将我现有的转换为另一种格式。
I also tried external tools such as xliff-to-json
but they didn't work.我还尝试了
xliff-to-json
等外部工具,但它们没有用。
Since I couldn't find a suitable tool I created a console command which converts from XLIFF to CSV:由于找不到合适的工具,我创建了一个控制台命令,将 XLIFF 转换为 CSV:
This is the link to the Gist, please feel free to suggest edits: https://gist.github.com/lukepass/6955d0cf25c44138df24f605e53a96cb这是 Gist 的链接,请随时提出修改建议: https://gist.github.com/lukepass/6955d0cf25c44138df24f605e53a96cb
<?php
namespace App\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Loader\XliffFileLoader;
#[AsCommand(
name: 'app:convert-translations',
description: 'Converts translation files from XLIFF to CSV.'
)]
class ConvertTranslationsCommand extends Command
{
private string $projectDir;
public function __construct(string $projectDir)
{
// best practices recommend to call the parent constructor first and
// then set your own properties. That wouldn't work in this case
// because configure() needs the properties set in this constructor
$this->projectDir = $projectDir;
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('locale', InputArgument::REQUIRED, 'Locale')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
/** @var string $locale */
$locale = $input->getArgument('locale');
$translationsDir = $this->projectDir.DIRECTORY_SEPARATOR.'translations';
// takes all the XLIFF files in the translations directory using the Finder component
$finder = new Finder();
$finder->files()->in($translationsDir)->name('*.'.$locale.'.xlf');
if (!$finder->hasResults()) {
$io->error('No XLIFF files found in the translations directory.');
return Command::FAILURE;
}
// iterates over all the XLIFF files found and converts them to CSV
foreach ($finder as $file) {
$xliffFileLoader = new XliffFileLoader();
$messageCatalogue = $xliffFileLoader->load($file->getRealPath(), $locale);
$translations = [];
foreach ($messageCatalogue->all('messages') as $id => $translation) {
$translations[$id] = $translation;
}
// replaces the XLIFF file extension with '.csv'
$csvFilePath = str_replace('.xlf', '.csv', $file->getRealPath());
// creates the CSV file and adds the BOM (Byte Order Mark)
// this is required to make LibreOffice being able to open it
$csvFile = fopen($csvFilePath, 'w');
// writes the actual CSV contents using ';' as the delimiter
foreach ($translations as $id => $translation) {
fputcsv($csvFile, [$id, $translation], ';');
}
fclose($csvFile);
$io->success(sprintf('XLIFF file "%s" converted to CSV.', $file->getFilename()));
}
return Command::SUCCESS;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.