简体   繁体   English

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

[英]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.我不想使用extractupdate命令,因为这会重新生成翻译。 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.

相关问题 以有意义的格式(例如,csv,tab)导出SPSS输出吗? - Export SPSS output in a meaningful format (e.g. csv, tab)? PHP-将Unicode转换为CSV,例如汉字 - PHP - Convert Unicode for CSV, e.g. chinese characters 在HDFS上将CSV文件转换为另一种CSV格式 - Convert csv files to another csv format on hdfs CSV模块读入文件,位于其他文件夹(例如,我的下载文件夹)中 - CSV Module Reading In Files, which are in some other folder (e.g. my Downloads folder) 使用JavaScript使用UTF-8(例如希腊语)编码将JSON导出为CSV或Excel - Export JSON to CSV or Excel with UTF-8 (e.g. Greek) encoding using JavaScript 如何将一个列表(例如2和3)上的数字与另一个列表中的近似值(例如5)进行匹配? - How to match numbers on one list (e.g. 2 and 3) with the approximate sum on another list (e.g. 5)? 使用 KQL(kusto 查询语言)探索本地文件中的数据(例如 Excel、CSV、JSON 等) - Using KQL (kusto query language) to explore data from a local file (e.g. Excel, CSV, JSON etc.) 将 json 格式转换为 csv 格式 - convert json to csv format JQ,将CSV(父子格式)转换为JSON,另一个问题 - JQ, convert CSV (parent child format) to JSON, another questions 将 csv 转换为特定的 json 格式 - convert csv to specific json format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM