Skip to content

Commit 0422ed1

Browse files
committed
GUI - Add function to test all found UI translations
1 parent 3c98ff1 commit 0422ed1

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

app/gui/qt/utils/sonic_pi_i18n.cpp

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
SonicPii18n::SonicPii18n(QString rootpath) {
1515
this->root_path = rootpath;
16-
this->available_languages = findAvailableLanguages();
16+
this->system_language_available = true; // Set to true unless we can't load the system language
1717

18-
// Set to true unless we can't load the system language
19-
this->system_language_available = true;
18+
this->available_languages = findAvailableLanguages();
19+
//checkAllTranslations(); // For testing and debugging purposes
2020
}
2121

2222
SonicPii18n::~SonicPii18n() {
@@ -65,22 +65,21 @@ QString SonicPii18n::determineUILanguage(QString lang_pref) {
6565

6666
QStringList SonicPii18n::findAvailableLanguages() {
6767
QStringList languages;
68-
QLocale locale;
6968

7069
QString m_langPath = root_path + "/app/gui/qt/lang";
71-
//std::cout << m_langPath.toUtf8().constData() << "\n";
70+
//std::cout << m_langPath.toUtf8().constData() << std::endl;
7271
QDir dir(m_langPath);
7372
QStringList fileNames = dir.entryList(QStringList("sonic-pi_*.qm"));
7473

7574
for (int i = 0; i < fileNames.size(); ++i) {
76-
// get locale extracted by filename
77-
QString locale;
78-
locale = fileNames[i]; // "sonic-pi_pt_BR.qm"
79-
locale.truncate(locale.lastIndexOf('.')); // "sonic-pi_pt_BR"
80-
locale.remove(0, locale.lastIndexOf("sonic-pi_") + 9); // "pt_BR"
81-
//locale.replace("_", "-"); // Replace underscores with dashes so it matches the language codes e.g: "pt-BR"
82-
//std::cout << locale.toUtf8().constData() << '\n';
83-
languages << locale;
75+
// extract the language from the filename
76+
QString lang;
77+
lang = fileNames[i]; // "sonic-pi_pt_BR.qm"
78+
lang.truncate(lang.lastIndexOf('.')); // "sonic-pi_pt_BR"
79+
lang.remove(0, lang.lastIndexOf("sonic-pi_") + 9); // "pt_BR"
80+
//lang.replace("_", "-"); // Replace underscores with dashes so it matches the language codes e.g: "pt-BR"
81+
//std::cout << lang.toUtf8().constData() << '\n';
82+
languages << lang;
8483
}
8584
// Add the source language
8685
languages << "en_GB";
@@ -101,7 +100,7 @@ bool SonicPii18n::loadTranslations(QString lang) {
101100

102101
i18n = translator.load("sonic-pi_" + language, ":/lang/") || language == "en_GB" || language == "en" || language == "C";
103102
if (!i18n) {
104-
std::cout << language.toUtf8().constData() << ": Language translation not available" << std::endl;
103+
std::cout << "Error: Failed to load language translation for " << language.toUtf8().constData() << std::endl;
105104
language = "en";
106105
}
107106
app->installTranslator(&translator);
@@ -142,3 +141,35 @@ QString SonicPii18n::getNativeLanguageName(QString lang) {
142141
}
143142
}
144143
}
144+
145+
// For testing and debugging purposes
146+
bool SonicPii18n::checkAllTranslations() {
147+
QStringList failed_to_load = QStringList();
148+
bool all_succeeded = true;
149+
150+
std::cout << "==========================================" << std::endl;
151+
std::cout << "Testing all found language translations..." << std::endl;
152+
for (int i = 0; i < this->available_languages.size(); ++i) {
153+
QString lang = this->available_languages[i];
154+
bool success = loadTranslations(lang);
155+
if (success) {
156+
std::cout << lang.toUtf8().constData() << ": ✓" << std::endl;
157+
} else {
158+
std::cout << lang.toUtf8().constData() << ": ✗" << std::endl;
159+
failed_to_load << lang;
160+
all_succeeded = false;
161+
}
162+
}
163+
std::cout << "Done" << std::endl;
164+
165+
std::cout << "------------------------------------------" << std::endl;
166+
if (failed_to_load.length() > 0) {
167+
std::cout << "Some translations failed to load:" << std::endl;
168+
std::cout << failed_to_load.join("\n").toUtf8().constData() << std::endl;
169+
} else {
170+
std::cout << "All found translations loaded successfully :)" << std::endl;
171+
}
172+
std::cout << "==========================================" << std::endl;
173+
174+
return all_succeeded;
175+
}

app/gui/qt/utils/sonic_pi_i18n.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public slots:
2828
std::map<QString, QString> getNativeLanguageNameList();
2929
QString getNativeLanguageName(QString lang);
3030
bool loadTranslations(QString lang);
31-
31+
3232
bool system_language_available;
3333

3434
private:
@@ -39,5 +39,6 @@ public slots:
3939
static std::map<QString, QString> native_language_names;
4040

4141
QStringList findAvailableLanguages();
42+
bool checkAllTranslations();
4243
};
4344
#endif

0 commit comments

Comments
 (0)