[gelöst] language wechsel geht nicht
Verfasst: 12. August 2006 17:02
hi
ich habe in mein Project language suppor thinzugefügt
aber es kann nicht vom Preferences dialog ausgewählt werden:
ich habe jeweils eine *.de.ts, *.en.ts und noch die dazugehörigen *.de.qm und *.en.qm erstellt alles ist im lang verzeichnis
es müsste in dem combo box auswählbar sein
die flage + Text man kann leider nichts auswählen....

so müsste es eigentlich anzeigen:

hier mein languagesupport.cpp und .h
die befindet sich in lang/ verzeichnis
hier ist mein PreferencesDialog.cpp und .h
main.cpp:
und die dazu gehörige .cpp/.h
im gleichen verzeichnis wie main.cpp
myapp.cpp/myapp.h
ich habe in mein Project language suppor thinzugefügt
aber es kann nicht vom Preferences dialog ausgewählt werden:
ich habe jeweils eine *.de.ts, *.en.ts und noch die dazugehörigen *.de.qm und *.en.qm erstellt alles ist im lang verzeichnis
es müsste in dem combo box auswählbar sein
die flage + Text man kann leider nichts auswählen....

so müsste es eigentlich anzeigen:

hier mein languagesupport.cpp und .h
die befindet sich in lang/ verzeichnis
Code: Alles auswählen
#include <QTranslator>
#include <QLocale>
#include "languagesupport.h"
/** Static list of supported languages and codes. */
QMap<QString, QString> LanguageSupport::_languages;
/** Initializes the list of available languages. */
void
LanguageSupport::initialize()
{
_languages.clear();
_languages.insert("en", "English");
_languages.insert("de", "Deutsch");
}
/** Returns the default language code for the system locale. */
QString
LanguageSupport::defaultLanguageCode()
{
QString localeName = QLocale::system().name();
QString language = localeName.mid(0, localeName.indexOf("_"));
if (!isValidLanguageCode(language)) {
language = "en";
}
return language;
}
/** Returns the language code for a given language name. */
QString
LanguageSupport::languageCode(QString languageName)
{
return _languages.key(languageName);
}
/** Returns a list of all supported language codes. (e.g., "en"). */
QStringList
LanguageSupport::languageCodes()
{
return _languages.keys();
}
/** Returns the language name for a given language code. */
QString
LanguageSupport::languageName(QString languageCode)
{
return _languages.value(languageCode);
}
/** Returns a list of all supported language names (e.g., "English"). */
QStringList
LanguageSupport::languageNames()
{
return _languages.values();
}
/** Returns a list of all supported language codes and names. */
QMap<QString, QString>
LanguageSupport::languages()
{
return _languages;
}
/** Returns true if we understand the given language code. */
bool
LanguageSupport::isValidLanguageCode(QString code)
{
return languageCodes().contains(code.toLower());
}
/** Sets the application's translator to the specified language. */
bool
LanguageSupport::translate(QString langCode)
{
if (isValidLanguageCode(langCode)) {
QTranslator *translator = new QTranslator();
if (translator->load(QString(":/lang/") + langCode.toLower())) {
QApplication::installTranslator(translator);
return true;
}
delete translator;
}
return false;
}
Code: Alles auswählen
#ifndef _LANGUAGESUPPORT_H
#define _LANGUAGESUPPORT_H
#include <QApplication>
#include <QStringList>
#include <QMap>
class LanguageSupport
{
public:
/** Initializes the list of supported languages. */
static void initialize();
/** Returns the default language code for the system locale. */
static QString defaultLanguageCode();
/** Returns the language code for a given language name. */
static QString languageCode(QString languageName);
/** Returns a list of all supported language codes (e.g., "en"). */
static QStringList languageCodes();
/** Returns the language name for a given language code. */
static QString languageName(QString languageCode);
/** Returns a list of all supported language names (e.g., "English"). */
static QStringList languageNames();
/** Returns a list of all supported language codes and names. */
static QMap<QString, QString> languages();
/** Returns true if we understand the given language code. */
static bool isValidLanguageCode(QString code);
/** Sets the application's translator to the specified language. */
static bool translate(QString langCode);
private:
static QMap<QString,QString> _languages; /**< List of support languages. */
};
#endifCode: Alles auswählen
#ifndef _PREFERENCESDIALOG_H
#define _PREFERENCESDIALOG_H
#include <QFileDialog>
#include <QStyleFactory>
#include <QLineEdit>
#include <config/mysettings.h>
#include <lang/languagesupport.h>
#include "gui/configpage.h"
#include "ui_PreferencesDialog.h"
class PreferencesDialog : public ConfigPage
{
Q_OBJECT
public:
/** Default Constructor */
PreferencesDialog(QWidget *parent = 0);
/** Default Destructor */
~PreferencesDialog();
/** Saves the changes on this page */
bool save(QString &errmsg);
/** Loads the settings for this page */
void load();
private slots:
private:
/** A mySettings object used for saving/loading settings */
MySettings* _settings;
/** Qt Designer generated object */
Ui::PreferencesDialog ui;
};
#endif
Code: Alles auswählen
#ifndef _PREFERENCESDIALOG_H
#define _PREFERENCESDIALOG_H
#include <QFileDialog>
#include <QStyleFactory>
#include <QLineEdit>
#include <config/mysettings.h>
#include <lang/languagesupport.h>
#include "gui/configpage.h"
#include "ui_PreferencesDialog.h"
class PreferencesDialog : public ConfigPage
{
Q_OBJECT
public:
/** Default Constructor */
PreferencesDialog(QWidget *parent = 0);
/** Default Destructor */
~PreferencesDialog();
/** Saves the changes on this page */
bool save(QString &errmsg);
/** Loads the settings for this page */
void load();
private slots:
private:
/** A MySettings object used for saving/loading settings */
mySettings* _settings;
/** Qt Designer generated object */
Ui::PreferencesDialog ui;
};
#endif
Code: Alles auswählen
#include <gui/MainWindow.h>
int main( int argc, char ** argv )
{
QApplication app( argc, argv );
MainWindow w;
w.show();
return app.exec();
}im gleichen verzeichnis wie main.cpp
myapp.cpp/myapp.h
Code: Alles auswählen
#include <QDir>
#include <QTextStream>
#include <QStyleFactory>
#include <util/string.h>
#include <lang/languagesupport.h>
#include "myapp.h"
/* Available command-line arguments. */
#define ARG_LANGUAGE "lang" /**< Argument specifying language. */
#define ARG_GUISTYLE "style" /**< Argument specfying GUI style. */
#define ARG_RESET "reset" /**< Reset Myapp's saved settings. */
#define ARG_DATADIR "datadir" /**< Directory to use for data files. */
#define ARG_PIDFILE "pidfile" /**< Location and name of our pidfile.*/
/* Static member variables */
QMap<QString, QString> Myapp::_args; /**< List of command-line arguments. */
QString Myapp::_style; /**< The current GUI style. */
QString Myapp::_language; /**< The current language. */
MySettings Myapp::_settings; /**< Myapp's configurable settings. */
/** Constructor. Parses the command-line arguments, resets Myapp's
* configuration (if requested), and sets up the GUI style and language
* translation. */
Myapp::Myapp(QStringList args, int &argc, char **argv)
: QApplication(argc, argv)
{
/* Read in all our command-line arguments. */
parseArguments(args);
/* Check if we're supposed to reset our config before proceeding. */
if (_args.contains(ARG_RESET)) {
_settings.reset();
}
/** Initialize support for language translations. */
LanguageSupport::initialize();
/** Translate the GUI to the appropriate language. */
setLanguage(_args.value(ARG_LANGUAGE));
/** Set the GUI style appropriately. */
setStyle(_args.value(ARG_GUISTYLE));
}
#if defined(Q_OS_WIN)
/** On Windows, we need to catch the WM_QUERYENDSESSION message
* so we know that it is time to shutdown. */
bool
Myapp::winEventFilter(MSG *msg, long *result)
{
if (msg->message == WM_QUERYENDSESSION) {
emit shutdown();
}
return QApplication::winEventFilter(msg, result);
}
#endif
/** Display usage information regarding command-line arguments. */
void
Myapp::printUsage(QString errmsg)
{
QTextStream out(stdout);
/* If there was an error message, print it out. */
if (!errmsg.isEmpty()) {
out << "** " << errmsg << " **" << endl << endl;
}
/* Now print the application usage */
out << "Usage: " << endl;
out << "\t" << qApp->arguments().at(0) << " [options]" << endl;
/* And available options */
out << endl << "Available Options:" << endl;
out << "\t-"ARG_RESET"\t\tResets ALL stored Myapp settings." << endl;
out << "\t-"ARG_DATADIR"\tSets the directory Myapp uses for data files"<< endl;
out << "\t-"ARG_PIDFILE"\tSets the name and location of Myapp's pidfile"<< endl;
out << "\t-"ARG_GUISTYLE"\t\tSets Myapp's interface style." << endl;
out << "\t\t\t[" << QStyleFactory::keys().join("|") << "]" << endl;
out << "\t-"ARG_LANGUAGE"\t\tSets Myapp's language." << endl;
out << "\t\t\t[" << LanguageSupport::languageCodes().join("|") << "]" << endl;
}
/** Returns true if the specified argument expects a value. */
Myapp::argNeedsValue(QString argName)
{
return (argName == ARG_GUISTYLE ||
argName == ARG_LANGUAGE ||
argName == ARG_DATADIR ||
argName == ARG_PIDFILE);
}
/** Parses the list of command-line arguments for their argument names and
* values. */
void
Myapp::parseArguments(QStringList args)
{
QString arg, value;
/* Loop through all command-line args/values and put them in a map */
for (int i = 0; i < args.size(); i++) {
/* Get the argument name and set a blank value */
arg = args.at(i).toLower();
value = "";
/* Check if it starts with a - or -- */
if (arg.startsWith("-")) {
arg = arg.mid((arg.startsWith("--") ? 2 : 1));
}
/* Check if it takes a value and there is one on the command-line */
if (i < args.size()-1 && argNeedsValue(arg)) {
value = args.at(++i);
}
/* Place this arg/value in the map */
_args.insert(arg, value);
}
}
/** Verifies that all specified arguments were valid. */
bool
Myapp::validateArguments(QString &errmsg)
{
/* If they want help, just return false now
if (_args.contains(ARG_HELP)) {
return false;
}*/
/* Check for a language that Myapp recognizes. */
if (_args.contains(ARG_LANGUAGE) &&
!LanguageSupport::isValidLanguageCode(_args.value(ARG_LANGUAGE))) {
errmsg = tr("Invalid language code specified: ") + _args.value(ARG_LANGUAGE);
return false;
}
/* Check for a valid GUI style */
if (_args.contains(ARG_GUISTYLE) &&
!QStyleFactory::keys().contains(_args.value(ARG_GUISTYLE),
Qt::CaseInsensitive)) {
errmsg = tr("Invalid GUI style specified: ") + _args.value(ARG_GUISTYLE);
return false;
}
return true;
}
/** Sets the translation Myapp will use. If one was specified on the
* command-line, we will use that. Otherwise, we'll check to see if one was
* saved previously. If not, we'll default to one appropriate for the system
* locale. */
bool
Myapp::setLanguage(QString languageCode)
{
/* If the language code is empty, use the previously-saved setting */
if (languageCode.isEmpty()) {
languageCode = _settings.getLanguageCode();
}
/* Translate into the desired langauge */
if (LanguageSupport::translate(languageCode)) {
_language = languageCode;
return true;
}
return false;
}
/** Sets the GUI style Myapp will use. If one was specified on the
* command-line, we will use that. Otherwise, we'll check to see if one was
* saved previously. If not, we'll default to one appropriate for the
* operating system. */
bool
Myapp::setStyle(QString styleKey)
{
/* If no style was specified, use the previously-saved setting */
if (styleKey.isEmpty()) {
styleKey = _settings.getInterfaceStyle();
}
/* Apply the specified GUI style */
if (QApplication::setStyle(styleKey)) {
_style = styleKey;
return true;
}
return false;
}
/** Returns the directory Myapp uses for its data files. */
QString
Myapp::dataDirectory()
{
if (_args.contains(ARG_DATADIR)) {
return _args.value(ARG_DATADIR);
}
return defaultDataDirectory();
}
/** Returns the default location of Myapp's data directory. */
QString
Myapp::defaultDataDirectory()
{
#if defined(Q_OS_WIN32)
return (win32_app_data_folder() + "\\Myapp");
#else
return (QDir::homePath() + "/.Myapp");
#endif
}
/** Creates Myapp's data directory, if it doesn't already exist. */
bool
Myapp::createDataDirectory(QString *errmsg)
{
QDir datadir(dataDirectory());
if (!datadir.exists()) {
QString path = datadir.absolutePath();
if (!datadir.mkpath(path)) {
return err(errmsg,
QString("Could not create data directory: %1").arg(path));
}
}
return true;
}
/** Returns the location of Myapp's pid file. */
QString
Myapp::pidFile()
{
if (_args.contains(ARG_PIDFILE)) {
return _args.value(ARG_PIDFILE);
}
return QDir::convertSeparators(dataDirectory() + "/Myapp.pid");
}Code: Alles auswählen
#ifndef _MYAPP_H
#define _MYAPP_H
#if defined(Q_OS_WIN)
#include <windows.h>
#include <util/win32.h>
#endif
#include <QApplication>
#include <QMap>
#include <QString>
#include <config/mysettings.h>
class Myapp : public QApplication
{
Q_OBJECT
public:
/** Constructor. */
Myapp(QStringList args, int &argc, char **argv);
/** Destructor. */
~Myapp();
/** Return the map of command-line arguments and values. */
static QMap<QString, QString> arguments() { return _args; }
/** Validates that all arguments were well-formed. */
bool validateArguments(QString &errmsg);
/** Prints usage information to the given text stream. */
void printUsage(QString errmsg = QString());
/** Sets the current language. */
static bool setLanguage(QString languageCode = QString());
/** Sets the current GUI style. */
static bool setStyle(QString styleKey = QString());
/** Shows the specified help topic, or the default if empty. */
//static void help(QString topic = QString());
/** Returns the current language. */
static QString language() { return _language; }
/** Returns the current GUI style. */
static QString style() { return _style; }
/** Returns Myapp's application version. */
/** Returns the location Myapp uses for its data files. */
static QString dataDirectory();
/** Returns the default location of Myapp's data directory. */
static QString defaultDataDirectory();
/** Creates Myapp's data directory, if it doesn't already exist. */
static bool createDataDirectory(QString *errmsg);
/** Returns the location of Myapp's pid file. */
static QString pidFile();
signals:
/** Signals that the application needs to shutdown now. */
void shutdown();
protected:
#if defined(Q_OS_WIN)
/** Filters Windows events, looking for events of interest */
bool winEventFilter(MSG *msg, long *result);
#endif
private:
/** Parse the list of command-line arguments. */
void parseArguments(QStringList args);
/** Returns true if the specified arguments wants a value. */
bool argNeedsValue(QString argName);
static QMap<QString, QString> _args; /**< List of command-line arguments. */
static QString _style; /**< The current GUI style. */
static QString _language; /**< The current language. */
static MySettings _settings; /**< Myapp's configurable settings. */
};
#endif