Ejemplo n.º 1
0
/** Sets the application's translator to the specified language. */
bool
LanguageSupport::translate(const QString &languageCode)
{
  if (!isValidLanguageCode(languageCode))
    return false;

  static QTranslator *retroshareTranslator = NULL;
  if (retroshareTranslator) {
    // remove the previous translator, is needed, when switching to en
    QApplication::removeTranslator(retroshareTranslator);
    delete(retroshareTranslator);
    retroshareTranslator = NULL;

    removePluginTranslation();
  }

  if (languageCode == "en")
    return true;

  /* Attempt to load the translations for Qt's internal widgets from their installed Qt directory. */
  QString qtTranslation = QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + languageCode + ".qm";
  QTranslator *systemQtTranslator = new QTranslator(rApp);
  Q_CHECK_PTR(systemQtTranslator);

  if (QFile::exists(qtTranslation) && systemQtTranslator->load(qtTranslation))
    QApplication::installTranslator(systemQtTranslator);
  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;
  }

  /* Install a translator for RetroShare's UI widgets */
  retroshareTranslator = new QTranslator();
  Q_CHECK_PTR(retroshareTranslator);

  bool result = true;

  if (retroshareTranslator->load(EXTERNAL_TRANSLATION_DIR + "/translations/retroshare_" + languageCode + ".qm")) {
    QApplication::installTranslator(retroshareTranslator);
  } else if (retroshareTranslator->load(":/lang/retroshare_" + languageCode + ".qm")) {
    QApplication::installTranslator(retroshareTranslator);
  } else {
    delete retroshareTranslator;
    retroshareTranslator = NULL;
    result = false;
  }

  result = translatePlugins(languageCode) && result;

  return result;
}
Ejemplo n.º 2
0
/** Returns the default language code for the system locale. */
QString
LanguageSupport::defaultLanguageCode()
{
  QString language = QLocale::system().name();

  if (language != "zh_CN" && language != "zh_TW")
    language = language.mid(0, language.indexOf("_"));
  if (!isValidLanguageCode(language))
    language = "en";
  
  return language;
}