ts与安卓语言文件string.xml的转换工具

原创 2014年09月02日 09:13:25
#include "xmlchange2.h"
#include <QtGui/QApplication>
//#include <QXmlDefaultHandler>
#include <QTextCodec>
#include <QTranslator>  


int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	//QString translatorFileName = "xmlchange2_zh.qm";  
	QTranslator *translator = new QTranslator(0);  
	if (translator->load(":/xmlchange2_zh.qm"))  
		a.installTranslator(translator);  

	xmlChange2 w;
	w.show();
	return a.exec();
}


xmlChange.h

#ifndef XMLCHANGE2_H
#define XMLCHANGE2_H

#include <QtGui/QMainWindow>
#include "ui_xmlchange2.h"
#include <QDomDocument>
#include <QXmlDefaultHandler>
#include <QSettings> 

class xmlChange2 : public QMainWindow, public Ui::xmlChange2Class
{
	Q_OBJECT

public:
	xmlChange2(QWidget *parent = 0, Qt::WFlags flags = 0);
	~xmlChange2();

	public slots:
		QString openSourceDir();
		QString openTargetDir();
		void on_actionOpen_triggered();

 		void xmlFileChange();

		void xmlFileUpdate();
 		void showCheckbox();
		void checkBoxAllCheck();
		bool isExistsFile(QString pathS,QString dirS,bool isXMLFlag);
		bool getXmlFileList(QString dirPath,QString dirName,QStringList xmlListHead,QStringList xmlListContext);
		void radioButtonCheck();
		void enableCheckbox();
		void updatePushButton();


public:
	QString loadPath;
	QSettings *configIni;
	void textEditOutShow(QString flagStr,QString textStr,int colorFlag=0);
	int checkboxCheck();
	QTextCodec *codec;
 private:
		QString sourceDir;
		QString targetDir;
		QGridLayout *gLayout;
		QString tsType;
		QTimer *timer;
		
};

#endif // XMLCHANGE2_H


xmlChange.cpp

#include "xmlchange2.h"
#include "xmlHandler.h"
#include "tsHandler.h"

#include <QtGui>
#include <QFile>
#include <QDir>
#include <QTimer>


xmlChange2::xmlChange2(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	setupUi(this);

	connect(pushButtonSource, SIGNAL(clicked()), this, SLOT(openSourceDir()));
	connect(pushButtonTarget, SIGNAL(clicked()), this, SLOT(openTargetDir()));

	connect(lineEditSource, SIGNAL(textChanged(QString)), this, SLOT(showCheckbox()));
	connect(lineEditTarget, SIGNAL(textChanged(QString)), this, SLOT(enableCheckbox()));

	connect(checkBoxAll, SIGNAL(clicked()), this, SLOT(checkBoxAllCheck()));

	connect(pushButtonXMLToTS, SIGNAL(clicked()), this, SLOT(xmlFileChange()));
	connect(pushButtonTSToXML, SIGNAL(clicked()), this, SLOT(xmlFileUpdate()));

	connect(radioButtonXMLToTS, SIGNAL(clicked()), this, SLOT(radioButtonCheck()));
	connect(radioButtonTSToXML, SIGNAL(clicked()), this, SLOT(radioButtonCheck()));

	gLayout = new QGridLayout;

	configIni = new QSettings("config.ini", QSettings::IniFormat);
	loadPath = configIni->value("/res/path").toString(); 
	if (!loadPath.isEmpty())
	{
		lineEditSource->setText(loadPath);
	}

	targetDir = configIni->value("/ts/path").toString(); 
	if (!targetDir.isEmpty())
	{
		lineEditTarget->setText(targetDir);
	}

	radioButtonXMLToTS->setChecked(TRUE);
	pushButtonTSToXML->setVisible(FALSE);
	//textEditOut->setOverwriteMode(TRUE);

	codec = QTextCodec::codecForLocale();

	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(updatePushButton()));
	timer->start();
}

xmlChange2::~xmlChange2()
{
	delete gLayout;
	delete configIni;
}



//检查可转换复选框,返回-1表示没有复选框或者复选框全没有选中,返回0表示有复选框,但是非全选,返回1表示复选框内容全选。
int xmlChange2::checkboxCheck()
{
	int ret=-1;

	if (gLayout->count()==0)
	{
		ret= -1;
	}else{

		int countCheckbox=0;
		for(int i=0;i<gLayout->count() ;i++) 
		{
			QCheckBox *widgetCheck = (QCheckBox *)gLayout->itemAt(i)->widget();

			if (widgetCheck->isChecked())
			{
				countCheckbox++;
			}
		}
		if (countCheckbox == 0)
			ret =-1;
		else if (countCheckbox == gLayout->count())
			ret = 1;
		else
			ret = 0;
	}
	return ret;
}


//根据计时器修改按钮的可用状态,当没有复选框被选中时,转换按钮为不可用,反之为可用
void xmlChange2::updatePushButton()
{
	int checkboxFlag = checkboxCheck();
	if (checkboxFlag==-1)
	{
		pushButtonXMLToTS->setEnabled(FALSE);
		pushButtonTSToXML->setEnabled(FALSE);
		if (radioButtonXMLToTS->isChecked())
		{
			checkBoxAll->setChecked(FALSE);
		}
	}else{

		if (radioButtonXMLToTS->isChecked())
		{
			if (checkboxFlag==1)
			{
				checkBoxAll->setChecked(TRUE);
			}else
				checkBoxAll->setChecked(FALSE);
			
		}
		pushButtonXMLToTS->setEnabled(TRUE);
		pushButtonTSToXML->setEnabled(TRUE);
	}

}

//单选按钮单击触发修改转换按钮和是否只提取未翻译字段复选框的可用状态
void xmlChange2::radioButtonCheck()
{
	if (radioButtonXMLToTS->isChecked ())
	{
		pushButtonXMLToTS->setVisible(TRUE);
		pushButtonTSToXML->setVisible(FALSE);
		checkBoxUnfinished->setEnabled(TRUE);
	}else if (radioButtonTSToXML->isChecked ())
	{
		pushButtonXMLToTS->setVisible(FALSE);
		pushButtonTSToXML->setVisible(TRUE);
		checkBoxUnfinished->setEnabled(FALSE);
		checkBoxUnfinished->setChecked(FALSE);
	}
	enableCheckbox();
}

QString xmlChange2::openSourceDir()
{
	QString tempStr = codec->toUnicode("请选择转换的工程(res)路径.");
	sourceDir = QFileDialog::getExistingDirectory(this, tempStr,"/home",
		QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks); 

	lineEditSource->setText(sourceDir);

	return sourceDir;

}

QString xmlChange2::openTargetDir()
{
	QString tempStr = codec->toUnicode("请选择目标存储路径.");
	targetDir = QFileDialog::getExistingDirectory(this, tempStr,"/home",
		QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks); 

	lineEditTarget->setText(targetDir);

	return targetDir;

}

void xmlChange2::on_actionOpen_triggered()
{
	if (lineEditSource->text().isEmpty())
	{
		openSourceDir();
	}

	if (lineEditTarget->text().isEmpty())
	{
		openTargetDir();
	}

}

//检查res路径里value目录下是否存在strings.xml文件,存在返回true,反之false
bool xmlChange2::isExistsFile(QString pathS,QString dirS,bool isXMLFlag)
{
	bool ret = false;
	QString pathTemp;
	pathTemp.append(pathS).append("/").append(dirS);
	if (isXMLFlag)
	{
		pathTemp.append("/strings.xml");
	}

	QFile file(pathTemp);
	ret = file.exists();
	file.close();
	return ret;
}

//根据ts保存路径去本地目录里面查找是否存在*.ts文件,刷新value复选框的可用和可选状态
void xmlChange2::enableCheckbox()
{
	if (!lineEditTarget->text().isEmpty())
	{
		configIni->setValue("/ts/path", lineEditTarget->text()); 
	}
	if (radioButtonXMLToTS->isChecked ())
	{
		checkBoxAll->setEnabled(TRUE);

		for(int i=0;i<gLayout->count() ;i++)
		{
			QCheckBox *cheeckTemp = (QCheckBox *)gLayout->itemAt(i)->widget();

			cheeckTemp->setEnabled(TRUE);

		}

	}else if (radioButtonTSToXML->isChecked ())
	{
		int checkFlag = 0;

		for(int i=0;i<gLayout->count() ;i++)
		{
			QCheckBox *checkTemp = (QCheckBox *)gLayout->itemAt(i)->widget();

			checkTemp->setEnabled(FALSE);

			checkTemp->setChecked(FALSE);
		}

		QString targetPath = lineEditTarget->text();

		QDir *dirRoot = new QDir(targetPath);
		dirRoot->setFilter(QDir::Files);
		QFileInfoList listDir = dirRoot->entryInfoList();

		for (int i = 0; i < listDir.size(); ++i) 
		{
			QFileInfo fileInfo = listDir.at(i);
			QString dirName = fileInfo.fileName();

			if (dirName.left(7)=="strings")
			{
				if (dirName.left(8)=="strings_")
				{
					int pos = dirName.lastIndexOf(".");
					QString posString = dirName.left(pos);
					QString typeTsName = posString.mid(8);

					QString dirNameTemp = QString("values-%1").arg(typeTsName);

					for(int i=0;i<gLayout->count() ;i++)
					{
						QCheckBox *checkTemp = (QCheckBox *)gLayout->itemAt(i)->widget();

						if (checkTemp->text()==dirNameTemp)
						{
							checkTemp->setEnabled(TRUE);
							checkTemp->setChecked(TRUE);
							checkFlag++;
							break;
						}	
					}
				}else if (dirName=="strings.ts")
				{
					for(int i=0;i<gLayout->count() ;i++)
					{
						QCheckBox *checkTemp = (QCheckBox *)gLayout->itemAt(i)->widget();

						if (checkTemp->text()=="values")
						{
							checkTemp->setEnabled(TRUE);
							checkTemp->setChecked(TRUE);
							checkFlag++;
							break;
						}	
					}
				}
			}
		}

		if (checkFlag == gLayout->count())
		{
			checkBoxAll->setChecked(TRUE);
			checkBoxAll->setEnabled(TRUE);
		}else{
			checkBoxAll->setChecked(FALSE);
			checkBoxAll->setEnabled(FALSE);
		}

		if (checkFlag==0)
		{
			pushButtonTSToXML->setEnabled(FALSE);
		}else{
			pushButtonTSToXML->setEnabled(TRUE);
		}
	}
}
//根据res目录刷新value的数量,存在strings.xml文件的才显示
void xmlChange2::showCheckbox()
{
	QString dirPath = lineEditSource->text();
	QDir *dirRoot = new QDir(dirPath);
	dirRoot->setFilter(QDir::Dirs| QDir::Hidden);
	QFileInfoList listDir = dirRoot->entryInfoList();

	int countPath=0;
	int rowT=0;
	int coloumT=0;

	while(gLayout->count() > 0) {
		QWidget* widget = gLayout->itemAt(0)->widget();
		gLayout->removeWidget(widget);
		delete widget;
	}

	gLayout->update();


	for (int i = 0; i < listDir.size(); ++i) {
		QFileInfo fileInfo = listDir.at(i);
		QString dirName = fileInfo.fileName();

		if (dirName.left(6)=="values")
		{
			bool isFile = false;

			isFile = isExistsFile(dirPath,dirName,TRUE);

			if (isFile)
			{
				QCheckBox *tempCheckbox = new QCheckBox(dirName);
				tempCheckbox->setChecked(true);

				coloumT = countPath%5;
				rowT = countPath/5;

				gLayout->addWidget(tempCheckbox,rowT,coloumT);  

				countPath++;
			}			
		}
	}

	if (countPath>0)
	{
		checkBoxAll->setChecked(TRUE);
		pushButtonXMLToTS->setEnabled(TRUE);
	}else
	{
		checkBoxAll->setChecked(FALSE);
		pushButtonXMLToTS->setEnabled(FALSE);
	}

	groupBox->setLayout(gLayout); 

	if (!dirPath.isEmpty())
	{
		configIni->setValue("/res/path", dirPath); 
	}

	enableCheckbox();
}

//全选按钮触发事件
void xmlChange2::checkBoxAllCheck()
{
	for(int i=0;i<gLayout->count() ;i++)
	{
		QCheckBox *cheeckTemp = (QCheckBox *)gLayout->itemAt(i)->widget();
		if (checkBoxAll->isChecked ())
		{
			cheeckTemp->setChecked(true);
		}else{
			cheeckTemp->setChecked(false);
		}
		
	}
}

//根据给定路径获取string.xml文件的字段内容
bool xmlChange2::getXmlFileList(QString dirPath,QString dirName,QStringList xmlListHead,QStringList xmlListContext)
{
	bool isFile = isExistsFile(dirPath,dirName,true);

	if (isFile)
	{
		QString pathFile = QString("%1/%2/strings.xml").arg(dirPath).arg(dirName);

		QFile file(pathFile);
		QXmlInputSource xmlInputSource(&file);

		QXmlSimpleReader reader;
		xmlHandler *myXMLHandler = new xmlHandler();
		myXMLHandler->textEditOut = textEditOut;

		reader.setContentHandler(myXMLHandler);
		reader.setErrorHandler(myXMLHandler);
		reader.parse(xmlInputSource);

		xmlListHead << myXMLHandler->xmlListHead;
		xmlListContext << myXMLHandler->xmlListContext;

		file.close();
		myXMLHandler->~xmlHandler();
	}

	return isFile;
}

//输出框格式
void xmlChange2::textEditOutShow(QString flagStr,QString textStr,int colorFlag)
{
	QString strTmp; 
	if (colorFlag==1)
	{
		strTmp=QString("<font color=red>%1: %2 -- %3</font>").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(flagStr).arg(textStr);
	}else{
		strTmp= QString("%1: %2 -- %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(flagStr).arg(textStr);
	}
	textEditOut->append(strTmp);

}


void xmlChange2::xmlFileChange()
{
	if (lineEditSource->text().isEmpty())
	{
		QString tempStr1 = codec->toUnicode("警告");
		QString tempStr2 = codec->toUnicode("请选择转换的工程(res)路径!");
		textEditOutShow(tempStr1,tempStr2,1);
		return;
	}

	if (lineEditTarget->text().isEmpty())
	{
		QString tempStr1 = codec->toUnicode("警告");
		QString tempStr2 = codec->toUnicode("请选择转换的工程(res)路径!");
		textEditOutShow(tempStr1,tempStr2,1);
		return;
	}

	timer->stop();

	textEditOut->clear();

	configIni->setValue("/ts/path", lineEditTarget->text()); 

	QString dirPath = lineEditSource->text();
	QDir *dirRoot = new QDir(dirPath);
	dirRoot->setFilter(QDir::Dirs| QDir::Hidden);
	QFileInfoList listDir = dirRoot->entryInfoList();

	int countFile =0;
	int countSuccess =0;
	int countError=0;
	QStringList errorList;
	QStringList existsNoList;

	QStringList zhFileHeadList;
	QStringList zhFileContextList;


	bool isFileZh = isExistsFile(dirPath,"values-zh",true);

	if (isFileZh)
	{
		QString pathFile = QString("%1/values-zh/strings.xml").arg(dirPath);

		QFile file(pathFile);
		QXmlInputSource xmlInputSource(&file);

		QXmlSimpleReader reader;
		xmlHandler *myXMLHandler = new xmlHandler();
		myXMLHandler->textEditOut = textEditOut;

		reader.setContentHandler(myXMLHandler);
		reader.setErrorHandler(myXMLHandler);
		reader.parse(xmlInputSource);

		zhFileHeadList = myXMLHandler->xmlListHead;
		zhFileContextList = myXMLHandler->xmlListContext;

		file.close();
		myXMLHandler->~xmlHandler();
	}


	QStringList integrationHeadList;
	QStringList integrationContextList;
	QStringList integrationZhList;


	QStringList enFileHeadList;
	QStringList enFileContextList;

	bool isFileEn = isExistsFile(dirPath,"values-en",true);

	if (isFileEn)
	{
		QString pathFile = QString("%1/values-en/strings.xml").arg(dirPath);

		QFile file(pathFile);
		QXmlInputSource xmlInputSource(&file);

		QXmlSimpleReader reader;
		xmlHandler *myXMLHandler = new xmlHandler();
		myXMLHandler->textEditOut = textEditOut;

		reader.setContentHandler(myXMLHandler);
		reader.setErrorHandler(myXMLHandler);
		reader.parse(xmlInputSource);

		enFileHeadList = myXMLHandler->xmlListHead;
		enFileContextList = myXMLHandler->xmlListContext;

		file.close();
		myXMLHandler->~xmlHandler();
	}

	for (int i = 0; i < listDir.size(); ++i) {
		QFileInfo fileInfo = listDir.at(i);
		QString dirName = fileInfo.fileName();

		if (dirName.left(6)=="values")
		{
			bool CheckFlag=false;
			for(int i=0;i<gLayout->count() ;i++) 
			{
				QCheckBox *widgetCheck = (QCheckBox *)gLayout->itemAt(i)->widget();

				if (dirName == widgetCheck->text()&& widgetCheck->isChecked())
				{
					CheckFlag = true;
					break;
				}
			}

			if (CheckFlag)
			{
				countFile++;
				QString pathFile = QString("%1/%2/strings.xml").arg(dirPath).arg(dirName);

				QFile file(pathFile);
				bool retExists = file.exists(pathFile);
				if (retExists)
				{
					QXmlInputSource xmlInputSource(&file);

					QString targetPath;
					targetPath.append(lineEditTarget->text()).append("/strings");

					if (dirName.left(7)=="values-")
					{
						tsType = dirName.mid(7);
						targetPath.append("_");

					}else 
					{
						tsType = "";
					}

					targetPath.append(tsType).append(".ts");

					QXmlSimpleReader reader;
					xmlHandler *myXMLHandler = new xmlHandler();
					myXMLHandler->textEditOut = textEditOut;
					myXMLHandler->targetPath = targetPath;
					myXMLHandler->filePathRet = pathFile;

					reader.setContentHandler(myXMLHandler);
					reader.setErrorHandler(myXMLHandler);
					bool errorRet = reader.parse(xmlInputSource);
					if (errorRet)
					{
						myXMLHandler->tsType = tsType;
						myXMLHandler->zhFileHeadList = zhFileHeadList;
						myXMLHandler->zhFileContextList = zhFileContextList;
						myXMLHandler->enFileHeadList = enFileHeadList;
						myXMLHandler->enFileContextList = enFileContextList;
						myXMLHandler->configIni = configIni;
						
						bool ret;
						if (checkBoxUnfinished->isChecked())
						{
							myXMLHandler->integrationHeadList = integrationHeadList;
							ret = myXMLHandler->saveTSUnfinishedFile();
							integrationHeadList = myXMLHandler->integrationHeadList;
							
						}else
							ret = myXMLHandler->saveTSFile();

						if (ret)
						{
							countSuccess++;
							if(tsType.isEmpty())
							{
								QString tempStr1 = codec->toUnicode("成功");
								QString tempStr2 = codec->toUnicode("--:有效文本项数%1项,其中%2项未翻译.").arg(myXMLHandler->xmlListHead.size()).arg(myXMLHandler->unfinishedCount);
								textEditOutShow(tempStr1,tempStr2);
							}else
							{
								QString tempStr1 = codec->toUnicode("成功");
								QString tempStr2 = codec->toUnicode("%1:有效文本项数%2项,其中%3项未翻译.").arg(tsType).arg(myXMLHandler->xmlListHead.size()).arg(myXMLHandler->unfinishedCount);
								textEditOutShow(tempStr1,tempStr2);
							}
						}else{
							countError++;
							QString tempStr1 = codec->toUnicode("错误");
							QString tempStr2 = codec->toUnicode("路径:%1").arg(pathFile);

							textEditOutShow(tempStr1,tempStr2,1);
						}
					}else{
						countError++;
					}

					
					myXMLHandler->~xmlHandler();
				}else{		
					existsNoList<<pathFile;
				}

				file.close();
			}				
		}

	}

	if (checkBoxUnfinished->isChecked() && integrationHeadList.size()>0 && enFileHeadList.size()>0)
	{
		xmlHandler *myXMLHandler = new xmlHandler();
		QString targetPath;
		targetPath.append(lineEditTarget->text()).append("/_en.ts");
		myXMLHandler->targetPath = targetPath;

		myXMLHandler->tsType = "en";
		myXMLHandler->zhFileHeadList = zhFileHeadList;
		myXMLHandler->zhFileContextList = zhFileContextList;
		myXMLHandler->enFileHeadList = enFileHeadList;
		myXMLHandler->enFileContextList = enFileContextList;
		myXMLHandler->integrationHeadList = integrationHeadList;
		myXMLHandler->configIni = configIni;

		myXMLHandler->saveTSUnfinishedStarFile();

	}

	if (countFile == countSuccess)
	{
		QString tempStr1 = codec->toUnicode("提示");
		QString tempStr2 = codec->toUnicode("所有文件转换完成!");
		textEditOutShow(tempStr1,tempStr2);
	}else{
// 		QString errorString;
// 		for (int i=0;i<errorList.size();i++)
// 		{
// 			errorString.append("--");
// 			errorString.append(errorList.at(i));
// 			errorString.append("\n");
// 		}
// 
// 		textEditOutShow(tr("Information"),QString(tr("XML file change false, the errors path: %1.")).arg(errorString));

		QString tempStr1 = codec->toUnicode("提示");
		QString tempStr2 = codec->toUnicode("%1个文件转换失败.").arg(countError);
		textEditOutShow(tempStr1,tempStr2);
	}

	timer->start();

}

void xmlChange2::xmlFileUpdate()
{
	if (lineEditSource->text().isEmpty())
	{
		QString tempStr1 = codec->toUnicode("警告");
		QString tempStr2 = codec->toUnicode("请选择转换的工程(res)路径!");
		textEditOutShow(tempStr1,tempStr2,1);
		return;
	}

	if (lineEditTarget->text().isEmpty())
	{
		QString tempStr1 = codec->toUnicode("警告");
		QString tempStr2 = codec->toUnicode("请选择转换的工程(res)路径!");
		textEditOutShow(tempStr1,tempStr2,1);
		return;
	}

	timer->stop();
	configIni->setValue("/ts/path", lineEditTarget->text()); 

	textEditOut->clear();

	QString dirPath = lineEditSource->text();
	QDir *dirRoot = new QDir(dirPath);
	dirRoot->setFilter(QDir::Dirs| QDir::Hidden);
	QFileInfoList listDir = dirRoot->entryInfoList();

	int countFile =0;
	int countSuccess =0;
	int countError=0;
	QStringList errorList;
	QStringList existsNoList;

	for (int i = 0; i < listDir.size(); ++i) {
		QFileInfo fileInfo = listDir.at(i);
		QString dirName = fileInfo.fileName();

		if (dirName.left(6)=="values")
		{
			bool CheckFlag=false;
			for(int i=0;i<gLayout->count() ;i++) 
			{
				QCheckBox *widgetCheck = (QCheckBox *)gLayout->itemAt(i)->widget();

				if (dirName == widgetCheck->text()&& widgetCheck->isChecked() && widgetCheck->isEnabled())
				{
					CheckFlag = true;
					break;
				}
			}

			if (CheckFlag)
			{
				countFile++;

				QString targetPath;
				targetPath.append(lineEditTarget->text()).append("/strings");

				if (dirName.left(7)=="values-")
				{
					tsType = dirName.mid(7);
					targetPath.append("_");

				}else 
				{
					tsType = "";
				}

				targetPath.append(tsType).append(".ts");

				QFile fileTs(targetPath);
				bool retExists = fileTs.exists(targetPath);
				if (retExists)
				{
					QXmlInputSource xmlInputSource(&fileTs);
					QXmlSimpleReader reader;
					tsHandler *myTSHandler = new tsHandler();
					myTSHandler->textEditOut = textEditOut;
					myTSHandler->filePathRet = targetPath;

					reader.setContentHandler(myTSHandler);
					reader.setErrorHandler(myTSHandler);
					bool errorRet = reader.parse(xmlInputSource);
					if (errorRet)
					{
						QStringList headListReadTS = myTSHandler->xmlListHead;
						QStringList contextListReadTS = myTSHandler->xmlListContext;

						QString pathFile = QString("%1/%2/strings.xml").arg(dirPath).arg(dirName);
						QFile fileXml(pathFile);
						bool retExistsXml = fileXml.exists(pathFile);
						QStringList headListReadXML;
						QStringList contextListReadXML;
						QStringList headListReadComment;
						QStringList contextListReadComment;

						if (retExistsXml)
						{
							QXmlInputSource xmlInputSourceTemp(&fileXml);
							QXmlSimpleReader readerTemp;
							xmlHandler *myXMLHandler = new xmlHandler();
							myXMLHandler->textEditOut = textEditOut;

							readerTemp.setContentHandler(myXMLHandler);
							readerTemp.setLexicalHandler(myXMLHandler);
							readerTemp.setErrorHandler(myXMLHandler);
							readerTemp.parse(xmlInputSourceTemp);

							myXMLHandler->filePathRet = pathFile;

							headListReadXML = myXMLHandler->xmlListHead;
							contextListReadXML = myXMLHandler->xmlListContext;
							headListReadComment = myXMLHandler->commentPostList;
							contextListReadComment = myXMLHandler->commentContextList;

							myXMLHandler->~xmlHandler();
						}

						fileTs.close();
						fileXml.close();

						bool ret= myTSHandler->mergeXMLFile(pathFile,headListReadTS,contextListReadTS,headListReadXML,
							contextListReadXML,headListReadComment,contextListReadComment);

						if (ret)
						{
							countSuccess++;
							
							if(tsType.isEmpty())
							{
								QString tempStr1 = codec->toUnicode("成功");
								QString tempStr2 = codec->toUnicode("--:合并更新文本项数%1.").arg(myTSHandler->updateCount);

								textEditOutShow(tempStr1,tempStr2);
							}else
							{
								QString tempStr1 = codec->toUnicode("成功");
								QString tempStr2 = codec->toUnicode("%1:合并更新文本项数%2.").arg(tsType).arg(myTSHandler->updateCount);

								textEditOutShow(tempStr1,tempStr2);
							}
								
						}else{
							countError++;
							QString tempStr1 = codec->toUnicode("错误");
							QString tempStr2 = codec->toUnicode("路径:%1").arg(pathFile);

							textEditOutShow(tempStr1,tempStr2,1);
						}
					}else{
						fileTs.close();
						countError++;
					}
					
					myTSHandler->~tsHandler();
				}else{		
					existsNoList<<targetPath;
				}
			}

		}

	}

	if (countFile == countSuccess)
	{
		QString tempStr1 = codec->toUnicode("提示");
		QString tempStr2 = codec->toUnicode("所有文件转换完成!");
		textEditOutShow(tempStr1,tempStr2);

	}else{
// 		QString errorString;
// 		for (int i=0;i<errorList.size();i++)
// 		{
// 			errorString.append("--");
// 			errorString.append(errorList.at(i));
// 			errorString.append("\n");
// 		}
		QString tempStr1 = codec->toUnicode("提示");
		QString tempStr2 = codec->toUnicode("%1个文件转换失败.").arg(countError);
		textEditOutShow(tempStr1,tempStr2);
	}
	
	timer->start();
}


 

tsHandle.h

#ifndef tsHandler_H
#define tsHandler_H

#include <QXmlDefaultHandler>

class tsHandler : public QXmlDefaultHandler
{
public:
	tsHandler();
	~tsHandler();

	bool startElement(const QString &namespaceURI, const QString &localName,
		const QString &qName, const QXmlAttributes &attributes);
	bool endElement(const QString &namespaceURI, const QString &localName,
		const QString &qName);
	bool characters(const QString &str);
	bool fatalError(const QXmlParseException &exception);
	QString errorString() const;
	bool comment(const QString &ch);

	bool saveXMLFile();
	bool mergeXMLFile(QString filePath,QStringList readHeadlist,QStringList readContextList);
	bool mergeXMLFile(QString filePath,QStringList headListReadTS,QStringList contextListReadTS,
						QStringList headListReadXML,QStringList contextListReadXML,
						QStringList headListReadComment,QStringList contextListReadComment);
	QStringList xmlListHead;
	QStringList xmlListContext;

	QStringList commentPostList;
	QStringList commentContextList;

	QString filePathRet;

	QTextEdit *textEditOut;
	void textEditOutShow(QString flagStr,QString textStr,int colorFlag=0);//日志输出

	QString tsChangeSpecialChar(QString xmlOldChar);

	int updateCount;//更新文本数
	int unfinishedCount;//未翻译数

	QTextCodec *codec;

private:

	QString currentText;
	QString errorStr;
	bool xmlTag;

	QIcon folderIcon;
	QIcon bookmarkIcon;

};

#endif


tsHandle.cpp

#include <QtGui>

#include "tsHandler.h"
#include <QXmlDefaultHandler>
#include <QTextCodec>
#include <QDomDocument>
#include <QDomDocumentType>

tsHandler::tsHandler()
{
	unfinishedCount = 0;
	updateCount = 0;

	codec = QTextCodec::codecForLocale();
}

tsHandler::~tsHandler()
{
}

bool tsHandler::startElement(const QString & /* namespaceURI */,
							 const QString & /* localName */,
							 const QString &qName,
							 const QXmlAttributes &attributes)
{
	if (qName == "translation") {
		QString headFild = attributes.value("type");
		if (headFild == "unfinished")
		{
			unfinishedCount++;
		}
	} 
	currentText.clear();
	return true;
}

bool tsHandler::endElement(const QString & /* namespaceURI */,
						   const QString & /* localName */,
						   const QString &qName)
{
	QString contextFild = tsChangeSpecialChar(currentText);
	if (qName == "comment") 
	{
		xmlListHead<<contextFild;
	} else if(qName == "translation") 
	{
		xmlListContext<<contextFild;
	}
	return true;
}

bool tsHandler::characters(const QString &str)
{
	currentText += str;
	return true;
}

bool tsHandler::fatalError(const QXmlParseException &exception)
{
// 	QMessageBox::information(NULL, QObject::tr("Error"),
// 		QObject::tr("Parse error at line %1, column %2:\n"
// 		"%3")
// 		.arg(exception.lineNumber())
// 		.arg(exception.columnNumber())
// 		.arg(exception.message()));
	QString tempStr1 = codec->toUnicode("错误");
	QString tempStr2 = codec->toUnicode("路径:%1,出错位置:%2行,%3列,错误信息:%4").arg(filePathRet).arg(exception.lineNumber()).arg(exception.columnNumber()).arg(exception.message());
	textEditOutShow(tempStr1,tempStr2,1);
	
	return false;
}

QString tsHandler::errorString() const
{
	return errorStr;
}

bool tsHandler::comment(const QString &ch)
{
	commentPostList<<QString("%1").arg(xmlListHead.size());
	commentContextList<<ch;
	return true;
}

void tsHandler::textEditOutShow(QString flagStr,QString textStr,int colorFlag)
{
	QString strTmp; 
	if (colorFlag==1)
	{
		strTmp=QString("<font color=red>%1: %2 -- %3</font>").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(flagStr).arg(textStr);
	}else{
		strTmp= QString("%1: %2 -- %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(flagStr).arg(textStr);
	}
	textEditOut->append(strTmp);

}

QString tsHandler::tsChangeSpecialChar(QString xmlOldChar)
{
	QString ret=xmlOldChar;


	ret.replace("\'","\\\'");

	ret.replace("\"","\\\"");

	ret.replace("'","\\\'");

	ret.replace("'","\\\"");

	ret.replace(""","\\\'");

	ret.replace("'","\\\"");


	return ret;
}

bool tsHandler::saveXMLFile()
{
	if (xmlListHead.size()!=xmlListContext.size())
	{
		return FALSE;
	}

	int pos = filePathRet.lastIndexOf("/")+1;
	QString pathT = filePathRet.left(pos);
	QString fileNameT = filePathRet.mid(pos);

	pathT.append("strings.xml");

	QFile file(pathT);

	bool retSet = file.setPermissions(QFile::WriteOwner);

	if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
		return FALSE;

	QDomDocument doc;
	QDomText text;
	QDomElement element;

	QDomProcessingInstruction instruction;
	instruction = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"utf-8\"" );
	doc.appendChild( instruction );


	QDomElement root = doc.createElement( "resources" );
	doc.appendChild( root );

	for (int i=0;i<xmlListHead.size();i++)
	{
		element = doc.createElement( "string" );
		QDomAttr nameQDE = doc.createAttribute( "name" );
		nameQDE.setValue(xmlListHead.at(i));
		element.setAttributeNode( nameQDE );
		text = doc.createTextNode( xmlListContext.at(i) );
		element.appendChild( text);
		root.appendChild( element );

	}

	QTextStream out( &file );
	out.setCodec("UTF-8"); 
	doc.save( out, 4 );

	file.close();

	return TRUE;
}


bool tsHandler::mergeXMLFile(QString filePath,QStringList readHeadlist,QStringList readContextList)
{
	QFile file(filePath);

	bool retSet = file.setPermissions(QFile::WriteOwner);

	if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
		return FALSE;

	QDomDocument doc;
	QDomText text;
	QDomElement element;

	QDomProcessingInstruction instruction;
	instruction = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"utf-8\"" );
	doc.appendChild( instruction );


	QDomElement root = doc.createElement( "resources" );
	doc.appendChild( root );

	for (int i=0;i<readHeadlist.size();i++)
	{
		element = doc.createElement( "string" );
		QDomAttr nameQDE = doc.createAttribute( "name" );
		nameQDE.setValue(readHeadlist.at(i));
		element.setAttributeNode( nameQDE );
		text = doc.createTextNode( readContextList.at(i) );
		element.appendChild( text);
		root.appendChild( element );

	}

	QTextStream out( &file );
	out.setCodec("UTF-8"); 
	doc.save( out, 4 );

	file.close();

	return TRUE;
}




bool tsHandler::mergeXMLFile(QString filePath,QStringList headListReadTS,QStringList contextListReadTS,
							 QStringList headListReadXML,QStringList contextListReadXML,
							 QStringList headListReadComment,QStringList contextListReadComment)
{
	QFile file(filePath);

	bool retSet = file.setPermissions(QFile::WriteOwner);

	if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
		return FALSE;

	QDomDocument doc;
	QDomText text;
	QDomElement element;

	QDomProcessingInstruction instruction;
	instruction = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"utf-8\"" );
	doc.appendChild( instruction );


	QDomElement root = doc.createElement( "resources" );
	doc.appendChild( root );


	for (int i=0;i<headListReadXML.size();i++)
	{
		QString tempHeadT = headListReadXML.at(i);
		
		int posRComment = headListReadComment.lastIndexOf(QString("%1").arg(i));
		int countComment = headListReadComment.count(QString("%1").arg(i));
		if (countComment>0)
		{
			int posLComment = headListReadComment.indexOf(QString("%1").arg(i));
			int j=0;
			while(j<countComment)
			{
				QDomComment domComment = doc.createComment(contextListReadComment.at(posLComment+j));
				root.appendChild(domComment);
				j++;
			}
		}

		element = doc.createElement( "string" );
		QDomAttr nameQDE = doc.createAttribute( "name" );

		if (!headListReadTS.contains(tempHeadT))
		{
			nameQDE.setValue(headListReadXML.at(i));
			element.setAttributeNode( nameQDE );
			text = doc.createTextNode( contextListReadXML.at(i) );

		}else{
			
			nameQDE.setValue(headListReadXML.at(i));
			element.setAttributeNode( nameQDE );
			text = doc.createTextNode( contextListReadTS.at(headListReadTS.lastIndexOf(tempHeadT)) );
			QString xmlContext,tsContext;
			xmlContext = contextListReadXML.at(i);
			tsContext = contextListReadTS.at(headListReadTS.lastIndexOf(tempHeadT));
			if (xmlContext != tsContext)
			{
				updateCount++;
			}
		}

		element.appendChild( text);
		root.appendChild( element );
	}

	QTextStream out( &file );
	out.setCodec("UTF-8"); 
	doc.save( out, 4 );

	file.close();

	return TRUE;
}


xmlHandle.h

#ifndef XMLHANDLER_H
#define XMLHANDLER_H

#include <QIcon>
#include <QXmlDefaultHandler>
#include <QSettings> 

class xmlHandler : public QXmlDefaultHandler
{
public:
	xmlHandler();
	~xmlHandler();

	bool startElement(const QString &namespaceURI, const QString &localName,
		const QString &qName, const QXmlAttributes &attributes);
	bool endElement(const QString &namespaceURI, const QString &localName,
		const QString &qName);
	bool characters(const QString &str);
	bool fatalError(const QXmlParseException &exception);
	QString errorString() const;

	bool comment(const QString &ch);

	bool saveTSFile();
	QString getLanguageType();

	QStringList xmlListHead;//该转换文件内容
	QStringList xmlListContext;
	QStringList zhFileHeadList;//zh文件内容
	QStringList zhFileContextList;
	QStringList enFileHeadList;//en文件内容
	QStringList enFileContextList;

	QString filePathRet;//该转换文件本地路径
	QString tsType;//语种
	QString targetPath;//ts文件保存目标路径

	QStringList commentPostList;//保存注释内容
	QStringList commentContextList;

	QStringList integrationHeadList;

	QSettings *configIni;//配置文件

	QTextEdit *textEditOut;
	void textEditOutShow(QString flagStr,QString textStr,int colorFlag=0);//日志输出

	int unfinishedCount;//保存未翻译项数
	bool saveTSUnfinishedFile();//ts文件中仅保存未翻译字段
	bool saveTSUnfinishedStarFile();//创建英文星标文件,用作翻译工具的比较
	QString xmlChangeSpecialChar(QString xmlOldChar);

	QTextCodec *codec;

private:
	QString currentText;
	QString errorStr;
	bool metXbelTag;

	QIcon folderIcon;
	QIcon bookmarkIcon;
	int checkFlag;

	QStringList languageHeadList;
	QStringList languageContextList;
};

#endif


xmlHandle.cpp

#include <QtGui>

#include "xmlHandler.h"
#include <QXmlDefaultHandler>
#include <QTextCodec>
#include <QDomDocument>


xmlHandler::xmlHandler()
{
	checkFlag = -1;
	unfinishedCount = 0;
	languageHeadList<<"en"<<"ko"<<"tr"<<"ru"<<"pt"<<"fr"<<"it"<<"en-rCA"<<"en-rUS"
		<<"es"<<"de"<<"zh-rTW"<<"pl"<<"hu"<<"el"<<"nl"<<"zh-rHK"<<"zh"
		<<"sr"<<"ro"<<"da"<<"fa"<<"fi"<<"sv"<<"cs"<<"ar"<<"ja";
	//其中波斯语fr没有选择是阿富汗还是伊朗版
	languageContextList<<"en_US"<<"ko_KR"<<"tr_TR"<<"ru_RU"<<"pt_PT"<<"fr_FR"<<"it_IT"<<"en_CA"<<"en_US"
		<<"es_ES"<<"de_DE"<<"zh_TW"<<"pl_PL"<<"hu_HU"<<"el_GR"<<"nl_NL"<<"zh_HK"<<"zh_CN"
		<<"sr_CS"<<"ro_MD"<<"da_DK"<<"fa_IR"<<"fi_FI"<<"sv_SE"<<"cs_CZ"<<"ar_SA"<<"ja_JP";

	codec = QTextCodec::codecForLocale();
}

xmlHandler::~xmlHandler()
{
}

bool xmlHandler::startElement(const QString & /* namespaceURI */,
							  const QString & /* localName */,
							  const QString &qName,
							  const QXmlAttributes &attributes)
{

	if (qName == "string") {
		QString headFild = attributes.value("name");
		int pos = xmlListHead.indexOf(headFild);
		if (pos==-1)
		{
			xmlListHead<<headFild;
		}else{
			checkFlag = pos;
		}			
		metXbelTag = true;

	} 
	currentText.clear();
	return true;
}

bool xmlHandler::endElement(const QString & /* namespaceURI */,
							const QString & /* localName */,
							const QString &qName)
{
	if (qName == "string") 
	{
		//QString contextFild = currentText;
		QString contextFild = xmlChangeSpecialChar(currentText);
		if (checkFlag!=-1)
		{
			xmlListContext.replace(checkFlag,contextFild);
			checkFlag = -1;
		}else
			xmlListContext<<contextFild;
		
	}
	return true;
}

bool xmlHandler::characters(const QString &str)
{
	currentText += str;
	return true;
}

bool xmlHandler::fatalError(const QXmlParseException &exception)
{
// 	QMessageBox::information(NULL, QObject::tr("Error"),
// 		QObject::tr("Parse error at line %1, column %2:\n"
// 		"%3")
// 		.arg(exception.lineNumber())
// 		.arg(exception.columnNumber())
// 		.arg(exception.message()));

	QString tempStr1 = codec->toUnicode("错误");
	QString tempStr2 = codec->toUnicode("路径:%1,出错位置:%2行,%3列,错误信息:%4.").arg(filePathRet).arg(exception.lineNumber()).arg(exception.columnNumber()).arg(exception.message());
	textEditOutShow(tempStr1,tempStr2,1);

	return false;
}

QString xmlHandler::errorString() const
{
	return errorStr;
}


bool xmlHandler::comment(const QString &ch)
{
	commentPostList<<QString("%1").arg(xmlListHead.size());
	commentContextList<<ch;
	return true;
}


void xmlHandler::textEditOutShow(QString flagStr,QString textStr,int colorFlag)
{
	QString strTmp; 
	if (colorFlag==1)
	{
		strTmp=QString("<font color=red>%1: %2 -- %3</font>").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(flagStr).arg(textStr);
	}else{
		strTmp= QString("%1: %2 -- %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(flagStr).arg(textStr);
	}
	textEditOut->append(strTmp);

}

QString xmlHandler::getLanguageType()
{
	QString retS="";
	if (!tsType.isEmpty())
	{
		retS = configIni->value(QString("/language/%1").arg(tsType)).toString();
		if (retS.isEmpty())
		{
			int pos = languageHeadList.indexOf(tsType);
			if (pos>=0)
			{
				retS = languageContextList.at(pos);
			}else
				retS = tsType;

			for (int i=0;i<languageHeadList.size();i++)
			{
				configIni->setValue(QString("/language/%1").arg(languageHeadList.at(i)), languageContextList.at(i)); 
			}
		}
		
	}else{
		retS = configIni->value(QString("/language/en")).toString();
		if (retS.isEmpty())
		{
			retS = "en_US";
			for (int i=0;i<languageHeadList.size();i++)
			{
				configIni->setValue(QString("/language/%1").arg(languageHeadList.at(i)), languageContextList.at(i)); 
			}
		}
	}
	
	return retS;
}

QString xmlHandler::xmlChangeSpecialChar(QString xmlOldChar)
{
	QString ret=xmlOldChar;


	ret.replace("\\\'","\'");

	ret.replace("\\\"","\"");


	return ret;
}



bool xmlHandler::saveTSFile()
{
	if (xmlListHead.size()!=xmlListContext.size())
	{
		return FALSE;
	}

	QFile file(targetPath);

	if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
		return FALSE;

	QDomDocument doc;
	QDomText text;
	QDomElement element;

	QDomProcessingInstruction instruction;
	instruction = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"utf-8\"" );
	doc.appendChild( instruction );

	QDomElement elementTS = doc.createElement( "TS" );
	QDomAttr versionQDE = doc.createAttribute( "version" );
	versionQDE.setValue( "2.0" );
	QDomAttr languageQDE = doc.createAttribute( "language" );
	QString lTypeS = getLanguageType();
	languageQDE.setValue(lTypeS);

	
	elementTS.setAttributeNode( versionQDE );
	elementTS.setAttributeNode( languageQDE );
	doc.appendChild( elementTS );


	element = doc.createElement( "defaultcodec" );
	text = doc.createTextNode( "System" );
	element.appendChild( text );
	elementTS.appendChild( element );

	QDomElement root = doc.createElement( "context" );
	elementTS.appendChild( root );

	element = doc.createElement( "name" );
	text = doc.createTextNode( "public" );
	element.appendChild( text );
	root.appendChild( element );

	for (int i=0;i<xmlListHead.size();i++)
	{
		QDomElement elementMsg = doc.createElement( "message" );

		int pos = zhFileHeadList.indexOf(xmlListHead.at(i));
		element = doc.createElement( "source" );
		QString sourceZH;
		QString commentC;
		QString tranC;
		if (pos!=-1)
		{
			sourceZH = zhFileContextList.at(pos);
			text = doc.createTextNode( zhFileContextList.at(pos) );
		}else{
			text = doc.createTextNode( xmlListHead.at(i) );
		}
		element.appendChild( text);
		elementMsg.appendChild( element );

		commentC = xmlListHead.at(i) ;
		element = doc.createElement( "comment" );
		text = doc.createTextNode( xmlListHead.at(i) );
		element.appendChild( text);
		elementMsg.appendChild( element );

		if (lTypeS != "en_US")
		{
			int posEn = enFileHeadList.indexOf(commentC);

			if (posEn ==-1)
			{
				element = doc.createElement( "translation" );

// 				QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 				if (tranStr.isEmpty())
// 				{
					text = doc.createTextNode( xmlListContext.at(i) );
// 				}else
// 					text = doc.createTextNode( tranStr );


				element.appendChild( text);
				elementMsg.appendChild( element );

			}else{

				QString enContext = enFileContextList.at(posEn);

				if (enContext == xmlListContext.at(i))
				{
					element = doc.createElement( "translation" );
					element.setAttribute("type","unfinished");

// 					QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 					if (tranStr.isEmpty())
// 					{
						text = doc.createTextNode( xmlListContext.at(i) );
// 					}else
// 						text = doc.createTextNode( tranStr );

					element.appendChild( text);
					elementMsg.appendChild( element );
					unfinishedCount++;
				}else{

					element = doc.createElement( "translation" );

// 					QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 					if (tranStr.isEmpty())
// 					{
						text = doc.createTextNode( xmlListContext.at(i) );
// 					}else
// 						text = doc.createTextNode( tranStr );

					element.appendChild( text);
					elementMsg.appendChild( element );
				}
			}
			
		}else{

			if (!sourceZH.isEmpty() && sourceZH == xmlListContext.at(i))
			{
				element = doc.createElement( "translation" );
				element.setAttribute("type","unfinished");

// 				QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 				if (tranStr.isEmpty())
// 				{
					text = doc.createTextNode( xmlListContext.at(i) );
// 				}else
// 					text = doc.createTextNode( tranStr );

				element.appendChild( text);
				elementMsg.appendChild( element );
				unfinishedCount++;
			}else{

				element = doc.createElement( "translation" );

// 				QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 				if (tranStr.isEmpty())
// 				{
					text = doc.createTextNode( xmlListContext.at(i) );
// 				}else
// 					text = doc.createTextNode( tranStr );


				element.appendChild( text);
				elementMsg.appendChild( element );
			}
		}
		

		root.appendChild(elementMsg);

	
	}

	QTextStream out( &file );
	out.setCodec("UTF-8"); 
	doc.save( out, 4 );

	file.close();

	return TRUE;
}



bool xmlHandler::saveTSUnfinishedStarFile()
{
	QFile file(targetPath);

	if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
		return FALSE;

	QDomDocument doc;
	QDomText text;
	QDomElement element;

	QDomProcessingInstruction instruction;
	instruction = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"utf-8\"" );
	doc.appendChild( instruction );

	QDomElement elementTS = doc.createElement( "TS" );
	QDomAttr versionQDE = doc.createAttribute( "version" );
	versionQDE.setValue( "2.0" );
	QDomAttr languageQDE = doc.createAttribute( "language" );
	QString lTypeS = getLanguageType();
	languageQDE.setValue(lTypeS);


	elementTS.setAttributeNode( versionQDE );
	elementTS.setAttributeNode( languageQDE );
	doc.appendChild( elementTS );


	element = doc.createElement( "defaultcodec" );
	text = doc.createTextNode( "System" );
	element.appendChild( text );
	elementTS.appendChild( element );

	QDomElement root = doc.createElement( "context" );
	elementTS.appendChild( root );

	element = doc.createElement( "name" );
	text = doc.createTextNode( "public" );
	element.appendChild( text );
	root.appendChild( element );

	for (int i=0;i<integrationHeadList.size();i++)
	{
		QDomElement elementMsg = doc.createElement( "message" );

		int pos = zhFileHeadList.indexOf(integrationHeadList.at(i));
		element = doc.createElement( "source" );
		QString sourceZH;
		QString commentC;
		QString tranC;
		if (pos!=-1)
		{
			sourceZH = zhFileContextList.at(pos);
			text = doc.createTextNode( zhFileContextList.at(pos) );
		}else{
			text = doc.createTextNode( integrationHeadList.at(i) );
		}
		element.appendChild( text);
		elementMsg.appendChild( element );

		commentC = integrationHeadList.at(i) ;
		element = doc.createElement( "comment" );
		text = doc.createTextNode( integrationHeadList.at(i) );
		element.appendChild( text);
		elementMsg.appendChild( element );

		int posEn = enFileHeadList.indexOf(commentC);
		element = doc.createElement( "translation" );

// 		QString tranStr = xmlChangeSpecialChar(enFileContextList.at(posEn));
// 		if (tranStr.isEmpty())
// 		{
			text = doc.createTextNode( enFileContextList.at(posEn) );
// 		}else
// 			text = doc.createTextNode( tranStr );


		element.appendChild( text);
		elementMsg.appendChild( element );

		root.appendChild(elementMsg);
	}

	QTextStream out( &file );
	out.setCodec("UTF-8"); 
	doc.save( out, 4 );

	file.close();

	return TRUE;
}




bool xmlHandler::saveTSUnfinishedFile()
{
	if (xmlListHead.size()!=xmlListContext.size())
	{
		return FALSE;
	}

	QFile file(targetPath);

	if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
		return FALSE;

	QDomDocument doc;
	QDomText text;
	QDomElement element;

	QDomProcessingInstruction instruction;
	instruction = doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"utf-8\"" );
	doc.appendChild( instruction );

	QDomElement elementTS = doc.createElement( "TS" );
	QDomAttr versionQDE = doc.createAttribute( "version" );
	versionQDE.setValue( "2.0" );
	QDomAttr languageQDE = doc.createAttribute( "language" );
	QString lTypeS = getLanguageType();
	languageQDE.setValue(lTypeS);


	elementTS.setAttributeNode( versionQDE );
	elementTS.setAttributeNode( languageQDE );
	doc.appendChild( elementTS );


	element = doc.createElement( "defaultcodec" );
	text = doc.createTextNode( "System" );
	element.appendChild( text );
	elementTS.appendChild( element );

	QDomElement root = doc.createElement( "context" );
	elementTS.appendChild( root );

	element = doc.createElement( "name" );
	text = doc.createTextNode( "public" );
	element.appendChild( text );
	root.appendChild( element );

	for (int i=0;i<xmlListHead.size();i++)
	{
		if (lTypeS != "en_US")
		{
			if (enFileHeadList.size()==0)
			{
				QDomElement elementMsg = doc.createElement( "message" );

				int pos = zhFileHeadList.indexOf(xmlListHead.at(i));
				element = doc.createElement( "source" );
				QString sourceZH;
				QString commentC;
				QString tranC;
				if (pos!=-1)
				{
					sourceZH = zhFileContextList.at(pos);
					text = doc.createTextNode( zhFileContextList.at(pos) );//增加中文字段为source字段
				}else{
					text = doc.createTextNode( xmlListHead.at(i) );//增加开发者注释字段同为source字段
				}
				element.appendChild( text);
				elementMsg.appendChild( element );

				commentC = xmlListHead.at(i) ;
				element = doc.createElement( "comment" );//增加开发者注释字段
				text = doc.createTextNode( xmlListHead.at(i) );
				element.appendChild( text);
				elementMsg.appendChild( element );

				if (integrationHeadList.indexOf(commentC)==-1)
				{
					integrationHeadList<<commentC;
				}

				element = doc.createElement( "translation" );
// 
// 				QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 				if (tranStr.isEmpty())
// 				{
					text = doc.createTextNode( xmlListContext.at(i) );
// 				}else
// 					text = doc.createTextNode( tranStr );

				element.appendChild( text);
				elementMsg.appendChild( element );

				root.appendChild(elementMsg);
			}else
			{
				int posEn = enFileHeadList.indexOf(xmlListHead.at(i));//根据开发者注释字段查询是否存在英文版本

				if (posEn != -1)//当该字段存在英文版本时
				{
					QString enContext = enFileContextList.at(posEn);

					if (enContext == xmlListContext.at(i))//当英文版本翻译内容与文本翻译内容相同时,视为未翻译状态
					{
						QDomElement elementMsg = doc.createElement( "message" );

						int pos = zhFileHeadList.indexOf(xmlListHead.at(i));
						element = doc.createElement( "source" );
						QString sourceZH;
						QString commentC;
						QString tranC;
						if (pos!=-1)
						{
							sourceZH = zhFileContextList.at(pos);
							text = doc.createTextNode( zhFileContextList.at(pos) );//增加中文字段为source字段
						}else{
							text = doc.createTextNode( xmlListHead.at(i) );//增加开发者注释字段同为source字段
						}
						element.appendChild( text);
						elementMsg.appendChild( element );

						commentC = xmlListHead.at(i) ;
						element = doc.createElement( "comment" );//增加开发者注释字段
						text = doc.createTextNode( xmlListHead.at(i) );
						element.appendChild( text);
						elementMsg.appendChild( element );

						if (integrationHeadList.indexOf(commentC)==-1)
						{
							integrationHeadList<<commentC;
						}

						element = doc.createElement( "translation" );
						element.setAttribute("type","unfinished");

// 						QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 						if (tranStr.isEmpty())
// 						{
							text = doc.createTextNode( xmlListContext.at(i) );
// 						}else
// 							text = doc.createTextNode( tranStr );

						element.appendChild( text);
						elementMsg.appendChild( element );

						unfinishedCount++;
						root.appendChild(elementMsg);
					}
				}
			}
		}else{

			QString sourceZH;
			int pos = zhFileHeadList.indexOf(xmlListHead.at(i));
			if (pos!=-1)
			{
				sourceZH = zhFileContextList.at(pos);
			}

			if (!sourceZH.isEmpty() && sourceZH == xmlListContext.at(i))
			{
				QDomElement elementMsg = doc.createElement( "message" );

				int pos = zhFileHeadList.indexOf(xmlListHead.at(i));
				element = doc.createElement( "source" );
				QString sourceZH;
				QString commentC;
				QString tranC;
				if (pos!=-1)
				{
					sourceZH = zhFileContextList.at(pos);
					text = doc.createTextNode( zhFileContextList.at(pos) );//增加中文字段为source字段
				}else{
					text = doc.createTextNode( xmlListHead.at(i) );//增加开发者注释字段同为source字段
				}
				element.appendChild( text);
				elementMsg.appendChild( element );

				commentC = xmlListHead.at(i) ;
				element = doc.createElement( "comment" );//增加开发者注释字段
				text = doc.createTextNode( xmlListHead.at(i) );
				element.appendChild( text);
				elementMsg.appendChild( element );

				if (integrationHeadList.indexOf(commentC)==-1)
				{
					integrationHeadList<<commentC;
				}

				element = doc.createElement( "translation" );
				element.setAttribute("type","unfinished");

// 				QString tranStr = xmlChangeSpecialChar(xmlListContext.at(i));
// 				if (tranStr.isEmpty())
// 				{
					text = doc.createTextNode( xmlListContext.at(i) );
// 				}else
// 					text = doc.createTextNode( tranStr );
// 				
				element.appendChild( text);
				elementMsg.appendChild( element );
				
				unfinishedCount++;
				root.appendChild(elementMsg);
			}
		}
	}

	QTextStream out( &file );
	out.setCodec("UTF-8"); 
	doc.save( out, 4 );

	file.close();

	return TRUE;
}


 

相关文章推荐

android String.xml Excel 相互转换工具

  • 2017年07月26日 19:54
  • 2.88MB
  • 下载

Android string.xml多语言翻译工具

Android string.xml 翻译工具源码你可以在Github上看到:https://github.com/onlynight/Translator概述最近要把公司的app往国外推广,无奈只做...

安卓快速解析xml工具类

xml工具类XmlHelper利用sax的DefaultHandler做快速的解析,并通过反射把解析结果储存在一个对象里或者一个储存该对象的list里。XmlHelper代码import java.i...

安卓Systemproperties的读写 && Android平台LOG输出规范 && Log写入文件 && 将异常(getStackTrace)转化成String

简要分析一下build.prop是如何生成的。Android的build.prop文件是在Android编译时收集的各种property(LCD density/语言/编译时间, etc.),编译完成...

xml文件安卓

  • 2013年03月21日 15:38
  • 4KB
  • 下载

ANDROID STRINGS.XML的特殊字符_安卓STRING.XML添加空格或字符的方法

项目中要在string.xml 中显示特殊符号、如@号冒号等、直接写肯定不行啦 只能考虑使用ASCII码进行显示、在XML中空格符不是用HMTL中的表示、而是用编码表示 以下为常见的ASC...
  • oPINGU
  • oPINGU
  • 2015年05月26日 09:31
  • 1369

安卓解析XML文件

  • 2015年01月24日 15:16
  • 2.48MB
  • 下载

C语言获取ts格式文件的总时长

以下为当时做的一个功能so源码,对博主的原文做了简单的修改然后调用了其中一个接口,如果要在此源码上实现该功能,只需要将GetFileTime方法修改成main方法即可,也可以对此源码进行编译成so库,...

DOM方式进行的XML文件、Document、String之间的相互转换

XML文件test.xml:   Xml代码    xml version="1.0" encoding="UTF-8"?>books>book>name>哈里波特nam...
  • Alsyuan
  • Alsyuan
  • 2017年06月14日 14:54
  • 201

DOM方式进行的XML文件、Document、String之间的相互转换

XML文件test.xml: Xml代码   xml version="1.0" encoding="UTF-8"?>books>book>name>哈里波特name>price>10p...
  • wcknxx
  • wcknxx
  • 2017年04月26日 09:36
  • 100
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ts与安卓语言文件string.xml的转换工具
举报原因:
原因补充:

(最多只允许输入30个字)