OSCAR-code/sleepyhead/translation.cpp

176 lines
5.6 KiB
C++
Raw Normal View History

/* Multilingual Support files
2014-04-24 09:44:15 +00:00
*
* Copyright (c) 2011-2018 Mark Watkins <mark@jedimark.net>
2014-04-24 09:44:15 +00:00
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of the source code
* for more details. */
2014-04-24 09:44:15 +00:00
#include <QApplication>
#include <QDebug>
#include <QStringList>
#include <QList>
#include <QDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QPushButton>
#include <QDir>
#include <QSettings>
#include <QTranslator>
#include <QListWidget>
2014-04-24 09:44:15 +00:00
#include "SleepLib/common.h"
2018-06-07 02:31:05 +00:00
//#ifndef nullptr
//#define nullptr NULL
//#endif
2014-04-24 09:44:15 +00:00
#include "translation.h"
QHash<QString, QString> langNames;
2014-04-24 09:44:15 +00:00
QString currentLanguage()
{
QSettings settings;
return settings.value(LangSetting).toString();
}
QString lookupLanguageName(QString language)
{
auto it = langNames.find(language);
if (it != langNames.end()) {
return it.value();
}
return language;
}
void initTranslations() {
2014-04-24 09:44:15 +00:00
// (Ordinary character sets will just use the name before the first '.' in the filename.)
// (This u8 stuff deliberately kills Qt4.x build support - if you know another way feel free to
// change it, but Qt4 support is still going to die sooner or later)
// Add any languages with special character set needs to this list
2018-06-07 00:09:06 +00:00
langNames["zh"] = "\xe6\xbc\xa2\xe8\xaa\x9e\xe7\xb9\x81\xe9\xab\x94\xe5\xad\x97";
langNames["es"] = "Espa\xc3\xb1ol";
langNames["bg"] = "\xd0\xb1\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd1\x81\xd0\xba\xd0\xb8";
langNames["fr"] = "\x46\x72\x61\x6e\xc3\xa7\x61\x69\x73";
langNames["en_UK"] = "English (UK)";
langNames["en_US"] = "English (US)";
// CHECK: Will the above break with MS VisualC++ compiler?
2014-04-24 09:44:15 +00:00
QHash<QString, QString> langFiles;
2018-06-07 02:31:05 +00:00
const QString transdir = appResourcePath() +"/Translations";
QDir dir(transdir);
qDebug() << "Scanning" << transdir.toLocal8Bit().data();
dir.setFilter(QDir::Files);
dir.setNameFilters(QStringList("*.qm"));
QFileInfoList list = dir.entryInfoList();
QSettings settings;
QString language = settings.value(LangSetting).toString();
2014-04-24 09:44:15 +00:00
QString langfile, langname;
2014-04-24 09:44:15 +00:00
// Add default language (English)
const QString en="en_US";
langFiles[en]="English.en_US.qm";
langNames[en]="English US";
2014-04-24 09:44:15 +00:00
// Scan through available translations, and add them to the list
QStringList availtrans;
for (const auto & fi : list) {
2014-04-24 09:44:15 +00:00
QString name = fi.fileName().section('.', 0, 0);
QString code = fi.fileName().section('.', 1, 1);
availtrans.push_back(name);
2014-04-24 09:44:15 +00:00
if (langNames.contains(code)) {
name = langNames[code];
} else {
langNames[code]=name;
}
langFiles[code]=fi.fileName();
2014-04-24 09:44:15 +00:00
}
qDebug() << "Available Translations:" << QString(availtrans.join(", ")).toLocal8Bit().data();
2014-04-24 09:44:15 +00:00
if (language.isEmpty() || !langNames.contains(language)) {
QDialog langsel(nullptr, Qt::CustomizeWindowHint | Qt::WindowTitleHint);
QFont font;
font.setPointSize(20);
2014-04-24 09:44:15 +00:00
langsel.setFont(font);
2018-06-07 00:09:06 +00:00
langsel.setWindowTitle("Language / Taal / Sprache / Langue / \xe8\xaf\xad\xe8\xa8\x80 / ... ");
2014-04-24 09:44:15 +00:00
QHBoxLayout lang_layout(&langsel);
QLabel img;
img.setPixmap(QPixmap(":/docs/sheep.png"));
// hard coded non translatable
QPushButton lang_okbtn("->", &langsel);
2014-05-04 06:21:26 +00:00
QVBoxLayout layout1;
QVBoxLayout layout2;
2014-04-24 09:44:15 +00:00
layout2.setMargin(6);
lang_layout.setContentsMargins(4,4,4,4);
lang_layout.setMargin(6);
layout2.setSpacing(6);
QListWidget langlist;
2014-04-24 09:44:15 +00:00
lang_layout.addLayout(&layout1);
lang_layout.addLayout(&layout2);
layout1.addWidget(&img);
layout2.addWidget(&langlist, 1);
2014-04-24 09:44:15 +00:00
langlist.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
int row = 0;
2014-05-06 18:03:13 +00:00
for (QHash<QString, QString>::iterator it = langNames.begin(); it != langNames.end(); ++it) {
2014-04-24 09:44:15 +00:00
const QString & code = it.key();
const QString & name = it.value();
2014-06-16 00:16:49 +00:00
if (!langFiles.contains(code) || langFiles[code].isEmpty())
continue;
QListWidgetItem *item = new QListWidgetItem(name);
item->setData(Qt::UserRole, code);
langlist.insertItem(row++, item);
// Todo: Use base system language code
if (code.compare(en) == 0) {
langlist.setCurrentItem(item);
}
2014-04-24 09:44:15 +00:00
}
langlist.sortItems();
layout2.addWidget(&lang_okbtn);
2014-04-24 09:44:15 +00:00
langsel.connect(&langlist, SIGNAL(itemDoubleClicked(QListWidgetItem*)), &langsel, SLOT(close()));
2014-04-24 09:44:15 +00:00
langsel.connect(&lang_okbtn, SIGNAL(clicked()), &langsel, SLOT(close()));
langsel.exec();
langsel.disconnect(&lang_okbtn, SIGNAL(clicked()), &langsel, SLOT(close()));
langsel.disconnect(&langlist, SIGNAL(itemDoubleClicked(QListWidgetItem*)), &langsel, SLOT(close()));
langname = langlist.currentItem()->text();
language = langlist.currentItem()->data(Qt::UserRole).toString();
settings.setValue(LangSetting, language);
2014-04-24 09:44:15 +00:00
}
langname=langNames[language];
langfile=langFiles[language];
if (language.compare(en) != 0) {
2014-06-16 00:16:49 +00:00
qDebug() << "Loading " << langname << " Translation" << langfile << "from" << transdir;
QTranslator * translator = new QTranslator();
2014-04-24 09:44:15 +00:00
2014-06-16 00:16:49 +00:00
if (!langfile.isEmpty() && !translator->load(langfile, transdir)) {
qWarning() << "Could not load translation" << langfile << "reverting to english :(";
}
2014-04-24 09:44:15 +00:00
2014-06-16 00:16:49 +00:00
qApp->installTranslator(translator);
} else {
qDebug() << "Using in-built english Translation";
}
2014-04-24 09:44:15 +00:00
}