예제 #1
0
int main(int argc, char *argv[])
{
	qInstallMsgHandler(myMessageHandler);
	QCoreApplication app(argc, argv);

	if (argc != 3)
	{
		qCritical("Usage: autotranslate <destination-dir> <source-dir>.");
		return -1;
	}

	QDir dstDir(app.arguments().value(1));
	if (!dstDir.exists())
	{
		qCritical("Destination directory '%s' not found.",dstDir.dirName().toLocal8Bit().constData());
		return -1;
	}

	QDir srcDir(app.arguments().value(2),"*.ts",QDir::Name,QDir::Files);
	if (!srcDir.exists())
	{
		qCritical("Source directory '%s' not found.",srcDir.dirName().toLocal8Bit().constData());
		return -1;
	}

	QDirIterator srcIt(srcDir);
	while (srcIt.hasNext())
	{
		QFile srcFile(srcIt.next());
		if (srcFile.open(QFile::ReadOnly))
		{
			QDomDocument doc;
			if (doc.setContent(&srcFile,true))
			{
				qDebug("Generation auto translation from '%s'.",srcFile.fileName().toLocal8Bit().constData());

				QDomElement rootElem = doc.firstChildElement("TS");
				rootElem.setAttribute("language",rootElem.attribute("sourcelanguage","en"));
				
				QDomElement contextElem = rootElem.firstChildElement("context");
				while(!contextElem.isNull())
				{
					QDomElement messageElem = contextElem.firstChildElement("message");
					while(!messageElem.isNull())
					{
						QDomElement sourceElem = messageElem.firstChildElement("source");
						QDomElement translationElem = messageElem.firstChildElement("translation");
						if (!sourceElem.isNull() && !translationElem.isNull())
						{
							QString sourceText = sourceElem.text();
							if (messageElem.attribute("numerus") == "yes")
							{
								QDomElement numerusEelem = translationElem.firstChildElement("numerusform");
								while(!numerusEelem.isNull())
								{
									numerusEelem.removeChild(numerusEelem.firstChild());
									numerusEelem.appendChild(doc.createTextNode(sourceText));
									numerusEelem = numerusEelem.nextSiblingElement("numerusform");
								}
							}
							else
							{
								translationElem.removeChild(translationElem.firstChild());
								translationElem.appendChild(doc.createTextNode(sourceText));
							}
							translationElem.removeAttribute("type");
						}
						messageElem = messageElem.nextSiblingElement("message");
					}
					contextElem = contextElem.nextSiblingElement("context");
				}
				srcFile.close();

				QFileInfo srcFileInfo(srcDir.absoluteFilePath(srcFile.fileName()));
				QFile dstFile(dstDir.absoluteFilePath(srcFileInfo.fileName()));
				if (dstFile.open(QFile::WriteOnly|QFile::Truncate))
				{
					dstFile.write(doc.toByteArray());
					dstFile.close();
				}
				else
				{
					qWarning("Failed to open destination file '%s' for write.",dstFile.fileName().toLocal8Bit().constData());
				}
			}
			else
			{
				qWarning("Invalid translation source file '%s'.",srcFile.fileName().toLocal8Bit().constData());
			}
			srcFile.close();
		}
		else
		{
			qWarning("Could not open translation source file '%s'.",srcFile.fileName().toLocal8Bit().constData());
		}
	}
	return 0;
}
예제 #2
0
QString QgsPathResolver::writePath( const QString &src ) const
{
  if ( mBaseFileName.isEmpty() || src.isEmpty() )
  {
    return src;
  }

  QFileInfo pfi( mBaseFileName );
  QString projPath = pfi.absoluteFilePath();

  if ( projPath.isEmpty() )
  {
    return src;
  }

  QFileInfo srcFileInfo( src );
  QString srcPath = srcFileInfo.exists() ? srcFileInfo.canonicalFilePath() : src;

  // if this is a VSIFILE, remove the VSI prefix and append to final result
  QString vsiPrefix = qgsVsiPrefix( src );
  if ( ! vsiPrefix.isEmpty() )
  {
    srcPath.remove( 0, vsiPrefix.size() );
  }

#if defined( Q_OS_WIN )
  const Qt::CaseSensitivity cs = Qt::CaseInsensitive;

  srcPath.replace( '\\', '/' );

  if ( srcPath.startsWith( "//" ) )
  {
    // keep UNC prefix
    srcPath = "\\\\" + srcPath.mid( 2 );
  }

  projPath.replace( '\\', '/' );
  if ( projPath.startsWith( "//" ) )
  {
    // keep UNC prefix
    projPath = "\\\\" + projPath.mid( 2 );
  }
#else
  const Qt::CaseSensitivity cs = Qt::CaseSensitive;
#endif

  QStringList projElems = mBaseFileName.split( '/', QString::SkipEmptyParts );
  QStringList srcElems = srcPath.split( '/', QString::SkipEmptyParts );

  // remove project file element
  projElems.removeLast();

  projElems.removeAll( QStringLiteral( "." ) );
  srcElems.removeAll( QStringLiteral( "." ) );

  // remove common part
  int n = 0;
  while ( !srcElems.isEmpty() &&
          !projElems.isEmpty() &&
          srcElems[0].compare( projElems[0], cs ) == 0 )
  {
    srcElems.removeFirst();
    projElems.removeFirst();
    n++;
  }

  if ( n == 0 )
  {
    // no common parts; might not even by a file
    return src;
  }

  if ( !projElems.isEmpty() )
  {
    // go up to the common directory
    for ( int i = 0; i < projElems.size(); i++ )
    {
      srcElems.insert( 0, QStringLiteral( ".." ) );
    }
  }
  else
  {
    // let it start with . nevertheless,
    // so relative path always start with either ./ or ../
    srcElems.insert( 0, QStringLiteral( "." ) );
  }

  return vsiPrefix + srcElems.join( QStringLiteral( "/" ) );
}