From d59b1331f9cbca25e3ad190d4c6579b372adcc89 Mon Sep 17 00:00:00 2001 From: Jared Van Bortel Date: Thu, 8 Aug 2024 13:41:47 -0400 Subject: [PATCH] chat: translation tweaks (#2797) Signed-off-by: Jared Van Bortel --- gpt4all-chat/CMakeLists.txt | 6 ++--- gpt4all-chat/mysettings.cpp | 27 ++++++++----------- gpt4all-chat/mysettings.h | 3 ++- .../{gpt4all_en.ts => gpt4all_en_US.ts} | 26 +++++++++--------- 4 files changed, 29 insertions(+), 33 deletions(-) rename gpt4all-chat/translations/{gpt4all_en.ts => gpt4all_en_US.ts} (99%) diff --git a/gpt4all-chat/CMakeLists.txt b/gpt4all-chat/CMakeLists.txt index 09066a4b..1e7066a4 100644 --- a/gpt4all-chat/CMakeLists.txt +++ b/gpt4all-chat/CMakeLists.txt @@ -32,8 +32,8 @@ project(gpt4all VERSION ${APP_VERSION_BASE} LANGUAGES CXX C) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) -option(GPT4ALL_TRANSLATIONS OFF "Build with translations") -option(GPT4ALL_LOCALHOST OFF "Build installer for localhost repo") +option(GPT4ALL_TRANSLATIONS "Build with translations" OFF) +option(GPT4ALL_LOCALHOST "Build installer for localhost repo" OFF) option(GPT4ALL_OFFLINE_INSTALLER "Build an offline installer" OFF) option(GPT4ALL_SIGN_INSTALL "Sign installed binaries and installers (requires signing identities)" OFF) @@ -231,7 +231,7 @@ qt_add_qml_module(chat if (GPT4ALL_TRANSLATIONS) qt_add_translations(chat TS_FILES - ${CMAKE_SOURCE_DIR}/translations/gpt4all_en.ts + ${CMAKE_SOURCE_DIR}/translations/gpt4all_en_US.ts ${CMAKE_SOURCE_DIR}/translations/gpt4all_es_MX.ts ${CMAKE_SOURCE_DIR}/translations/gpt4all_zh_CN.ts ${CMAKE_SOURCE_DIR}/translations/gpt4all_zh_TW.ts diff --git a/gpt4all-chat/mysettings.cpp b/gpt4all-chat/mysettings.cpp index b29ec431..a1fc4fa4 100644 --- a/gpt4all-chat/mysettings.cpp +++ b/gpt4all-chat/mysettings.cpp @@ -593,7 +593,7 @@ QString MySettings::languageAndLocale() const QString MySettings::filePathForLocale(const QLocale &locale) { // Check and see if we have a translation for the chosen locale and set it if possible otherwise - // we return the filepath for the 'en' translation + // we return the filepath for the 'en_US' translation QStringList uiLanguages = locale.uiLanguages(); for (int i = 0; i < uiLanguages.size(); ++i) uiLanguages[i].replace('-', '_'); @@ -604,18 +604,18 @@ QString MySettings::filePathForLocale(const QLocale &locale) // rather than having to recompile all of GPT4All QString directory = modelPath(); for (const QString &bcp47Name : uiLanguages) { - QString filePath = QString("%1/gpt4all_%2.qm").arg(directory).arg(bcp47Name); + QString filePath = u"%1/gpt4all_%2.qm"_s.arg(directory, bcp47Name); QFileInfo filePathInfo(filePath); if (filePathInfo.exists()) return filePath; } // Now scan the internal built-in translations for (QString bcp47Name : uiLanguages) { - QString filePath = QString(":/i18n/gpt4all_%1.qm").arg(bcp47Name); + QString filePath = u":/i18n/gpt4all_%1.qm"_s.arg(bcp47Name); QFileInfo filePathInfo(filePath); if (filePathInfo.exists()) return filePath; } - return QString(":/i18n/gpt4all_en.qm"); + return u":/i18n/gpt4all_en_US.qm"_s; } void MySettings::setLanguageAndLocale(const QString &bcp47Name) @@ -634,11 +634,10 @@ void MySettings::setLanguageAndLocale(const QString &bcp47Name) // If we previously installed a translator, then remove it if (m_translator) { - if (!qGuiApp->removeTranslator(m_translator)) { + if (!qGuiApp->removeTranslator(m_translator.get())) { qDebug() << "ERROR: Failed to remove the previous translator"; } else { - delete m_translator; - m_translator = nullptr; + m_translator.reset(); } } @@ -646,24 +645,20 @@ void MySettings::setLanguageAndLocale(const QString &bcp47Name) Q_ASSERT(!m_translator); const QString filePath = filePathForLocale(locale); - // Installing the default gpt4all_en.qm fails presumably because it has no strings that are - // different from the ones stored in the binary - if (!m_translator && !filePath.endsWith("en.qm")) { + if (!m_translator) { // Create a new translator object on the heap - m_translator = new QTranslator(this); + m_translator = std::make_unique(this); bool success = m_translator->load(filePath); Q_ASSERT(success); if (!success) { qDebug() << "ERROR: Failed to load translation file:" << filePath; - delete m_translator; - m_translator = nullptr; + m_translator.reset(); } // If we've successfully loaded it, then try and install it - if (!qGuiApp->installTranslator(m_translator)) { + if (!qGuiApp->installTranslator(m_translator.get())) { qDebug() << "ERROR: Failed to install the translator:" << filePath; - delete m_translator; - m_translator = nullptr; + m_translator.reset(); } } diff --git a/gpt4all-chat/mysettings.h b/gpt4all-chat/mysettings.h index 64eb54c5..3db8b234 100644 --- a/gpt4all-chat/mysettings.h +++ b/gpt4all-chat/mysettings.h @@ -11,6 +11,7 @@ #include #include +#include #include namespace MySettingsEnums { @@ -245,7 +246,7 @@ private: const QStringList m_deviceList; const QStringList m_embeddingsDeviceList; const QStringList m_uiLanguages; - QTranslator *m_translator = nullptr; + std::unique_ptr m_translator; private: explicit MySettings(); diff --git a/gpt4all-chat/translations/gpt4all_en.ts b/gpt4all-chat/translations/gpt4all_en_US.ts similarity index 99% rename from gpt4all-chat/translations/gpt4all_en.ts rename to gpt4all-chat/translations/gpt4all_en_US.ts index e74a45e9..83ed70fd 100644 --- a/gpt4all-chat/translations/gpt4all_en.ts +++ b/gpt4all-chat/translations/gpt4all_en_US.ts @@ -1,6 +1,6 @@ - + AddCollectionView @@ -1267,18 +1267,18 @@ model to get started %n file(s) - - - + + %n file + %n files %n word(s) - - - + + %n word + %n words @@ -1722,18 +1722,18 @@ model to get started %n file(s) - - - + + %n file + %n files %n word(s) - - - + + %n word + %n words