| 68 | } |
| 69 | |
| 70 | bool TranslationHandler::setLanguage(const QString &code) |
| 71 | { |
| 72 | bool failure = false; |
| 73 | QString error; |
| 74 | |
| 75 | //If English is the language. Code can be e.g. en_US |
| 76 | if (code.indexOf("en") == 0) { |
| 77 | //Just remove all extra translators |
| 78 | if (mTranslator) { |
| 79 | qApp->removeTranslator(mTranslator); |
| 80 | delete mTranslator; |
| 81 | mTranslator = nullptr; |
| 82 | } |
| 83 | |
| 84 | mCurrentLanguage = code; |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | //Make sure the translator is otherwise valid |
| 89 | const int index = getLanguageIndexByCode(code); |
| 90 | if (index == -1) { |
| 91 | error = QObject::tr("Unknown language specified!"); |
| 92 | failure = true; |
| 93 | } else { |
| 94 | // Make sure there is a translator |
| 95 | if (!mTranslator) |
| 96 | mTranslator = new QTranslator(this); |
| 97 | |
| 98 | //Load the new language |
| 99 | const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); |
| 100 | |
| 101 | QString datadir = getDataDir(); |
| 102 | |
| 103 | QString translationFile; |
| 104 | if (QFile::exists(datadir + "/lang/" + mTranslations[index].mFilename + ".qm")) |
| 105 | translationFile = datadir + "/lang/" + mTranslations[index].mFilename + ".qm"; |
| 106 | |
| 107 | else if (QFile::exists(datadir + "/" + mTranslations[index].mFilename + ".qm")) |
| 108 | translationFile = datadir + "/" + mTranslations[index].mFilename + ".qm"; |
| 109 | |
| 110 | else |
| 111 | translationFile = appPath + "/" + mTranslations[index].mFilename + ".qm"; |
| 112 | |
| 113 | if (!mTranslator->load(translationFile)) { |
| 114 | failure = true; |
| 115 | //If it failed, lets check if the default file exists |
| 116 | if (!QFile::exists(translationFile)) { |
| 117 | error = QObject::tr("Language file %1 not found!"); |
| 118 | error = error.arg(translationFile); |
| 119 | } |
| 120 | else { |
| 121 | //If file exists, there's something wrong with it |
| 122 | error = QObject::tr("Failed to load translation for language %1 from file %2"); |
| 123 | error = error.arg(mTranslations[index].mName); |
| 124 | error = error.arg(translationFile); |
| 125 | } |
| 126 | } |
| 127 | } |
nothing calls this directly
no test coverage detected