Skip to content

Commit

Permalink
Fix bugs, enable all language translations
Browse files Browse the repository at this point in the history
  • Loading branch information
SunderB committed Jan 29, 2021
1 parent efc9a5e commit d5a425a
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 130 deletions.
2 changes: 1 addition & 1 deletion app/gui/qt/SonicPi.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
<file>lang/sonic-pi_ug.qm</file>
<file>lang/sonic-pi_uk.qm</file>
<file>lang/sonic-pi_vi.qm</file>
<file>lang/sonic-pi_zh-Hans.qm</file>
<file>lang/sonic-pi_zh_Hans.qm</file>
<file>lang/sonic-pi_zh_HK.qm</file>
<file>lang/sonic-pi_zh.qm</file>
<file>lang/sonic-pi_zh_TW.qm</file>
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions app/gui/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <QPixmap>
#include <QBitmap>
#include <QLabel>
#include <QTranslator>
#include <QSettings>
#include <QLibraryInfo>

#include "mainwindow.h"
Expand Down
64 changes: 34 additions & 30 deletions app/gui/qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ MainWindow::MainWindow(QApplication &app, QSplashScreen* splash)
initProcess->waitForFinished();

// Throw all stdout into ~/.sonic-pi/log/gui.log
//setupLogPathAndRedirectStdOut();
setupLogPathAndRedirectStdOut();

std::cout << "[GUI] - Welcome to the Sonic Pi GUI" << std::endl;
std::cout << "[GUI] - ===========================" << std::endl;
Expand Down Expand Up @@ -1944,39 +1944,41 @@ void MainWindow::changeSystemPreAmp(int val, int silent)
statusBar()->showMessage(tr("Updating System Volume..."), 2000);
}

// TODO: Implement real-time language switching
void MainWindow::changeUILanguage(QString lang) {
if (lang != piSettings->language) {
std::cout << "New language selected: " << lang.toUtf8().constData() << std::endl;
QString old_lang = sonicPii18n->getNativeLanguageName(piSettings->language);
QString new_lang = sonicPii18n->getNativeLanguageName(lang);

// Load new language
//QString language = sonicPii18n->determineUILanguage(lang);
//sonicPii18n->loadTranslations(language);
//QString title_new = tr("Updated the UI language from %s to %s").arg();

QMessageBox msgBox(this);
msgBox.setText(tr("You've selected a new language: %s").arg(new_lang));
msgBox.setInformativeText(tr("Do you want to apply these new settings?\nApplying the new language will restart Sonic Pi."));
QPushButton *restartButton = msgBox.addButton(tr("Apply & Restart"), QMessageBox::ActionRole);
QPushButton *dismissButton = msgBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
msgBox.setDefaultButton(restartButton);
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();

if (msgBox.clickedButton() == (QAbstractButton*)restartButton) {
piSettings->language = lang;
writeSettings();
restartApp();
//statusBar()->showMessage(tr("Updated UI language setting, please restart Sonic Pi to apply it"), 2000);
} else if (msgBox.clickedButton() == (QAbstractButton*)dismissButton) {
settingsWidget->updateSelectedUILanguage(piSettings->language);
}
std::cout << "Current language: " << piSettings->language.toUtf8().constData() << std::endl;
std::cout << "New language selected: " << lang.toUtf8().constData() << std::endl;
QString old_lang = sonicPii18n->getNativeLanguageName(piSettings->language);
QString new_lang = sonicPii18n->getNativeLanguageName(lang);

// Load new language
//QString language = sonicPii18n->determineUILanguage(lang);
//sonicPii18n->loadTranslations(language);
//QString title_new = tr("Updated the UI language from %s to %s").arg();

QMessageBox msgBox(this);
msgBox.setText(QString(tr("You've selected a new language: %1")).arg(new_lang));
msgBox.setInformativeText(tr("Do you want to apply this language?\nApplying the new language will restart Sonic Pi."));
QPushButton *restartButton = msgBox.addButton(tr("Apply and Restart"), QMessageBox::ActionRole);
QPushButton *dismissButton = msgBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
msgBox.setDefaultButton(restartButton);
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();

if (msgBox.clickedButton() == (QAbstractButton*)restartButton) {
piSettings->language = lang;
writeSettings();
restartApp();
//statusBar()->showMessage(tr("Updated UI language setting, please restart Sonic Pi to apply it"), 2000);
} else if (msgBox.clickedButton() == (QAbstractButton*)dismissButton) {
// Don't apply the new language settings
settingsWidget->updateSelectedUILanguage(piSettings->language);
}

// Load previously set language
//sonicPii18n->loadTranslations(ui_language);
// Load previously set language
//sonicPii18n->loadTranslations(ui_language);
}

}


Expand Down Expand Up @@ -3916,6 +3918,8 @@ void MainWindow::updateContext(int line, int index){

}

// TODO: Implement real-time language switching
// This is currently unused, but put in place to aid in implementing real-time language switching in the future
void MainWindow::updateTranslatedUIText() {
// Widget titles
prefsWidget->setWindowTitle(tr("Preferences"));
Expand Down
3 changes: 1 addition & 2 deletions app/gui/qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class MainWindow : public QMainWindow

// void initPrefsWindow();
void initDocsWindow(QString language);
void addTutorialDocsTab(QString lang);
void addLangDocsTab();
void addSynthDocsTab();
void addFXDocsTab();
Expand Down Expand Up @@ -324,8 +325,6 @@ class MainWindow : public QMainWindow
void addUniversalCopyShortcuts(QTextEdit *te);
void updateTranslatedUIText();

void restartApp();

QMenu *liveMenu, *codeMenu, *audioMenu, *displayMenu, *viewMenu, *ioMenu, *ioMidiInMenu, *ioMidiOutMenu, *ioMidiOutChannelMenu, *localIpAddressesMenu, *themeMenu, *scopeKindVisibilityMenu;

SonicPiSettings *piSettings;
Expand Down
18 changes: 0 additions & 18 deletions app/gui/qt/ruby_help.tmpl

This file was deleted.

20 changes: 12 additions & 8 deletions app/gui/qt/utils/lang_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
std::map<QString, QString> SonicPii18n::native_language_names = {
{"system_locale", ""},
{"bg", "български"},
{"bn", "বাংলা"},
{"bs", "Bosanski/босански"},
{"ca", "Català"},
{"ca@valencia", "Valencià"},
{"cs", "Čeština"},
{"da", "Dansk"},
{"de", "Deutsch"},
{"el", "ελληνικά"},
{"en-GB", "English (UK)"},
{"en-US", "English (US)"},
{"en_AU", "English (Australian)"},
{"en_GB", "English (UK)"},
{"en_US", "English (US)"},
{"eo", "Esperanto"},
{"es", "Español"},
{"et", "Eesti keel"},
{"fa", "فارسی"},
Expand All @@ -50,22 +54,22 @@ std::map<QString, QString> SonicPii18n::native_language_names = {
{"nl", "Nederlands"},
{"pl", "Polski"},
{"pt", "Português"},
{"pt-BR", "Português do Brasil"},
{"pt_BR", "Português do Brasil"},
{"ro", "Română"},
{"ru", "Pусский"},
{"si", "සිංහල"},
{"sk", "Slovenčina"},
{"sl", "Slovenščina"},
{"sv", "Svenska"},
{"sw", "Kiswahili"},
{"ti", "ไทย"},
{"th", "ไทย"},
{"tr", "Türkçe"},
{"ug", "ئۇيغۇر تىلى"},
{"uk", "Українська"},
{"vi", "Tiếng Việt"},
{"zh", "繁體中文"},
{"zh-Hans", "简体中文"},
{"zh-Hk", "廣東話"},
{"zh-TW", "臺灣華語"}
{"zh", "简体中文"},
{"zh_Hans", "简体中文"},
{"zh_HK", "廣東話"},
{"zh_TW", "臺灣華語"}
};
#endif
70 changes: 50 additions & 20 deletions app/gui/qt/utils/sonic_pi_i18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#include <QString>
#include <QStringList>
#include <QLocale>
#include <QLanguage>
#include <QTranslator>
#include <QMainWindow>
#include <QApplication>
#include <QLibraryInfo>

Expand All @@ -16,11 +14,29 @@
SonicPii18n::SonicPii18n(QString rootpath) {
this->root_path = rootpath;
this->available_languages = findAvailableLanguages();
// Set to true unless we can't load the system language
this->system_language_available = true;

// // Print all Qt's language codes for debugging
// QList<QLocale> allLocales = QLocale::matchingLocales(
// QLocale::AnyLanguage,
// QLocale::AnyScript,
// QLocale::AnyCountry);
// QStringList iso639LanguageCodes;
//
// for(const QLocale &locale : allLocales) {
// iso639LanguageCodes << locale.name();
// }
//
// std::cout << iso639LanguageCodes.join("\n").toUtf8().constData() << std::endl;
}

SonicPii18n::~SonicPii18n() {
}

QString SonicPii18n::determineUILanguage(QString lang_pref) {
QStringList available_languages = getAvailableLanguages();
std::cout << available_languages.join("\n").toUtf8().constData() << '\n';
std::cout << available_languages.join("\n").toUtf8().constData() << std::endl;
QLocale locale;

if (lang_pref != "system_locale") {
Expand All @@ -34,22 +50,28 @@ QString SonicPii18n::determineUILanguage(QString lang_pref) {
general_name.truncate(general_name.lastIndexOf('-'));

if (available_languages.contains(general_name)) {
return lang_pref;
return general_name;
}
} else {
QStringList preferred_languages = locale.uiLanguages();
// If the specified language isn't available, or if the setting is set to system_locale...
// ...run through the list of preferred languages
std::cout << "Looping through preferred ui languages" << std::endl;

QString l;
for (int i = 0; i < preferred_languages.length(); i += 1) {
if (available_languages.contains(preferred_languages[i])) {
return lang_pref;
l = preferred_languages[i];
l.replace("-", "_");

std::cout << preferred_languages[i].toUtf8().constData() << std::endl;
if (available_languages.contains(l)) {
return l;
}
}
}

// Fallback to English
// Fpreferred_languages[i]preferred_languages[i]preferred_languages[i]allback to English
this->system_language_available = false;
return "en";
}

Expand All @@ -65,12 +87,16 @@ QStringList SonicPii18n::findAvailableLanguages() {
for (int i = 0; i < fileNames.size(); ++i) {
// get locale extracted by filename
QString locale;
locale = fileNames[i]; // "TranslationExample_de.qm"
locale.truncate(locale.lastIndexOf('.')); // "TranslationExample_de"
locale.remove(0, locale.lastIndexOf("sonic-pi_") + 9); // "de"
locale = fileNames[i]; // "sonic-pi_pt_BR.qm"
locale.truncate(locale.lastIndexOf('.')); // "sonic-pi_pt_BR"
locale.remove(0, locale.lastIndexOf("sonic-pi_") + 9); // "pt_BR"
//locale.replace("_", "-"); // Replace underscores with dashes so it matches the language codes e.g: "pt-BR"
std::cout << locale.toUtf8().constData() << '\n';
languages << locale;
}
// Add the source language
languages << "en_GB";
languages.sort();
return languages;
}

Expand Down Expand Up @@ -99,25 +125,29 @@ bool SonicPii18n::loadTranslations(QString lang) {
}

QStringList SonicPii18n::getAvailableLanguages() {
return self->available_languages;
return this->available_languages;
}

static std::map<QString, QString> SonicPii18n::getLanguageNameList() {
return self->native_language_names;
std::map<QString, QString> SonicPii18n::getNativeLanguageNameList() {
return native_language_names;
}

static QString SonicPii18n::getNativeLanguageName(QString lang) {
if (native_language_names.contains(lang))
return self->native_language_names[lang];
else if (lang == "system_locale") {
return tr("System language")
QString SonicPii18n::getNativeLanguageName(QString lang) {
std::map<QString, QString>::iterator it = native_language_names.find(lang);
if(it != native_language_names.end()) {
// language found
return native_language_names[lang];
} else if (lang == "system_locale") {
return tr("System language");
} else {
std::cout << "Warning: Predefined language name not found'" << lang.toUtf8().constData() << "'" << std::endl;
// Try using QLocale to find the native language name
QLocale locale(lang);
if (locale.language != QLanguage::C) {
QString name = locale.nativeLanguageName();
if (name != "C" && name != "") {
return locale.nativeLanguageName();
} else {
std::cout << "Warning: Invalid language code '" << lang.toUtf8().constData() << "'" << std:endl;
std::cout << "Warning: Invalid language code '" << lang.toUtf8().constData() << "'" << std::endl;
return lang;
}
}
Expand Down
9 changes: 5 additions & 4 deletions app/gui/qt/utils/sonic_pi_i18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
class SonicPii18n : public QObject {
public:
SonicPii18n(QString rootpath);
~SonicPii18n();

public slots:
QString determineUILanguage(QString lang_pref);
QStringList getAvailableLanguages();
static std::map<QString, QString> getLanguageNameList();
static QString getNativeLanguageName(QString lang);
std::map<QString, QString> getNativeLanguageNameList();
QString getNativeLanguageName(QString lang);
bool loadTranslations(QString lang);

bool system_language_available;

private:
QString root_path;
Expand All @@ -36,7 +39,5 @@ public slots:
static std::map<QString, QString> native_language_names;

QStringList findAvailableLanguages();


};
#endif
Loading

0 comments on commit d5a425a

Please sign in to comment.