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-09-15 06:39:31 -04:00
|
|
|
languages.insert("cs", "Czech");
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("de", "Deutsch");
|
2010-03-26 12:20:12 -04:00
|
|
|
languages.insert("da", "Danish");
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("en", "English");
|
2011-06-10 14:40:31 -04:00
|
|
|
languages.insert("es", QString::fromUtf8("spanish"));
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("fr", QString::fromUtf8("Fran\303\247ais"));
|
2010-12-17 15:43:05 -05:00
|
|
|
languages.insert("fi", "suomi");
|
2010-03-17 08:30:57 -04:00
|
|
|
//languages.insert("gr", "Greek");
|
|
|
|
//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"));
|
|
|
|
languages.insert("ko", "Korean");
|
2012-09-15 06:39:31 -04:00
|
|
|
languages.insert("pl", "Polska");
|
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");
|
2010-03-11 19:16:39 -05:00
|
|
|
languages.insert("sv", "svenska");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/** 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-11-04 14:39:24 -04:00
|
|
|
QString qtTranslation = QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + languageCode + ".qm";
|
2010-03-11 19:16:39 -05:00
|
|
|
QTranslator *systemQtTranslator = new QTranslator(rApp);
|
|
|
|
Q_CHECK_PTR(systemQtTranslator);
|
|
|
|
|
2010-11-04 14:39:24 -04:00
|
|
|
if (QFile::exists(qtTranslation) && systemQtTranslator->load(qtTranslation))
|
2010-03-11 19:16:39 -05:00
|
|
|
QApplication::installTranslator(systemQtTranslator);
|
2010-11-04 14:39:24 -04:00
|
|
|
else {
|
|
|
|
/* Attempt to load the translations for Qt's internal widgets from the translations directory in the exe dir. */
|
|
|
|
qtTranslation = QCoreApplication::applicationDirPath() + "/translations/qt_" + languageCode + ".qm";
|
|
|
|
if (QFile::exists(qtTranslation) && systemQtTranslator->load(qtTranslation))
|
|
|
|
QApplication::installTranslator(systemQtTranslator);
|
|
|
|
else
|
|
|
|
delete systemQtTranslator;
|
|
|
|
}
|
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
|
|
|
}
|