Qt: Add translation support (#664)

* Translation PoC

* i18n but better

* More Greek translation

* Add proper translation UI

* Linux CI: Install qt6-tools-dev
This commit is contained in:
wheremyfoodat 2024-12-04 21:08:33 +02:00 committed by GitHub
parent 28461a1d44
commit bfdc6f0240
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1656 additions and 7 deletions

View file

@ -12,7 +12,7 @@ ConfigWindow::ConfigWindow(ConfigCallback configCallback, MainWindowCallback win
// Set the window title of the main window appropriately if we enable showing the app version on the window
if (config.windowSettings.showAppVersion) {
getMainWindow()->setWindowTitle("Alber v" PANDA3DS_VERSION);
getMainWindow()->setWindowTitle(tr("Alber v%1").arg(PANDA3DS_VERSION));
}
// Initialize the widget list and the widget container widgets
@ -96,6 +96,9 @@ ConfigWindow::ConfigWindow(ConfigCallback configCallback, MainWindowCallback win
});
guiLayout->addRow(tr("Window icon"), iconSelect);
QComboBox* languageSelect = createLanguageSelect();
guiLayout->addRow(tr("Language"), languageSelect);
QCheckBox* showAppVersion = new QCheckBox(tr("Show version on window title"));
showAppVersion->setChecked(config.windowSettings.showAppVersion);
connect(showAppVersion, &QCheckBox::toggled, this, [&](bool checked) {
@ -103,7 +106,7 @@ ConfigWindow::ConfigWindow(ConfigCallback configCallback, MainWindowCallback win
updateConfig();
// Update main window title
getMainWindow()->setWindowTitle(checked ? "Alber v" PANDA3DS_VERSION : "Alber");
getMainWindow()->setWindowTitle(checked ? tr("Alber v%1").arg(PANDA3DS_VERSION) : tr("Alber"));
});
connectCheckbox(showAppVersion, config.windowSettings.showAppVersion);
guiLayout->addRow(showAppVersion);

View file

@ -14,6 +14,9 @@
#include "version.hpp"
MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), keyboardMappings(InputMappings::defaultKeyboardMappings()) {
emu = new Emulator();
loadTranslation();
setWindowTitle(tr("Alber"));
// Enable drop events for loading ROMs
@ -75,7 +78,6 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
auto aboutAction = aboutMenu->addAction(tr("About Panda3DS"));
connect(aboutAction, &QAction::triggered, this, &MainWindow::showAboutMenu);
emu = new Emulator();
emu->setOutputSize(screen->surfaceWidth, screen->surfaceHeight);
// Set up misc objects
@ -678,4 +680,4 @@ void MainWindow::setupControllerSensors(SDL_GameController* controller) {
if (haveAccelerometer) {
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE);
}
}
}

View file

@ -0,0 +1,91 @@
#include <QFile>
#include <QTranslator>
#include <array>
#include <cstdio>
#include "panda_qt/config_window.hpp"
#include "panda_qt/main_window.hpp"
void MainWindow::loadTranslation() {
// TODO: This should become a member variable when we allow changing language at runtime.
QTranslator* translator = nullptr;
// Fetch the .qm file for our language and load it
auto language = QString::fromStdString(emu->getConfig().frontendSettings.language);
const QString baseDir = QStringLiteral(":/translations");
const QString basePath = QStringLiteral("%1/%2.qm").arg(baseDir).arg(language);
if (QFile::exists(basePath)) {
if (translator != nullptr) {
qApp->removeTranslator(translator);
}
translator = new QTranslator(qApp);
if (!translator->load(basePath)) {
QMessageBox::warning(
nullptr, QStringLiteral("Translation Error"),
QStringLiteral("Failed to find load translation file for '%1':\n%2").arg(language).arg(basePath)
);
delete translator;
} else {
qApp->installTranslator(translator);
}
} else {
printf("Language file %s does not exist. Defaulting to English\n", basePath.toStdString().c_str());
}
}
struct LanguageInfo {
QString name; // Full name of the language (for example "English (US)")
const char* code; // ISO 639 language code (for example "en_us")
explicit LanguageInfo(const QString& name, const char* code) : name(name), code(code) {}
};
// List of languages in the order they should appear in the menu
// Please keep this list mostly in alphabetical order.
// Also, for Unicode characters in language names, use Unicode keycodes instead of writing out the name,
// as some compilers/toolchains may not enjoy Unicode in source files.
static std::array<LanguageInfo, 2> languages = {
LanguageInfo(QStringLiteral(u"English"), "en"), // English
LanguageInfo(QStringLiteral(u"\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC"), "el"), // Greek
};
QComboBox* ConfigWindow::createLanguageSelect() {
QComboBox* select = new QComboBox();
for (usize i = 0; i < languages.size(); i++) {
const auto& lang = languages[i];
select->addItem(lang.name);
if (config.frontendSettings.language == lang.code) {
select->setCurrentIndex(i);
}
}
connect(select, &QComboBox::currentIndexChanged, this, [&](int index) {
const QString baseDir = QStringLiteral(":/translations");
const QString basePath = QStringLiteral("%1/%2.qm").arg(baseDir).arg(languages[index].code);
if (QFile::exists(basePath)) {
config.frontendSettings.language = languages[index].code;
updateConfig();
QMessageBox messageBox(
QMessageBox::Icon::Information, tr("Language change successful"),
tr("Restart Panda3DS for the new language to be used.")
);
messageBox.exec();
} else {
QMessageBox messageBox(
QMessageBox::Icon::Warning, tr("Language change failed"),
tr("The language you selected is not included in Panda3DS. If you're seeing this, someone messed up the language UI code...")
);
messageBox.exec();
}
});
return select;
}