chat: translation tweaks (#2797)

Signed-off-by: Jared Van Bortel <jared@nomic.ai>
This commit is contained in:
Jared Van Bortel 2024-08-08 13:41:47 -04:00 committed by GitHub
parent 0fcf1dda5f
commit d59b1331f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 33 deletions

View File

@ -32,8 +32,8 @@ project(gpt4all VERSION ${APP_VERSION_BASE} LANGUAGES CXX C)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
option(GPT4ALL_TRANSLATIONS OFF "Build with translations") option(GPT4ALL_TRANSLATIONS "Build with translations" OFF)
option(GPT4ALL_LOCALHOST OFF "Build installer for localhost repo") option(GPT4ALL_LOCALHOST "Build installer for localhost repo" OFF)
option(GPT4ALL_OFFLINE_INSTALLER "Build an offline installer" OFF) option(GPT4ALL_OFFLINE_INSTALLER "Build an offline installer" OFF)
option(GPT4ALL_SIGN_INSTALL "Sign installed binaries and installers (requires signing identities)" 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) if (GPT4ALL_TRANSLATIONS)
qt_add_translations(chat qt_add_translations(chat
TS_FILES 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_es_MX.ts
${CMAKE_SOURCE_DIR}/translations/gpt4all_zh_CN.ts ${CMAKE_SOURCE_DIR}/translations/gpt4all_zh_CN.ts
${CMAKE_SOURCE_DIR}/translations/gpt4all_zh_TW.ts ${CMAKE_SOURCE_DIR}/translations/gpt4all_zh_TW.ts

View File

@ -593,7 +593,7 @@ QString MySettings::languageAndLocale() const
QString MySettings::filePathForLocale(const QLocale &locale) QString MySettings::filePathForLocale(const QLocale &locale)
{ {
// Check and see if we have a translation for the chosen locale and set it if possible otherwise // 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(); QStringList uiLanguages = locale.uiLanguages();
for (int i = 0; i < uiLanguages.size(); ++i) for (int i = 0; i < uiLanguages.size(); ++i)
uiLanguages[i].replace('-', '_'); uiLanguages[i].replace('-', '_');
@ -604,18 +604,18 @@ QString MySettings::filePathForLocale(const QLocale &locale)
// rather than having to recompile all of GPT4All // rather than having to recompile all of GPT4All
QString directory = modelPath(); QString directory = modelPath();
for (const QString &bcp47Name : uiLanguages) { 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); QFileInfo filePathInfo(filePath);
if (filePathInfo.exists()) return filePath; if (filePathInfo.exists()) return filePath;
} }
// Now scan the internal built-in translations // Now scan the internal built-in translations
for (QString bcp47Name : uiLanguages) { 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); QFileInfo filePathInfo(filePath);
if (filePathInfo.exists()) return 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) 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 we previously installed a translator, then remove it
if (m_translator) { if (m_translator) {
if (!qGuiApp->removeTranslator(m_translator)) { if (!qGuiApp->removeTranslator(m_translator.get())) {
qDebug() << "ERROR: Failed to remove the previous translator"; qDebug() << "ERROR: Failed to remove the previous translator";
} else { } else {
delete m_translator; m_translator.reset();
m_translator = nullptr;
} }
} }
@ -646,24 +645,20 @@ void MySettings::setLanguageAndLocale(const QString &bcp47Name)
Q_ASSERT(!m_translator); Q_ASSERT(!m_translator);
const QString filePath = filePathForLocale(locale); const QString filePath = filePathForLocale(locale);
// Installing the default gpt4all_en.qm fails presumably because it has no strings that are if (!m_translator) {
// different from the ones stored in the binary
if (!m_translator && !filePath.endsWith("en.qm")) {
// Create a new translator object on the heap // Create a new translator object on the heap
m_translator = new QTranslator(this); m_translator = std::make_unique<QTranslator>(this);
bool success = m_translator->load(filePath); bool success = m_translator->load(filePath);
Q_ASSERT(success); Q_ASSERT(success);
if (!success) { if (!success) {
qDebug() << "ERROR: Failed to load translation file:" << filePath; qDebug() << "ERROR: Failed to load translation file:" << filePath;
delete m_translator; m_translator.reset();
m_translator = nullptr;
} }
// If we've successfully loaded it, then try and install it // 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; qDebug() << "ERROR: Failed to install the translator:" << filePath;
delete m_translator; m_translator.reset();
m_translator = nullptr;
} }
} }

View File

@ -11,6 +11,7 @@
#include <QVector> #include <QVector>
#include <cstdint> #include <cstdint>
#include <memory>
#include <optional> #include <optional>
namespace MySettingsEnums { namespace MySettingsEnums {
@ -245,7 +246,7 @@ private:
const QStringList m_deviceList; const QStringList m_deviceList;
const QStringList m_embeddingsDeviceList; const QStringList m_embeddingsDeviceList;
const QStringList m_uiLanguages; const QStringList m_uiLanguages;
QTranslator *m_translator = nullptr; std::unique_ptr<QTranslator> m_translator;
private: private:
explicit MySettings(); explicit MySettings();

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS> <!DOCTYPE TS>
<TS version="2.1" language="en"> <TS version="2.1" language="en_US">
<context> <context>
<name>AddCollectionView</name> <name>AddCollectionView</name>
<message> <message>
@ -1267,18 +1267,18 @@ model to get started</source>
<location filename="../qml/CollectionsDrawer.qml" line="87"/> <location filename="../qml/CollectionsDrawer.qml" line="87"/>
<location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/CollectionsDrawer.qml" line="87"/> <location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/CollectionsDrawer.qml" line="87"/>
<source>%n file(s)</source> <source>%n file(s)</source>
<translation type="unfinished"> <translation>
<numerusform></numerusform> <numerusform>%n file</numerusform>
<numerusform></numerusform> <numerusform>%n files</numerusform>
</translation> </translation>
</message> </message>
<message numerus="yes"> <message numerus="yes">
<location filename="../qml/CollectionsDrawer.qml" line="87"/> <location filename="../qml/CollectionsDrawer.qml" line="87"/>
<location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/CollectionsDrawer.qml" line="87"/> <location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/CollectionsDrawer.qml" line="87"/>
<source>%n word(s)</source> <source>%n word(s)</source>
<translation type="unfinished"> <translation>
<numerusform></numerusform> <numerusform>%n word</numerusform>
<numerusform></numerusform> <numerusform>%n words</numerusform>
</translation> </translation>
</message> </message>
<message> <message>
@ -1722,18 +1722,18 @@ model to get started</source>
<location filename="../qml/LocalDocsView.qml" line="337"/> <location filename="../qml/LocalDocsView.qml" line="337"/>
<location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/LocalDocsView.qml" line="337"/> <location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/LocalDocsView.qml" line="337"/>
<source>%n file(s)</source> <source>%n file(s)</source>
<translation type="unfinished"> <translation>
<numerusform></numerusform> <numerusform>%n file</numerusform>
<numerusform></numerusform> <numerusform>%n files</numerusform>
</translation> </translation>
</message> </message>
<message numerus="yes"> <message numerus="yes">
<location filename="../qml/LocalDocsView.qml" line="337"/> <location filename="../qml/LocalDocsView.qml" line="337"/>
<location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/LocalDocsView.qml" line="337"/> <location filename="../../build_gpt4all-chat_Desktop_Qt_6_7_2/gpt4all/qml/LocalDocsView.qml" line="337"/>
<source>%n word(s)</source> <source>%n word(s)</source>
<translation type="unfinished"> <translation>
<numerusform></numerusform> <numerusform>%n word</numerusform>
<numerusform></numerusform> <numerusform>%n words</numerusform>
</translation> </translation>
</message> </message>
<message> <message>