| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "miscellaneous/localization.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "miscellaneous/settings.h" |
| 7 | |
| 8 | #include <QDir> |
| 9 | #include <QFileInfoList> |
| 10 | #include <QLocale> |
| 11 | #include <QTranslator> |
| 12 | |
| 13 | Localization::Localization(QObject* parent) : QObject(parent) {} |
| 14 | |
| 15 | Localization::~Localization() = default; |
| 16 | |
| 17 | QString Localization::desiredLanguage() const { |
| 18 | return qApp->settings()->value(GROUP(General), SETTING(General::Language)).toString(); |
| 19 | } |
| 20 | |
| 21 | void Localization::loadActiveLanguage() { |
| 22 | auto* qt_translator = new QTranslator(qApp); |
| 23 | auto* app_translator = new QTranslator(qApp); |
| 24 | QString desired_localization = desiredLanguage(); |
| 25 | |
| 26 | qDebugNN << LOGSEC_CORE << "Starting to load active localization. Desired localization is" |
| 27 | << QUOTE_W_SPACE_DOT(desired_localization); |
| 28 | |
| 29 | if (app_translator->load(QLocale(desired_localization), QSL("rssguard"), QSL("_"), APP_LANG_PATH)) { |
| 30 | const QString real_loaded_locale = app_translator->language(); |
| 31 | |
| 32 | QCoreApplication::installTranslator(app_translator); |
| 33 | |
| 34 | qDebugNN << LOGSEC_CORE << "Application localization" << QUOTE_W_SPACE(desired_localization) |
| 35 | << "loaded successfully, specifically sublocalization" << QUOTE_W_SPACE(real_loaded_locale) |
| 36 | << "was loaded."; |
| 37 | desired_localization = real_loaded_locale; |
| 38 | } |
| 39 | else { |
| 40 | qWarningNN << LOGSEC_CORE << "Application localization" << QUOTE_W_SPACE(desired_localization) |
| 41 | << "was not loaded. Loading" << QUOTE_W_SPACE(DEFAULT_LOCALE) << "instead."; |
| 42 | desired_localization = QSL(DEFAULT_LOCALE); |
| 43 | |
| 44 | if (!app_translator->load(QLocale(desired_localization), QSL("rssguard"), QSL("_"), APP_LANG_PATH)) { |
| 45 | qCriticalNN << LOGSEC_CORE << "Even default localzation was not loaded."; |
| 46 | |
| 47 | QCoreApplication::installTranslator(app_translator); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (qt_translator->load(QLocale(desired_localization), QSL("qtbase"), QSL("_"), APP_LANG_PATH)) { |
| 52 | QCoreApplication::installTranslator(qt_translator); |
| 53 | |
| 54 | qDebugNN << LOGSEC_CORE << "Qt localization" << QUOTE_W_SPACE(desired_localization) << "loaded successfully."; |
| 55 | } |
| 56 | else { |
| 57 | qWarningNN << LOGSEC_CORE << "Qt localization" << QUOTE_W_SPACE(desired_localization) |
| 58 | << "WAS NOT loaded successfully."; |
| 59 | } |
| 60 | |
| 61 | m_loadedLanguage = desired_localization; |
| 62 | m_loadedLocale = QLocale(desired_localization); |
| 63 | |
| 64 | QLocale::setDefault(m_loadedLocale); |
| 65 | } |
| 66 | |
| 67 | QList<Language> Localization::installedLanguages() const { |
| 68 | QList<Language> languages; |
| 69 | const QDir file_dir(APP_LANG_PATH); |
| 70 | QTranslator translator; |
| 71 | auto lang_files = |
| 72 | file_dir.entryInfoList(QStringList() << QSL("rssguard_*.qm"), QDir::Filter::Files, QDir::SortFlag::Name); |
| 73 | |
| 74 | // Iterate all found language files. |
| 75 | for (const QFileInfo& file : std::as_const(lang_files)) { |
| 76 | if (translator.load(file.absoluteFilePath())) { |
| 77 | Language new_language; |
| 78 | |
| 79 | new_language.m_code = translator.language(); |
| 80 | new_language.m_name = QLocale(new_language.m_code).nativeLanguageName(); |
| 81 | languages << new_language; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return languages; |
| 86 | } |