added -lang parameter to application to be able to choose the language. Available on linux

preferencesAboutTextFull
Claudio Valerio 13 years ago
parent 0ba0dcc79c
commit a9752aaf00
  1. 65
      src/core/UBApplication.cpp
  2. 1
      src/core/UBApplication.h
  3. 5
      src/frameworks/UBPlatformUtils.h
  4. 17
      src/frameworks/UBPlatformUtils_linux.cpp
  5. 16
      src/frameworks/UBPlatformUtils_win.cpp

@ -135,27 +135,13 @@ UBApplication::UBApplication(const QString &id, int &argc, char **argv) : QtSing
UBResources::resources();
if (!undoStack)
{
undoStack = new QUndoStack(staticMemoryCleaner);
}
mApplicationTranslator = new QTranslator(this);
mApplicationTranslator->load(UBPlatformUtils::preferredTranslation(QString("sankore_")));
installTranslator(mApplicationTranslator);
QString localString;
if (!mApplicationTranslator->isEmpty())
localString = UBPlatformUtils::preferredLanguage();
else
localString = "en_US";
mQtGuiTranslator = new QTranslator(this);
mQtGuiTranslator->load(UBPlatformUtils::preferredTranslation(QString("qt_")));
installTranslator(mQtGuiTranslator);
QString forcedLanguage("");
if(args.contains("-lang"))
forcedLanguage=args.at(args.indexOf("-lang") + 1);
QLocale::setDefault(QLocale(localString));
qDebug() << "Running application in:" << localString;
setupTranslator(forcedLanguage);
UBSettings *settings = UBSettings::settings();
@ -217,6 +203,49 @@ UBApplication::~UBApplication()
staticMemoryCleaner = 0;
}
void UBApplication::setupTranslator(QString forcedLanguage)
{
QStringList availablesTranslations = UBPlatformUtils::availableTranslations();
QString language("");
if(!forcedLanguage.isEmpty()){
if(availablesTranslations.contains(forcedLanguage,Qt::CaseInsensitive))
language = forcedLanguage;
else
qDebug() << "forced language " << forcedLanguage << " not available";
}
else{
QString systemLanguage = UBPlatformUtils::systemLanguage();
if(availablesTranslations.contains(systemLanguage,Qt::CaseInsensitive))
language = systemLanguage;
else
qDebug() << "translation for system language " << systemLanguage << " not found";
}
if(language.isEmpty()){
language = "en_US";
//fallback if no translation are available
}
else{
mApplicationTranslator = new QTranslator(this);
mQtGuiTranslator = new QTranslator(this);
mApplicationTranslator->load(UBPlatformUtils::translationPath(QString("sankore_"),language));
installTranslator(mApplicationTranslator);
mQtGuiTranslator->load(UBPlatformUtils::translationPath(QString("qt_"),language));
if(mQtGuiTranslator){
// checked because this translation could be not available
installTranslator(mQtGuiTranslator);
}
}
QLocale::setDefault(QLocale(language));
qDebug() << "Running application in:" << language;
}
int UBApplication::exec(const QString& pFileToImport)
{
QPixmapCache::setCacheLimit(1024 * 100);

@ -121,6 +121,7 @@ class UBApplication : public QtSingleApplication
private:
void updateProtoActionsState();
void setupTranslator(QString forcedLanguage);
QList<QMenu*> mProtoMenus;
bool mIsVerbose;

@ -103,8 +103,8 @@ class UBPlatformUtils
static void setFileType(const QString &filePath, unsigned long fileType);
static void fadeDisplayOut();
static void fadeDisplayIn();
static QString preferredTranslation(QString pFilePrefix);
static QString preferredLanguage();
static QString translationPath(QString pFilePrefix, QString pLanguage);
static QString systemLanguage();
static bool hasVirtualKeyboard();
//static void showVirtualKeyboard();
static void runInstaller(const QString &installerFilePath);
@ -115,6 +115,7 @@ class UBPlatformUtils
static QString computerName();
static UBKeyboardLocale** getKeyboardLayouts(int& nCount);
static QString urlFromClipboard();
static QStringList availableTranslations();
};

@ -20,6 +20,8 @@
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include "frameworks/UBFileSystemUtils.h"
void UBPlatformUtils::init()
{
@ -55,13 +57,22 @@ void UBPlatformUtils::fadeDisplayIn()
// NOOP
}
QString UBPlatformUtils::preferredTranslation(QString pFilePrefix)
QStringList UBPlatformUtils::availableTranslations()
{
QString translationsPath = applicationResourcesDirectory() + "/" + "i18n" + "/";
QStringList translationsList = UBFileSystemUtils::allFiles(translationsPath);
QRegExp sankoreTranslationFiles(".*sankore_.*.qm");
translationsList=translationsList.filter(sankoreTranslationFiles);
return translationsList.replaceInStrings(QRegExp("(.*)sankore_(.*).qm"),"\\2");
}
QString UBPlatformUtils::translationPath(QString pFilePrefix,QString pLanguage)
{
QString qmPath = applicationResourcesDirectory() + "/" + "i18n" + "/" + pFilePrefix + preferredLanguage() + ".qm";
QString qmPath = applicationResourcesDirectory() + "/" + "i18n" + "/" + pFilePrefix + pLanguage + ".qm";
return qmPath;
}
QString UBPlatformUtils::preferredLanguage()
QString UBPlatformUtils::systemLanguage()
{
return QLocale::system().name();
}

@ -59,14 +59,22 @@ void UBPlatformUtils::fadeDisplayIn()
// NOOP
}
QString UBPlatformUtils::preferredTranslation(QString pFilePrefix)
QStringList UBPlatformUtils::availableTranslations()
{
QString localPreferredLanguage = preferredLanguage();
QString qmPath = applicationResourcesDirectory() + "/" + "i18n" + "/" + pFilePrefix + localPreferredLanguage + ".qm";
QString translationsPath = applicationResourcesDirectory() + "/" + "i18n" + "/";
QStringList translationsList = UBFileSystemUtils::allFiles(translationsPath);
QRegExp sankoreTranslationFiles(".*sankore_.*.qm");
translationsList=translationsList.filter(sankoreTranslationFiles);
return translationsList.replaceInStrings(QRegExp("(.*)sankore_(.*).qm"),"\\2");
}
QString UBPlatformUtils::translationPath(QString pFilePrefix,QString pLanguage)
{
QString qmPath = applicationResourcesDirectory() + "/" + "i18n" + "/" + pFilePrefix + pLanguage + ".qm";
return qmPath;
}
QString UBPlatformUtils::preferredLanguage()
QString UBPlatformUtils::systemLanguage()
{
return QLocale::system().name();
}

Loading…
Cancel
Save