QT翻译文件合并

需求来源:在旧的翻译文件合并为一个后(脚本参考 https://blog.csdn.net/qq_15821883/article/details/114577479),需要把已经翻译的文件覆盖到新文件中

一、通过打开文件的形式,把已经翻译的文件替换

左边添加源文件、右边添加目标文件,点击中间的按钮,执行操作

二、源码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>

#ifdef _MSC_VER
#		if _MSC_VER >= 1600
#			pragma execution_character_set("utf-8")
#		endif
#endif

static void writeText(QString filename, QStringList &content)
{
	QDir(filename).remove(filename);

	
	QFile file(filename);
	if (file.open(QFile::Append | QFile::ReadWrite))
	{
		for (int i = 0 ; i < content.size(); ++i)
		{
			QTextStream out(&file);
			out << content.at(i) << "\n";
			out.flush();
		}
		file.close();
	}

}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_strFilePath(QString())
{
    ui->setupUi(this);
    m_strSourceList.clear();
    m_strTargetList.clear();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_source_add_clicked()
{
    QStringList strList = QFileDialog::getOpenFileNames(this, tr("添加源文件"),  m_strFilePath, tr("翻译文件(") + "*.ts" + ")");
    if(strList.isEmpty())
    {
        return;
    }
	m_strFilePath = QFileInfo(strList.at(0)).absolutePath();
	
	for (auto var : strList)
	{
		if (m_strSourceList.contains(var))
		{
			continue;
		}
		m_strSourceList.append(var);

		ui->sources_widget->addItem(var);
	}
}

void MainWindow::on_source_delete_clicked()
{
	QListWidgetItem*item = ui->target_widget->currentItem();
	if (nullptr == item)
	{
		return;
	}
	QString strText = item->text();
	if (strText.isEmpty())
	{
		return;
	}
	ui->sources_widget->removeItemWidget(item);
	m_strSourceList.removeAll(strText);
}

void MainWindow::on_target_add_clicked()
{
	QStringList strList = QFileDialog::getOpenFileNames(this, tr("添加目标文件"), m_strFilePath, tr("翻译文件(") + "*.ts" + ")");
	if (strList.isEmpty())
	{
		return;
	}
	m_strFilePath = QFileInfo(strList.at(0)).absolutePath();

	for (auto var : strList)
	{
		if (m_strTargetList.contains(var))
		{
			continue;
		}
		m_strTargetList.append(var);

		ui->target_widget->addItem(var);
	}
}

void MainWindow::on_target_delete_clicked()
{
	QListWidgetItem*item = ui->target_widget->currentItem();
	if (nullptr == item)
	{
		return;
	}
	QString strText = item->text();
	if (strText.isEmpty())
	{
		return;
	}
	ui->target_widget->removeItemWidget(item);
	m_strTargetList.removeAll(strText);
}

void MainWindow::on_toolButton_start_clicked()
{
	if (m_strSourceList.isEmpty())
	{
		QMessageBox::information(this, tr("警告"), tr("源翻译文件列表为空"));
		return;
	}

	if (m_strTargetList.isEmpty())
	{
		QMessageBox::information(this, tr("警告"), tr("目标翻译文件列表为空"));
		return;
	}

	// 源文件
	QMap<QString, QString> m_translation;
	for (int i = 0 ; i < m_strSourceList.size(); ++i)
	{
		QFile file(m_strSourceList.at(i));
		if (file.open(QIODevice::ReadOnly | QIODevice::Text))
		{
			QTextStream stream(&file);

			while (!stream.atEnd())
			{
				QString lineStr = stream.readLine();
				lineStr.replace(" ", "");
				if (lineStr.isEmpty() || !lineStr.startsWith("<source>"))
				{
					continue;
				}
				lineStr.replace("<source>", "");
				lineStr.replace("</source>", "");
				// 获取到Key
				QString key = lineStr;
				if (stream.atEnd())
				{
					continue;
				}
				lineStr = stream.readLine();
				if (lineStr.isEmpty() || lineStr.indexOf("<translation>") < 0)
				{
					continue;
				}
				m_translation.insert(key, lineStr);
			}
			file.close();
		}
		else
		{
			QMessageBox::information(this, tr("错误"), tr("文件:%1,打开失败(不存在或者被占用)").arg(m_strSourceList.at(i)));
		}
	}

	for (int i = 0; i < m_strTargetList.size(); ++i)
	{
		QFile file(m_strTargetList.at(i));
		if (file.open(QIODevice::OpenModeFlag::ReadWrite))
		{
			QTextStream outStream(&file);
			QStringList strText;
			while (!outStream.atEnd())
			{
				QString lineStr = outStream.readLine();
				strText.append(lineStr);
				lineStr = lineStr.replace(" ", "");
				if (lineStr.isEmpty() || !lineStr.startsWith("<source>"))
				{
					continue;
				}
				lineStr.replace("<source>", "");
				lineStr.replace("</source>", "");
				QString key = lineStr;
				if (outStream.atEnd())
				{
					continue;
				}
				// 读一行
				lineStr = outStream.readLine();
				QString strTemp = lineStr;

				if (m_translation.contains(key))
				{
					strTemp = m_translation[key];

					if (strTemp.indexOf("</translation>") < 0)
					{
						strTemp += "</translation>";
					}
				}
				strText.append(strTemp);
			}
			file.close();
			writeText(m_strTargetList.at(i), strText);
		}
		else
		{
			QMessageBox::information(this, tr("错误"), tr("文件:%1,打开失败(不存在或者被占用)").arg(m_strTargetList.at(i)));
		}
	}
	QMessageBox::information(this, tr("消息"), tr("执行完成"));
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_source_add_clicked();
    void on_source_delete_clicked();
    void on_target_add_clicked();
    void on_target_delete_clicked();
    void on_toolButton_start_clicked();

private:
    Ui::MainWindow *ui;
    QList<QString>  m_strSourceList;        // 婧愭枃浠?
    QList<QString>  m_strTargetList;        // 鐩爣鏂囦欢
    QString         m_strFilePath;          // 涓婃鎵撳紑鏂囦欢璺緞
};

#endif // MAINWINDOW_H

注意:需要在main解决汉字乱码问题

    QApplication a(argc, argv);

	QTextCodec *codec = QTextCodec::codecForName("UTF-8");
	QTextCodec::setCodecForLocale(codec); //解决汉字乱码问题

    MainWindow w;
    w.show();

    return a.exec();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值