Little improved localization handling.
This commit is contained in:
parent
57d2e32d59
commit
836e1b3901
14 changed files with 100 additions and 55 deletions
|
@ -370,6 +370,7 @@ set(APP_HEADERS
|
|||
|
||||
# CORE headers.
|
||||
src/core/settings.h
|
||||
src/core/localization.h
|
||||
src/core/basenetworkaccessmanager.h
|
||||
src/core/webbrowsernetworkaccessmanager.h
|
||||
src/core/silentnetworkaccessmanager.h
|
||||
|
@ -395,17 +396,17 @@ set(APP_FORMS
|
|||
|
||||
# Add translations.
|
||||
set(APP_TRANSLATIONS
|
||||
localization/rssguard_en.ts
|
||||
localization/rssguard_cs.ts
|
||||
localization/rssguard_nl.ts
|
||||
localization/qt_cs.ts
|
||||
localization/qt_nl.ts
|
||||
localization/rssguard-en_GB.ts
|
||||
localization/rssguard-cs_CZ.ts
|
||||
localization/rssguard-nl_NL.ts
|
||||
localization/qt-cs_CZ.ts
|
||||
localization/qt-nl_NL.ts
|
||||
)
|
||||
|
||||
set(APP_TRANSLATIONS_WO_QT
|
||||
localization/rssguard_en.ts
|
||||
localization/rssguard_cs.ts
|
||||
localization/rssguard_nl.ts
|
||||
localization/rssguard-en_GB.ts
|
||||
localization/rssguard-cs_CZ.ts
|
||||
localization/rssguard-nl_NL.ts
|
||||
)
|
||||
|
||||
set(APP_TEXT
|
||||
|
|
|
@ -1553,8 +1553,8 @@ Autoři této aplikace nenesou žádnou odpovědnost za ztrátu Vašich dat.</tr
|
|||
</message>
|
||||
<message>
|
||||
<source>LANG_ABBREV</source>
|
||||
<extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here. They may be combined with ISO 3166-1 (alpha-2) codes. Examples: "cs", "nl", "en", "cs_CZ", "en_GB", "en_US".</extracomment>
|
||||
<translation>cs</translation>
|
||||
<extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code. Examples: "cs_CZ", "en_GB", "en_US".</extracomment>
|
||||
<translation>cs_CZ</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>LANG_VERSION</source>
|
|
@ -1535,8 +1535,8 @@ Authors of this application are NOT responsible for lost data.</source>
|
|||
</message>
|
||||
<message>
|
||||
<source>LANG_ABBREV</source>
|
||||
<extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here. They may be combined with ISO 3166-1 (alpha-2) codes. Examples: "cs", "nl", "en", "cs_CZ", "en_GB", "en_US".</extracomment>
|
||||
<translation>en</translation>
|
||||
<extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code. Examples: "cs_CZ", "en_GB", "en_US".</extracomment>
|
||||
<translation>en_GB</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>LANG_VERSION</source>
|
|
@ -1553,8 +1553,8 @@ Auteurs van Rssguard zijn NIET verantwoordelijk voor verlies van gegevens.</tran
|
|||
</message>
|
||||
<message>
|
||||
<source>LANG_ABBREV</source>
|
||||
<extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here. They may be combined with ISO 3166-1 (alpha-2) codes. Examples: "cs", "nl", "en", "cs_CZ", "en_GB", "en_US".</extracomment>
|
||||
<translation>nl</translation>
|
||||
<extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code. Examples: "cs_CZ", "en_GB", "en_US".</extracomment>
|
||||
<translation>nl_NL</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>LANG_VERSION</source>
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 824 B After Width: | Height: | Size: 824 B |
|
@ -23,6 +23,7 @@
|
|||
#define APP_VERSION "@APP_VERSION@"
|
||||
#define APP_USERAGENT QString("@APP_NAME@/@APP_VERSION@ (@APP_URL@) on @CMAKE_SYSTEM@; Webkit/") + qWebKitVersion()
|
||||
|
||||
#define DEFAULT_LOCALE "en_GB"
|
||||
#define DEFAULT_FEED_ENCODING "UTF-8"
|
||||
#define URL_REGEXP "^(http|https|feed|ftp):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?$"
|
||||
#define USER_AGENT_HTTP_HEADER "User-Agent"
|
||||
|
|
|
@ -12,43 +12,62 @@
|
|||
#include <QLocale>
|
||||
|
||||
|
||||
Localization::Localization() {
|
||||
QPointer<Localization> Localization::s_instance;
|
||||
|
||||
Localization::Localization(QObject *parent)
|
||||
: QObject(parent) {
|
||||
}
|
||||
|
||||
Localization::~Localization() {
|
||||
qDebug("Destroying Localization instance.");
|
||||
}
|
||||
|
||||
Localization *Localization::instance() {
|
||||
if (s_instance.isNull()) {
|
||||
s_instance = new Localization(qApp);
|
||||
}
|
||||
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
QString Localization::desiredLanguage() {
|
||||
return Settings::instance()->value(APP_CFG_GEN,
|
||||
"language",
|
||||
QLocale::system().name()).toString();
|
||||
}
|
||||
|
||||
void Localization::load() {
|
||||
QTranslator *qt_translator = new QTranslator(qApp);
|
||||
QTranslator *app_translator = new QTranslator(qApp);
|
||||
QString locale_system = QLocale::system().name();
|
||||
QString locale_name = Settings::instance()->value(APP_CFG_GEN,
|
||||
"language",
|
||||
locale_system).toString();
|
||||
QString desired_localization = desiredLanguage();
|
||||
|
||||
qDebug("Try to load application localization. "
|
||||
"System locale was detected as '%s'. "
|
||||
"Trying to load localization for '%s'.",
|
||||
qPrintable(locale_system),
|
||||
qPrintable(locale_name));
|
||||
|
||||
if (app_translator->load(QString("rssguard_%1.qm").arg(locale_name),
|
||||
APP_LANG_PATH)) {
|
||||
if (app_translator->load(QString("rssguard-%1.qm").arg(desired_localization),
|
||||
APP_LANG_PATH,
|
||||
"-")) {
|
||||
QApplication::installTranslator(app_translator);
|
||||
qDebug("Application localization '%s' loaded successfully.",
|
||||
qPrintable(locale_name));
|
||||
qPrintable(desired_localization));
|
||||
}
|
||||
else {
|
||||
qWarning("Application localization '%s' was not loaded.", qPrintable(locale_name));
|
||||
}
|
||||
if (qt_translator->load(QString("qt_%1.qm").arg(locale_name),
|
||||
APP_LANG_PATH)) {
|
||||
QApplication::installTranslator(qt_translator);
|
||||
qDebug("Qt localization '%s' loaded successfully.",
|
||||
qPrintable(locale_name));
|
||||
}
|
||||
else {
|
||||
qWarning("Qt localization '%s' was not loaded.", qPrintable(locale_name));
|
||||
qWarning("Application localization '%s' was not loaded.", qPrintable(desired_localization));
|
||||
|
||||
m_loadedLanguage = DEFAULT_LOCALE;
|
||||
return;
|
||||
}
|
||||
|
||||
QLocale::setDefault(QLocale(locale_name));
|
||||
if (qt_translator->load(QString("qt-%1.qm").arg(desired_localization),
|
||||
APP_LANG_PATH,
|
||||
"-")) {
|
||||
QApplication::installTranslator(qt_translator);
|
||||
qDebug("Qt localization '%s' loaded successfully.",
|
||||
qPrintable(desired_localization));
|
||||
}
|
||||
else {
|
||||
qWarning("Qt localization '%s' was not loaded.", qPrintable(desired_localization));
|
||||
}
|
||||
|
||||
m_loadedLanguage = desired_localization;
|
||||
QLocale::setDefault(QLocale(desired_localization));
|
||||
}
|
||||
|
||||
QList<Language> Localization::installedLanguages() {
|
||||
|
@ -57,7 +76,7 @@ QList<Language> Localization::installedLanguages() {
|
|||
QTranslator translator;
|
||||
|
||||
// Iterate all found language files.
|
||||
foreach (const QFileInfo &file, file_dir.entryInfoList(QStringList() << "rssguard_*.qm",
|
||||
foreach (const QFileInfo &file, file_dir.entryInfoList(QStringList() << "rssguard-*.qm",
|
||||
QDir::Files,
|
||||
QDir::Name)) {
|
||||
if (translator.load(file.absoluteFilePath())) {
|
||||
|
@ -73,3 +92,4 @@ QList<Language> Localization::installedLanguages() {
|
|||
}
|
||||
return languages;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#define LOCALIZATION_H
|
||||
|
||||
#include <QString>
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
|
||||
|
||||
struct Language {
|
||||
|
@ -12,18 +14,43 @@ struct Language {
|
|||
QString m_email;
|
||||
};
|
||||
|
||||
class Localization {
|
||||
class Localization : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
// Constructor.
|
||||
explicit Localization();
|
||||
explicit Localization(QObject *parent = 0);
|
||||
|
||||
public:
|
||||
// Destructor.
|
||||
virtual ~Localization();
|
||||
|
||||
// Singleton getter.
|
||||
static Localization *instance();
|
||||
|
||||
// Returns code of language that should
|
||||
// be loaded according to settings.
|
||||
QString desiredLanguage();
|
||||
|
||||
// Loads currently active language.
|
||||
static void load();
|
||||
void load();
|
||||
|
||||
// Returns list of installed application localizations.
|
||||
// This list is used ie. in settings dialog.
|
||||
static QList<Language> installedLanguages();
|
||||
QList<Language> installedLanguages();
|
||||
|
||||
// Returns empty string or loaded language
|
||||
// name if it is really loaded.
|
||||
inline QString loadedLanguage() const {
|
||||
return m_loadedLanguage;
|
||||
}
|
||||
|
||||
private:
|
||||
// Code of loaded language.
|
||||
QString m_loadedLanguage;
|
||||
|
||||
// Singleton.
|
||||
static QPointer<Localization> s_instance;
|
||||
};
|
||||
|
||||
#endif // LOCALIZATION_H
|
||||
|
|
|
@ -391,7 +391,7 @@ void FormSettings::saveProxy() {
|
|||
}
|
||||
|
||||
void FormSettings::loadLanguage() {
|
||||
foreach (const Language &language, Localization::installedLanguages()) {
|
||||
foreach (const Language &language, Localization::instance()->installedLanguages()) {
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->m_treeLanguages);
|
||||
item->setText(0, language.m_name);
|
||||
item->setText(1, language.m_code);
|
||||
|
@ -401,10 +401,8 @@ void FormSettings::loadLanguage() {
|
|||
item->setIcon(0, IconThemeFactory::instance()->fromTheme(language.m_code));
|
||||
}
|
||||
|
||||
QList<QTreeWidgetItem*> matching_items = m_ui->m_treeLanguages->findItems(Settings::instance()->value(APP_CFG_GEN,
|
||||
"language",
|
||||
"en").toString(),
|
||||
Qt::MatchExactly,
|
||||
QList<QTreeWidgetItem*> matching_items = m_ui->m_treeLanguages->findItems(Localization::instance()->loadedLanguage(),
|
||||
Qt::MatchContains,
|
||||
1);
|
||||
if (!matching_items.isEmpty()) {
|
||||
m_ui->m_treeLanguages->setCurrentItem(matching_items[0]);
|
||||
|
@ -418,9 +416,7 @@ void FormSettings::saveLanguage() {
|
|||
}
|
||||
|
||||
Settings *settings = Settings::instance();
|
||||
QString actual_lang = settings->value(APP_CFG_GEN,
|
||||
"language",
|
||||
"en").toString();
|
||||
QString actual_lang = Localization::instance()->loadedLanguage();
|
||||
QString new_lang = m_ui->m_treeLanguages->currentItem()->text(1);
|
||||
|
||||
// Save prompt for restart if language has changed.
|
||||
|
|
|
@ -27,8 +27,8 @@ int main(int argc, char *argv[]) {
|
|||
//: Name of language, e.g. English.
|
||||
QObject::tr("LANG_NAME");
|
||||
//: Abbreviation of language, e.g. en.
|
||||
//: Use ISO 639-1 code here. They may be combined with ISO 3166-1 (alpha-2) codes.
|
||||
//: Examples: "cs", "nl", "en", "cs_CZ", "en_GB", "en_US".
|
||||
//: Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code.
|
||||
//: Examples: "cs_CZ", "en_GB", "en_US".
|
||||
QObject::tr("LANG_ABBREV");
|
||||
//: Version of your translation, e.g. 1.0.
|
||||
QObject::tr("LANG_VERSION");
|
||||
|
@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
// Load localization and setup locale before any widget is constructed.
|
||||
//LoadLocalization();
|
||||
Localization::load();
|
||||
Localization::instance()->load();
|
||||
|
||||
// These settings needs to be set before any QSettings object.
|
||||
QtSingleApplication::setApplicationName(APP_NAME);
|
||||
|
|
Loading…
Add table
Reference in a new issue