Little improved localization handling.

This commit is contained in:
Martin Rotter 2014-02-17 17:43:37 +01:00
parent 57d2e32d59
commit 836e1b3901
14 changed files with 100 additions and 55 deletions

View file

@ -370,6 +370,7 @@ set(APP_HEADERS
# CORE headers. # CORE headers.
src/core/settings.h src/core/settings.h
src/core/localization.h
src/core/basenetworkaccessmanager.h src/core/basenetworkaccessmanager.h
src/core/webbrowsernetworkaccessmanager.h src/core/webbrowsernetworkaccessmanager.h
src/core/silentnetworkaccessmanager.h src/core/silentnetworkaccessmanager.h
@ -395,17 +396,17 @@ set(APP_FORMS
# Add translations. # Add translations.
set(APP_TRANSLATIONS set(APP_TRANSLATIONS
localization/rssguard_en.ts localization/rssguard-en_GB.ts
localization/rssguard_cs.ts localization/rssguard-cs_CZ.ts
localization/rssguard_nl.ts localization/rssguard-nl_NL.ts
localization/qt_cs.ts localization/qt-cs_CZ.ts
localization/qt_nl.ts localization/qt-nl_NL.ts
) )
set(APP_TRANSLATIONS_WO_QT set(APP_TRANSLATIONS_WO_QT
localization/rssguard_en.ts localization/rssguard-en_GB.ts
localization/rssguard_cs.ts localization/rssguard-cs_CZ.ts
localization/rssguard_nl.ts localization/rssguard-nl_NL.ts
) )
set(APP_TEXT set(APP_TEXT

View file

@ -1553,8 +1553,8 @@ Autoři této aplikace nenesou žádnou odpovědnost za ztrátu Vašich dat.</tr
</message> </message>
<message> <message>
<source>LANG_ABBREV</source> <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: &quot;cs&quot;, &quot;nl&quot;, &quot;en&quot;, &quot;cs_CZ&quot;, &quot;en_GB&quot;, &quot;en_US&quot;.</extracomment> <extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code. Examples: &quot;cs_CZ&quot;, &quot;en_GB&quot;, &quot;en_US&quot;.</extracomment>
<translation>cs</translation> <translation>cs_CZ</translation>
</message> </message>
<message> <message>
<source>LANG_VERSION</source> <source>LANG_VERSION</source>

View file

@ -1535,8 +1535,8 @@ Authors of this application are NOT responsible for lost data.</source>
</message> </message>
<message> <message>
<source>LANG_ABBREV</source> <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: &quot;cs&quot;, &quot;nl&quot;, &quot;en&quot;, &quot;cs_CZ&quot;, &quot;en_GB&quot;, &quot;en_US&quot;.</extracomment> <extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code. Examples: &quot;cs_CZ&quot;, &quot;en_GB&quot;, &quot;en_US&quot;.</extracomment>
<translation>en</translation> <translation>en_GB</translation>
</message> </message>
<message> <message>
<source>LANG_VERSION</source> <source>LANG_VERSION</source>

View file

@ -1553,8 +1553,8 @@ Auteurs van Rssguard zijn NIET verantwoordelijk voor verlies van gegevens.</tran
</message> </message>
<message> <message>
<source>LANG_ABBREV</source> <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: &quot;cs&quot;, &quot;nl&quot;, &quot;en&quot;, &quot;cs_CZ&quot;, &quot;en_GB&quot;, &quot;en_US&quot;.</extracomment> <extracomment>Abbreviation of language, e.g. en. Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code. Examples: &quot;cs_CZ&quot;, &quot;en_GB&quot;, &quot;en_US&quot;.</extracomment>
<translation>nl</translation> <translation>nl_NL</translation>
</message> </message>
<message> <message>
<source>LANG_VERSION</source> <source>LANG_VERSION</source>

View file

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

Before

Width:  |  Height:  |  Size: 824 B

After

Width:  |  Height:  |  Size: 824 B

View file

@ -23,6 +23,7 @@
#define APP_VERSION "@APP_VERSION@" #define APP_VERSION "@APP_VERSION@"
#define APP_USERAGENT QString("@APP_NAME@/@APP_VERSION@ (@APP_URL@) on @CMAKE_SYSTEM@; Webkit/") + qWebKitVersion() #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 DEFAULT_FEED_ENCODING "UTF-8"
#define URL_REGEXP "^(http|https|feed|ftp):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?$" #define URL_REGEXP "^(http|https|feed|ftp):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?$"
#define USER_AGENT_HTTP_HEADER "User-Agent" #define USER_AGENT_HTTP_HEADER "User-Agent"

View file

@ -12,43 +12,62 @@
#include <QLocale> #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() { void Localization::load() {
QTranslator *qt_translator = new QTranslator(qApp); QTranslator *qt_translator = new QTranslator(qApp);
QTranslator *app_translator = new QTranslator(qApp); QTranslator *app_translator = new QTranslator(qApp);
QString locale_system = QLocale::system().name(); QString desired_localization = desiredLanguage();
QString locale_name = Settings::instance()->value(APP_CFG_GEN,
"language",
locale_system).toString();
qDebug("Try to load application localization. " if (app_translator->load(QString("rssguard-%1.qm").arg(desired_localization),
"System locale was detected as '%s'. " APP_LANG_PATH,
"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)) {
QApplication::installTranslator(app_translator); QApplication::installTranslator(app_translator);
qDebug("Application localization '%s' loaded successfully.", qDebug("Application localization '%s' loaded successfully.",
qPrintable(locale_name)); qPrintable(desired_localization));
} }
else { else {
qWarning("Application localization '%s' was not loaded.", qPrintable(locale_name)); qWarning("Application localization '%s' was not loaded.", qPrintable(desired_localization));
}
if (qt_translator->load(QString("qt_%1.qm").arg(locale_name), m_loadedLanguage = DEFAULT_LOCALE;
APP_LANG_PATH)) { return;
QApplication::installTranslator(qt_translator);
qDebug("Qt localization '%s' loaded successfully.",
qPrintable(locale_name));
}
else {
qWarning("Qt localization '%s' was not loaded.", qPrintable(locale_name));
} }
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() { QList<Language> Localization::installedLanguages() {
@ -57,7 +76,7 @@ QList<Language> Localization::installedLanguages() {
QTranslator translator; QTranslator translator;
// Iterate all found language files. // 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::Files,
QDir::Name)) { QDir::Name)) {
if (translator.load(file.absoluteFilePath())) { if (translator.load(file.absoluteFilePath())) {
@ -73,3 +92,4 @@ QList<Language> Localization::installedLanguages() {
} }
return languages; return languages;
} }

View file

@ -2,6 +2,8 @@
#define LOCALIZATION_H #define LOCALIZATION_H
#include <QString> #include <QString>
#include <QObject>
#include <QPointer>
struct Language { struct Language {
@ -12,18 +14,43 @@ struct Language {
QString m_email; QString m_email;
}; };
class Localization { class Localization : public QObject {
Q_OBJECT
private: private:
// Constructor. // Constructor.
explicit Localization(); explicit Localization(QObject *parent = 0);
public: 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. // Loads currently active language.
static void load(); void load();
// Returns list of installed application localizations. // Returns list of installed application localizations.
// This list is used ie. in settings dialog. // 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 #endif // LOCALIZATION_H

View file

@ -391,7 +391,7 @@ void FormSettings::saveProxy() {
} }
void FormSettings::loadLanguage() { void FormSettings::loadLanguage() {
foreach (const Language &language, Localization::installedLanguages()) { foreach (const Language &language, Localization::instance()->installedLanguages()) {
QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->m_treeLanguages); QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->m_treeLanguages);
item->setText(0, language.m_name); item->setText(0, language.m_name);
item->setText(1, language.m_code); item->setText(1, language.m_code);
@ -401,10 +401,8 @@ void FormSettings::loadLanguage() {
item->setIcon(0, IconThemeFactory::instance()->fromTheme(language.m_code)); item->setIcon(0, IconThemeFactory::instance()->fromTheme(language.m_code));
} }
QList<QTreeWidgetItem*> matching_items = m_ui->m_treeLanguages->findItems(Settings::instance()->value(APP_CFG_GEN, QList<QTreeWidgetItem*> matching_items = m_ui->m_treeLanguages->findItems(Localization::instance()->loadedLanguage(),
"language", Qt::MatchContains,
"en").toString(),
Qt::MatchExactly,
1); 1);
if (!matching_items.isEmpty()) { if (!matching_items.isEmpty()) {
m_ui->m_treeLanguages->setCurrentItem(matching_items[0]); m_ui->m_treeLanguages->setCurrentItem(matching_items[0]);
@ -418,9 +416,7 @@ void FormSettings::saveLanguage() {
} }
Settings *settings = Settings::instance(); Settings *settings = Settings::instance();
QString actual_lang = settings->value(APP_CFG_GEN, QString actual_lang = Localization::instance()->loadedLanguage();
"language",
"en").toString();
QString new_lang = m_ui->m_treeLanguages->currentItem()->text(1); QString new_lang = m_ui->m_treeLanguages->currentItem()->text(1);
// Save prompt for restart if language has changed. // Save prompt for restart if language has changed.

View file

@ -27,8 +27,8 @@ int main(int argc, char *argv[]) {
//: Name of language, e.g. English. //: Name of language, e.g. English.
QObject::tr("LANG_NAME"); QObject::tr("LANG_NAME");
//: Abbreviation of language, e.g. en. //: Abbreviation of language, e.g. en.
//: Use ISO 639-1 code here. They may be combined with ISO 3166-1 (alpha-2) codes. //: Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code.
//: Examples: "cs", "nl", "en", "cs_CZ", "en_GB", "en_US". //: Examples: "cs_CZ", "en_GB", "en_US".
QObject::tr("LANG_ABBREV"); QObject::tr("LANG_ABBREV");
//: Version of your translation, e.g. 1.0. //: Version of your translation, e.g. 1.0.
QObject::tr("LANG_VERSION"); QObject::tr("LANG_VERSION");
@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
// Load localization and setup locale before any widget is constructed. // Load localization and setup locale before any widget is constructed.
//LoadLocalization(); //LoadLocalization();
Localization::load(); Localization::instance()->load();
// These settings needs to be set before any QSettings object. // These settings needs to be set before any QSettings object.
QtSingleApplication::setApplicationName(APP_NAME); QtSingleApplication::setApplicationName(APP_NAME);