refactor: I18N use short (ISO639) names to save and load not native strings, the documentation says we use these already

This commit is contained in:
sithlord48
2025-12-03 17:00:37 -05:00
committed by Chris Rizzitello
parent d1e2865319
commit b151666f6b
3 changed files with 39 additions and 11 deletions

View File

@ -111,7 +111,17 @@ I18N::I18N(QObject *parent) : QObject{parent}
QStringList I18N::detectedLanguages()
{
return instance()->m_translations.keys();
return instance()->m_nameMap.values();
}
QString I18N::nativeTo639Name(QString nativeName)
{
return instance()->m_nameMap.key(nativeName);
}
QString I18N::toNativeName(QString shortName)
{
return instance()->m_nameMap.value(shortName);
}
QString I18N::currentLanguage()
@ -159,10 +169,10 @@ void I18N::detectLanguages()
{
const auto oldList = m_translations;
m_translations.clear();
m_nameMap.clear();
QStringList nameFilter = {QStringLiteral("%1_*.qm").arg(kAppId)};
QMap<QString, QString> appTranslations;
QMap<QString, QString> shortToNative;
QStringList detectedLangCodes;
QDir dir(m_appTrPath);
QStringList langList = dir.entryList(nameFilter, QDir::Files, QDir::Name);
@ -184,7 +194,7 @@ void I18N::detectLanguages()
shortCode = longCode.mid(0, 2);
appTranslations.insert(shortCode, translator.filePath());
shortToNative.insert(shortCode, nativeLang);
m_nameMap.insert(shortCode, nativeLang);
detectedLangCodes.append(QStringLiteral("qt_%1.qm").arg(shortCode));
}
@ -200,7 +210,7 @@ void I18N::detectLanguages()
const QStringList keys = appTranslations.keys();
for (const QString &lang : keys)
m_translations.insert(shortToNative.value(lang), {appTranslations.value(lang), qtTranslations.value(lang)});
m_translations.insert(lang, {appTranslations.value(lang), qtTranslations.value(lang)});
if (oldList != m_translations)
Q_EMIT langaugesChanged(m_translations.keys());

View File

@ -25,9 +25,23 @@ public:
*/
static QStringList detectedLanguages();
/**
* @brief nativeTo639Name Convert a native Language name into a 639 name
* @param nativeName English, Español etc..
* @return 639 name for the language e, zh_CN , it, etc..)
*/
static QString nativeTo639Name(QString nativeName);
/**
* @brief toNativeName Convert a 639 Name into a Native Language string
* @param shortName A 639 name en, es etc...
* @return native language string for the language
*/
static QString toNativeName(QString shortName);
/**
* @brief currentLanguage
* @return The current language string (native name: English, Español etc..)
* @return The current language string (639-1 names i.e en, es)
*/
static QString currentLanguage();
@ -45,12 +59,12 @@ public:
Q_SIGNALS:
/**
* @brief languageChanged Emitted when the current language changes
* @param language The current language (native name, i.e English, Español)
* @param language The current language (639-1 names i.e en, es)
*/
void languageChanged(const QString language);
/**
* @brief langaugesChanged Emitted when the detected languages changes
* @param languages The current list of languages (native names i.e English, Español..)
* @param languages The current list of languages (639-1 names i.e en, es)
*/
void langaugesChanged(const QStringList languages);
@ -63,8 +77,9 @@ private:
void detectLanguages();
QMap<QString, QStringList> m_translations;
QMap<QString, QString> m_nameMap;
QList<QTranslator *> m_currentTranslations;
QString m_currentLang = QStringLiteral("English");
QString m_currentLang = QStringLiteral("en");
QString m_appTrPath;
QString m_qtTrPath;
};

View File

@ -34,7 +34,7 @@ SettingsDialog::SettingsDialog(QWidget *parent, const IServerConfig &serverConfi
// set up the language combo
I18N::reDetectLanguages();
ui->comboLanguage->addItems(I18N::detectedLanguages());
ui->comboLanguage->setCurrentText(I18N::currentLanguage());
ui->comboLanguage->setCurrentText(I18N::toNativeName(I18N::currentLanguage()));
updateText();
@ -83,7 +83,10 @@ void SettingsDialog::initConnections() const
connect(ui->btnBrowseLog, &QPushButton::clicked, this, &SettingsDialog::browseLogPath);
connect(ui->cbLogToFile, &QCheckBox::toggled, this, &SettingsDialog::setLogToFile);
connect(ui->comboLogLevel, &QComboBox::currentIndexChanged, this, &SettingsDialog::logLevelChanged);
connect(ui->comboLanguage, &QComboBox::currentTextChanged, I18N::instance(), &I18N::setLanguage);
connect(ui->comboLanguage, &QComboBox::currentTextChanged, this, [](const QString &lang) {
const auto shortName = I18N::nativeTo639Name(lang);
I18N::setLanguage(shortName);
});
}
void SettingsDialog::regenCertificates()
@ -174,7 +177,7 @@ void SettingsDialog::accept()
Settings::setValue(Settings::Gui::SymbolicTrayIcon, ui->rbIconMono->isChecked());
Settings::setValue(Settings::Security::CheckPeers, ui->cbRequireClientCert->isChecked());
Settings::setValue(Settings::Client::ScrollSpeed, ui->sbScrollSpeed->value());
Settings::setValue(Settings::Core::Language, ui->comboLanguage->currentText());
Settings::setValue(Settings::Core::Language, I18N::nativeTo639Name(ui->comboLanguage->currentText()));
Settings::setValue(Settings::Log::GuiDebug, ui->cbGuiDebug->isChecked());
Settings::setValue(Settings::Core::UseWlClipboard, ui->cbUseWlClipboard->isChecked());