tools/linguist/lrelease/main.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    63 static void initBinaryDir(
    63 static void initBinaryDir(
    64 #ifndef Q_OS_WIN
    64 #ifndef Q_OS_WIN
    65         const char *argv0
    65         const char *argv0
    66 #endif
    66 #endif
    67         );
    67         );
       
    68 
       
    69 struct LR {
       
    70     static inline QString tr(const char *sourceText, const char *comment = 0)
       
    71     {
       
    72         return QCoreApplication::translate("LRelease", sourceText, comment);
       
    73     }
       
    74 };
       
    75 #else
       
    76 class LR {
       
    77     Q_DECLARE_TR_FUNCTIONS(LRelease)
       
    78 };
    68 #endif
    79 #endif
    69 
    80 
    70 static void printOut(const QString & out)
    81 static void printOut(const QString & out)
    71 {
    82 {
    72     QTextStream stream(stdout);
    83     QTextStream stream(stdout);
    73     stream << out;
    84     stream << out;
    74 }
    85 }
    75 
    86 
    76 static void printUsage()
    87 static void printUsage()
    77 {
    88 {
    78     printOut(QCoreApplication::tr(
    89     printOut(LR::tr(
    79         "Usage:\n"
    90         "Usage:\n"
    80         "    lrelease [options] project-file\n"
    91         "    lrelease [options] project-file\n"
    81         "    lrelease [options] ts-files [-qm qm-file]\n\n"
    92         "    lrelease [options] ts-files [-qm qm-file]\n\n"
    82         "lrelease is part of Qt's Linguist tool chain. It can be used as a\n"
    93         "lrelease is part of Qt's Linguist tool chain. It can be used as a\n"
    83         "stand-alone tool to convert XML-based translations files in the TS\n"
    94         "stand-alone tool to convert XML-based translations files in the TS\n"
   106 static bool loadTsFile(Translator &tor, const QString &tsFileName, bool /* verbose */)
   117 static bool loadTsFile(Translator &tor, const QString &tsFileName, bool /* verbose */)
   107 {
   118 {
   108     ConversionData cd;
   119     ConversionData cd;
   109     bool ok = tor.load(tsFileName, cd, QLatin1String("auto"));
   120     bool ok = tor.load(tsFileName, cd, QLatin1String("auto"));
   110     if (!ok) {
   121     if (!ok) {
   111         std::cerr << "lrelease error: " << qPrintable(cd.error());
   122         std::cerr << qPrintable(LR::tr("lrelease error: %1").arg(cd.error()));
   112     } else {
   123     } else {
   113         if (!cd.errors().isEmpty())
   124         if (!cd.errors().isEmpty())
   114             printOut(cd.error());
   125             printOut(cd.error());
   115     }
   126     }
   116     cd.clearErrors();
   127     cd.clearErrors();
   121     ConversionData &cd, bool removeIdentical)
   132     ConversionData &cd, bool removeIdentical)
   122 {
   133 {
   123     tor.reportDuplicates(tor.resolveDuplicates(), qmFileName, cd.isVerbose());
   134     tor.reportDuplicates(tor.resolveDuplicates(), qmFileName, cd.isVerbose());
   124 
   135 
   125     if (cd.isVerbose())
   136     if (cd.isVerbose())
   126         printOut(QCoreApplication::tr( "Updating '%1'...\n").arg(qmFileName));
   137         printOut(LR::tr("Updating '%1'...\n").arg(qmFileName));
   127     if (removeIdentical) {
   138     if (removeIdentical) {
   128         if (cd.isVerbose())
   139         if (cd.isVerbose())
   129             printOut(QCoreApplication::tr( "Removing translations equal to source text in '%1'...\n").arg(qmFileName));
   140             printOut(LR::tr("Removing translations equal to source text in '%1'...\n").arg(qmFileName));
   130         tor.stripIdenticalSourceTranslations();
   141         tor.stripIdenticalSourceTranslations();
   131     }
   142     }
   132 
   143 
   133     QFile file(qmFileName);
   144     QFile file(qmFileName);
   134     if (!file.open(QIODevice::WriteOnly)) {
   145     if (!file.open(QIODevice::WriteOnly)) {
   135         std::cerr << "lrelease error: cannot create '" << qPrintable(qmFileName)
   146         std::cerr << qPrintable(LR::tr("lrelease error: cannot create '%1': %2\n")
   136                   << "': " << qPrintable(file.errorString()) << std::endl;
   147                                 .arg(qmFileName, file.errorString()));
   137         return false;
   148         return false;
   138     }
   149     }
   139 
   150 
   140     tor.normalizeTranslations(cd);
   151     tor.normalizeTranslations(cd);
   141     bool ok = tor.release(&file, cd);
   152     bool ok = tor.release(&file, cd);
   142     file.close();
   153     file.close();
   143 
   154 
   144     if (!ok) {
   155     if (!ok) {
   145         std::cerr << "lrelease error: cannot save '" << qPrintable(qmFileName)
   156         std::cerr << qPrintable(LR::tr("lrelease error: cannot save '%1': %2")
   146                   << "': " << qPrintable(cd.error());
   157                                 .arg(qmFileName, cd.error()));
   147     } else if (!cd.errors().isEmpty()) {
   158     } else if (!cd.errors().isEmpty()) {
   148         printOut(cd.error());
   159         printOut(cd.error());
   149     }
   160     }
   150     cd.clearErrors();
   161     cd.clearErrors();
   151     return ok;
   162     return ok;
   179 #endif
   190 #endif
   180             );
   191             );
   181 #else
   192 #else
   182     QCoreApplication app(argc, argv);
   193     QCoreApplication app(argc, argv);
   183     QTranslator translator;
   194     QTranslator translator;
   184     if (translator.load(QLatin1String("lrelease_") + QLocale::system().name()))
   195     QTranslator qtTranslator;
       
   196     QString sysLocale = QLocale::system().name();
       
   197     QString resourceDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
       
   198     if (translator.load(QLatin1String("linguist_") + sysLocale, resourceDir)
       
   199         && qtTranslator.load(QLatin1String("qt_") + sysLocale, resourceDir)) {
   185         app.installTranslator(&translator);
   200         app.installTranslator(&translator);
       
   201         app.installTranslator(&qtTranslator);
       
   202     }
   186 #endif
   203 #endif
   187 
   204 
   188     ConversionData cd;
   205     ConversionData cd;
   189     cd.m_verbose = true; // the default is true starting with Qt 4.2
   206     cd.m_verbose = true; // the default is true starting with Qt 4.2
   190     bool removeIdentical = false;
   207     bool removeIdentical = false;
   219             continue;
   236             continue;
   220         } else if (!strcmp(argv[i], "-verbose")) {
   237         } else if (!strcmp(argv[i], "-verbose")) {
   221             cd.m_verbose = true;
   238             cd.m_verbose = true;
   222             continue;
   239             continue;
   223         } else if (!strcmp(argv[i], "-version")) {
   240         } else if (!strcmp(argv[i], "-version")) {
   224             printOut(QCoreApplication::tr( "lrelease version %1\n").arg(QLatin1String(QT_VERSION_STR)) );
   241             printOut(LR::tr("lrelease version %1\n").arg(QLatin1String(QT_VERSION_STR)));
   225             return 0;
   242             return 0;
   226         } else if (!strcmp(argv[i], "-qm")) {
   243         } else if (!strcmp(argv[i], "-qm")) {
   227             if (i == argc - 1) {
   244             if (i == argc - 1) {
   228                 printUsage();
   245                 printUsage();
   229                 return 1;
   246                 return 1;
   253 
   270 
   254             ProFileEvaluator visitor;
   271             ProFileEvaluator visitor;
   255             visitor.setVerbose(cd.isVerbose());
   272             visitor.setVerbose(cd.isVerbose());
   256 
   273 
   257             if (!visitor.queryProFile(&pro)) {
   274             if (!visitor.queryProFile(&pro)) {
   258                 std::cerr << "lrelease error: cannot read project file '"
   275                 std::cerr << qPrintable(LR::tr(
   259                           << qPrintable(inputFile) << "'.\n";
   276                           "lrelease error: cannot read project file '%1'.\n")
       
   277                           .arg(inputFile));
   260                 continue;
   278                 continue;
   261             }
   279             }
   262             if (!visitor.accept(&pro)) {
   280             if (!visitor.accept(&pro)) {
   263                 std::cerr << "lrelease error: cannot process project file '"
   281                 std::cerr << qPrintable(LR::tr(
   264                           << qPrintable(inputFile) << "'.\n";
   282                           "lrelease error: cannot process project file '%1'.\n")
       
   283                           .arg(inputFile));
   265                 continue;
   284                 continue;
   266             }
   285             }
   267 
   286 
   268             QStringList translations = visitor.values(QLatin1String("TRANSLATIONS"));
   287             QStringList translations = visitor.values(QLatin1String("TRANSLATIONS"));
   269             if (translations.isEmpty()) {
   288             if (translations.isEmpty()) {
   270                 std::cerr << "lrelease warning: Met no 'TRANSLATIONS' entry in project file '"
   289                 std::cerr << qPrintable(LR::tr(
   271                           << qPrintable(inputFile) << "'\n";
   290                           "lrelease warning: Met no 'TRANSLATIONS' entry in project file '%1'\n")
       
   291                           .arg(inputFile));
   272             } else {
   292             } else {
   273                 QDir proDir(fi.absolutePath());
   293                 QDir proDir(fi.absolutePath());
   274                 foreach (const QString &trans, translations)
   294                 foreach (const QString &trans, translations)
   275                     if (!releaseTsFile(QFileInfo(proDir, trans).filePath(), cd, removeIdentical))
   295                     if (!releaseTsFile(QFileInfo(proDir, trans).filePath(), cd, removeIdentical))
   276                         return 1;
   296                         return 1;