2007-11-14 22:18:48 -05:00
|
|
|
/****************************************************************
|
2009-04-20 09:24:56 -04:00
|
|
|
* This file is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2007, crypton
|
2007-12-08 06:27:01 -05:00
|
|
|
* Copyright (c) 2006, Matt Edman, Justin Hipple
|
2007-11-14 22:18:48 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#include <QTranslator>
|
|
|
|
#include <QLocale>
|
2010-03-11 19:16:39 -05:00
|
|
|
#include <QLibraryInfo>
|
|
|
|
#include <rshare.h>
|
2011-11-17 16:17:24 -05:00
|
|
|
#include <retroshare/rsplugin.h>
|
2012-10-12 14:44:02 -04:00
|
|
|
#include <retroshare/rsinit.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#include "languagesupport.h"
|
|
|
|
|
2011-11-17 16:17:24 -05:00
|
|
|
static QMap<RsPlugin*, QTranslator*> translatorPlugins;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-10-12 14:44:02 -04:00
|
|
|
#define EXTERNAL_TRANSLATION_DIR QString::fromUtf8(RsInit::getRetroshareDataDirectory().c_str())
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/** Initializes the list of available languages. */
|
2010-03-11 19:16:39 -05:00
|
|
|
QMap<QString, QString>
|
|
|
|
LanguageSupport::languages()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
static QMap<QString, QString> languages;
|
|
|
|
if (languages.isEmpty()) {
|
2010-03-17 08:30:57 -04:00
|
|
|
//languages.insert("af", "Afrikaans");
|
|
|
|
//languages.insert("bg", "Bulgarien");
|
|
|
|
//languages.insert("cy", "Welsh");
|
2012-10-15 17:17:57 -04:00
|
|
|
languages.insert("cs", QString::fromUtf8("\304\214esky"));
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("de", "Deutsch");
|
2012-10-15 17:17:57 -04:00
|
|
|
languages.insert("da", "Dansk");
|
2013-06-20 14:24:13 -04:00
|
|
|
languages.insert("nl", "Nederlands");
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("en", "English");
|
2012-10-15 17:17:57 -04:00
|
|
|
languages.insert("es", QString::fromUtf8("Espa\303\261ol"));
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("fr", QString::fromUtf8("Fran\303\247ais"));
|
2012-10-15 17:17:57 -04:00
|
|
|
languages.insert("fi", "Suomi");
|
2013-06-20 14:24:13 -04:00
|
|
|
languages.insert("el", QString::fromUtf8("\316\225\316\273\316\273\316\267\316\275\316\271\316\272\316\254"));
|
2012-11-12 12:04:37 -05:00
|
|
|
languages.insert("hu", "Magyar");
|
2012-12-06 09:31:40 -05:00
|
|
|
languages.insert("it", "Italiano");
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("ja_JP", QString::fromUtf8("\346\227\245\346\234\254\350\252\236"));
|
2012-10-15 17:17:57 -04:00
|
|
|
languages.insert("ko", QString::fromUtf8("\355\225\234\352\265\255\354\226\264"));
|
|
|
|
languages.insert("pl", "Polski");
|
2010-03-17 08:30:57 -04:00
|
|
|
//languages.insert("pt", "Portuguese");
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("ru", QString::fromUtf8("\320\240\321\203\321\201\321\201\320\272\320\270\320\271"));
|
2010-03-17 08:30:57 -04:00
|
|
|
//languages.insert("sl", "slovenian");
|
|
|
|
//languages.insert("sr", "Serbian");
|
2012-10-15 17:17:57 -04:00
|
|
|
languages.insert("sv", "Svenska");
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("tr", QString::fromUtf8("T\303\274rk\303\247e"));
|
|
|
|
languages.insert("zh_CN", QString::fromUtf8("\347\256\200\344\275\223\345\255\227"));
|
2010-03-17 08:30:57 -04:00
|
|
|
//languages.insert("zh_TW", QString::fromUtf8("\347\260\241\351\253\224\345\255\227"));
|
2010-03-11 19:16:39 -05:00
|
|
|
}
|
|
|
|
return languages;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-11-10 13:59:58 -05:00
|
|
|
QMap<QString, QLocale>
|
|
|
|
LanguageSupport::locales()
|
|
|
|
{
|
|
|
|
static QMap<QString, QLocale> locales;
|
|
|
|
if (locales.isEmpty()) {
|
|
|
|
//locales.insert("af", QLocale(QLocale::Afrikaans, QLocale::SouthAfrica));
|
|
|
|
//locales.insert("bg", QLocale(QLocale::Bulgarian, QLocale::Bulgaria),);
|
|
|
|
//locales.insert("cy", QLocale(QLocale::Welsh, QLocale::UnitedKingdom));
|
|
|
|
locales.insert("cs", QLocale(QLocale::Czech, QLocale::CzechRepublic));
|
|
|
|
locales.insert("de", QLocale(QLocale::German, QLocale::Germany));
|
|
|
|
locales.insert("da", QLocale(QLocale::Danish, QLocale::Denmark));
|
2013-06-04 19:39:07 -04:00
|
|
|
locales.insert("nl", QLocale(QLocale::Dutch, QLocale::Netherlands));
|
2012-11-10 13:59:58 -05:00
|
|
|
locales.insert("en", QLocale(QLocale::English, QLocale::UnitedStates));
|
|
|
|
locales.insert("es", QLocale(QLocale::Spanish, QLocale::Spain));
|
|
|
|
locales.insert("fr", QLocale(QLocale::French, QLocale::France));
|
|
|
|
locales.insert("fi", QLocale(QLocale::Finnish, QLocale::Finland));
|
2013-06-04 19:39:07 -04:00
|
|
|
locales.insert("el", QLocale(QLocale::Greek, QLocale::Greece));
|
2012-11-10 19:55:15 -05:00
|
|
|
locales.insert("hu", QLocale(QLocale::Hungarian, QLocale::Hungary));
|
2012-12-06 09:31:40 -05:00
|
|
|
locales.insert("it", QLocale(QLocale::Italian, QLocale::Italy));
|
2012-11-10 13:59:58 -05:00
|
|
|
locales.insert("ja", QLocale(QLocale::Japanese, QLocale::Japan));
|
|
|
|
locales.insert("ko", QLocale(QLocale::Korean, QLocale::RepublicOfKorea));
|
|
|
|
locales.insert("pl", QLocale(QLocale::Polish, QLocale::Poland));
|
|
|
|
//locales.insert("pt", QLocale(QLocale::Portuguese, QLocale::Brazil));
|
|
|
|
locales.insert("ru", QLocale(QLocale::Russian, QLocale::RussianFederation));
|
|
|
|
//locales.insert("sl", QLocale(QLocale::Slovenian, QLocale::Slovenia));
|
|
|
|
//locales.insert("sr", QLocale(QLocale::Serbian, QLocale::SerbiaAndMontenegro));
|
|
|
|
locales.insert("sv", QLocale(QLocale::Swedish, QLocale::Sweden));
|
|
|
|
locales.insert("tr", QLocale(QLocale::Turkish, QLocale::Turkey));
|
|
|
|
locales.insert("zh", QLocale(QLocale::Chinese, QLocale::China));
|
|
|
|
}
|
|
|
|
return locales;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/** Returns the default language code for the system locale. */
|
|
|
|
QString
|
|
|
|
LanguageSupport::defaultLanguageCode()
|
|
|
|
{
|
2009-04-20 09:24:56 -04:00
|
|
|
QString language = QLocale::system().name();
|
|
|
|
|
|
|
|
if (language != "zh_CN" && language != "zh_TW")
|
|
|
|
language = language.mid(0, language.indexOf("_"));
|
|
|
|
if (!isValidLanguageCode(language))
|
2007-11-14 22:18:48 -05:00
|
|
|
language = "en";
|
2009-04-20 09:24:56 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the language code for a given language name. */
|
|
|
|
QString
|
2010-03-11 19:16:39 -05:00
|
|
|
LanguageSupport::languageCode(const QString &languageName)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
return languages().key(languageName);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a list of all supported language codes. (e.g., "en"). */
|
|
|
|
QStringList
|
|
|
|
LanguageSupport::languageCodes()
|
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
return languages().keys();
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the language name for a given language code. */
|
|
|
|
QString
|
2010-03-11 19:16:39 -05:00
|
|
|
LanguageSupport::languageName(const QString &languageCode)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
return languages().value(languageCode);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a list of all supported language names (e.g., "English"). */
|
|
|
|
QStringList
|
|
|
|
LanguageSupport::languageNames()
|
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
return languages().values();
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2010-03-11 19:16:39 -05:00
|
|
|
/** Returns true if we understand the given language code. */
|
|
|
|
bool
|
|
|
|
LanguageSupport::isValidLanguageCode(const QString &languageCode)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
return languageCodes().contains(languageCode);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2010-03-11 19:16:39 -05:00
|
|
|
/** Returns true if <b>languageCode</b> requires a right-to-left layout. */
|
2007-11-14 22:18:48 -05:00
|
|
|
bool
|
2010-03-11 19:16:39 -05:00
|
|
|
LanguageSupport::isRightToLeft(const QString &languageCode)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
return (!languageCode.compare("ar", Qt::CaseInsensitive)
|
|
|
|
|| !languageCode.compare("fa", Qt::CaseInsensitive)
|
|
|
|
|| !languageCode.compare("he", Qt::CaseInsensitive));
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-11-17 16:17:24 -05:00
|
|
|
|
|
|
|
static void removePluginTranslation()
|
|
|
|
{
|
|
|
|
QMap<RsPlugin*, QTranslator*>::iterator it;
|
|
|
|
for (it = translatorPlugins.begin(); it != translatorPlugins.end(); ++it) {
|
|
|
|
if (it.value()) {
|
|
|
|
QApplication::removeTranslator(it.value());
|
|
|
|
delete(it.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
translatorPlugins.clear();
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
/** Sets the application's translator to the specified language. */
|
|
|
|
bool
|
2010-03-11 19:16:39 -05:00
|
|
|
LanguageSupport::translate(const QString &languageCode)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-03-11 19:16:39 -05:00
|
|
|
if (!isValidLanguageCode(languageCode))
|
|
|
|
return false;
|
2011-10-14 17:16:34 -04:00
|
|
|
|
|
|
|
static QTranslator *retroshareTranslator = NULL;
|
|
|
|
if (retroshareTranslator) {
|
|
|
|
// remove the previous translator, is needed, when switching to en
|
|
|
|
QApplication::removeTranslator(retroshareTranslator);
|
|
|
|
delete(retroshareTranslator);
|
|
|
|
retroshareTranslator = NULL;
|
2011-11-17 16:17:24 -05:00
|
|
|
|
|
|
|
removePluginTranslation();
|
2011-10-14 17:16:34 -04:00
|
|
|
}
|
|
|
|
|
2010-03-11 19:16:39 -05:00
|
|
|
if (languageCode == "en")
|
|
|
|
return true;
|
|
|
|
|
2012-10-11 19:46:54 -04:00
|
|
|
/* Attempt to load the translations for Qt's internal widgets from their installed Qt directory. */
|
2010-03-11 19:16:39 -05:00
|
|
|
QTranslator *systemQtTranslator = new QTranslator(rApp);
|
|
|
|
Q_CHECK_PTR(systemQtTranslator);
|
|
|
|
|
2012-10-15 17:17:57 -04:00
|
|
|
if (systemQtTranslator->load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + languageCode + ".qm")) {
|
2010-03-11 19:16:39 -05:00
|
|
|
QApplication::installTranslator(systemQtTranslator);
|
2012-10-15 17:17:57 -04:00
|
|
|
} else {
|
2010-11-04 14:39:24 -04:00
|
|
|
/* Attempt to load the translations for Qt's internal widgets from the translations directory in the exe dir. */
|
2012-10-15 17:17:57 -04:00
|
|
|
if (systemQtTranslator->load(QCoreApplication::applicationDirPath() + "/translations/qt_" + languageCode + ".qm")) {
|
2010-11-04 14:39:24 -04:00
|
|
|
QApplication::installTranslator(systemQtTranslator);
|
2012-10-15 17:17:57 -04:00
|
|
|
} else {
|
|
|
|
/* Attempt to load the translations for Qt's internal widgets from the translations directory in the data dir. */
|
|
|
|
if (systemQtTranslator->load(EXTERNAL_TRANSLATION_DIR + "/translations/qt_" + languageCode + ".qm")) {
|
|
|
|
QApplication::installTranslator(systemQtTranslator);
|
|
|
|
} else {
|
|
|
|
delete systemQtTranslator;
|
|
|
|
}
|
|
|
|
}
|
2010-11-04 14:39:24 -04:00
|
|
|
}
|
2010-03-11 19:16:39 -05:00
|
|
|
|
|
|
|
/* Install a translator for RetroShare's UI widgets */
|
2012-07-15 17:37:35 -04:00
|
|
|
retroshareTranslator = new QTranslator();
|
2010-03-11 19:16:39 -05:00
|
|
|
Q_CHECK_PTR(retroshareTranslator);
|
|
|
|
|
2011-11-17 16:17:24 -05:00
|
|
|
bool result = true;
|
|
|
|
|
2012-10-12 14:44:02 -04:00
|
|
|
if (retroshareTranslator->load(EXTERNAL_TRANSLATION_DIR + "/translations/retroshare_" + languageCode + ".qm")) {
|
2012-10-11 19:46:54 -04:00
|
|
|
QApplication::installTranslator(retroshareTranslator);
|
|
|
|
} else if (retroshareTranslator->load(":/lang/retroshare_" + languageCode + ".qm")) {
|
2010-03-11 19:16:39 -05:00
|
|
|
QApplication::installTranslator(retroshareTranslator);
|
2011-11-17 16:17:24 -05:00
|
|
|
} else {
|
|
|
|
delete retroshareTranslator;
|
|
|
|
retroshareTranslator = NULL;
|
|
|
|
result = false;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-11-17 16:17:24 -05:00
|
|
|
|
|
|
|
result = translatePlugins(languageCode) && result;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the application's translator to the specified language for the plugins. */
|
|
|
|
bool LanguageSupport::translatePlugins(const QString &languageCode)
|
|
|
|
{
|
|
|
|
removePluginTranslation();
|
|
|
|
|
|
|
|
if (rsPlugins == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:44:02 -04:00
|
|
|
QString externalDir = EXTERNAL_TRANSLATION_DIR + "/translations";
|
|
|
|
|
2011-11-17 16:17:24 -05:00
|
|
|
int count = rsPlugins->nbPlugins();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
RsPlugin* plugin = rsPlugins->plugin(i);
|
|
|
|
if (plugin) {
|
2012-10-12 14:44:02 -04:00
|
|
|
QTranslator* translator = plugin->qt_translator(rApp, languageCode, externalDir);
|
2011-11-17 16:17:24 -05:00
|
|
|
if (translator) {
|
|
|
|
QApplication::installTranslator(translator);
|
|
|
|
translatorPlugins[plugin] = translator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-11-10 13:59:58 -05:00
|
|
|
/** Sets the application's locale according to the specified language. */
|
|
|
|
bool LanguageSupport::localize(const QString &languageCode)
|
|
|
|
{
|
|
|
|
if (!isValidLanguageCode(languageCode))
|
|
|
|
return false;
|
|
|
|
QLocale::setDefault(locales().key(languageCode));
|
|
|
|
return true;
|
|
|
|
}
|