Symfony将XLIFF文件转换为另一种格式(例如JSON或CSV)

ipakzgxi  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(193)

我正在使用XLIFF文件来处理Symfony 5.4翻译,但我的客户希望将它们转换为CSV或JSON。
是否可以将现有文件转换为其他格式?我不想使用extractupdate命令,因为这会重新生成翻译。我希望将现有文件转换为其他格式。
我还尝试了xliff-to-json等外部工具,但它们不起作用。

oxf4rvwz

oxf4rvwz1#

因为我找不到合适的工具,所以我创建了一个控制台命令,用于将XLIFF转换为CSV:
这是要点的链接,请随时提出修改建议: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;
    }
}

相关问题