diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2011-02-21 16:42:48 (GMT) |
---|---|---|
committer | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2011-02-23 16:20:57 (GMT) |
commit | 18508ec10d0e1128b85dfb3fddfa3dee6a2b1ae9 (patch) | |
tree | c51bd5a16ef172255afecfa5e69ebb07c8aab468 /src/corelib/kernel | |
parent | c9071e8f9a26dee3a166579df69da11e0f6323fa (diff) | |
download | Qt-18508ec10d0e1128b85dfb3fddfa3dee6a2b1ae9.zip Qt-18508ec10d0e1128b85dfb3fddfa3dee6a2b1ae9.tar.gz Qt-18508ec10d0e1128b85dfb3fddfa3dee6a2b1ae9.tar.bz2 |
QTranslator improvements for QLocale UI languages
To be able to make use of the new QLocale::uiLanguages property, here is a new
overload in QTranslator::load that takes QLocale object as an argument and
tries to load the translation based on educated guesses of the locale name and
file name.
Task-number: related to QTBUG-7329
Reviewed-by: Zeno Albisser
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qtranslator.cpp | 149 | ||||
-rw-r--r-- | src/corelib/kernel/qtranslator.h | 6 |
2 files changed, 155 insertions, 0 deletions
diff --git a/src/corelib/kernel/qtranslator.cpp b/src/corelib/kernel/qtranslator.cpp index d72c1ab..bdb1d5f 100644 --- a/src/corelib/kernel/qtranslator.cpp +++ b/src/corelib/kernel/qtranslator.cpp @@ -47,6 +47,7 @@ #include "qfileinfo.h" #include "qstring.h" +#include "qstringlist.h" #include "qcoreapplication.h" #include "qcoreapplication_p.h" #include "qdatastream.h" @@ -55,6 +56,7 @@ #include "qalgorithms.h" #include "qhash.h" #include "qtranslator_p.h" +#include "qlocale.h" #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) #define QT_USE_MMAP @@ -236,6 +238,7 @@ public: uint contextLength; uint numerusRulesLength; + bool do_load(const QString &filename); bool do_load(const uchar *data, int len); QString do_translate(const char *context, const char *sourceText, const char *comment, int n) const; @@ -435,7 +438,12 @@ bool QTranslator::load(const QString & filename, const QString & directory, } // realname is now the fully qualified name of a readable file. + return d->do_load(realname); +} +bool QTranslatorPrivate::do_load(const QString &realname) +{ + QTranslatorPrivate *d = this; bool ok = false; #ifdef QT_USE_MMAP @@ -497,6 +505,147 @@ bool QTranslator::load(const QString & filename, const QString & directory, return d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength); } +static QString find_translation(const QLocale & locale, + const QString & filename, + const QString & prefix, + const QString & directory, + const QString & suffix) +{ + QString path; + if (QFileInfo(filename).isRelative()) { + path = directory; + if (!path.isEmpty() && !path.endsWith(QLatin1Char('/'))) + path += QLatin1Char('/'); + } + + QFileInfo fi; + QString realname; + QStringList fuzzyLocales; + + // see http://www.unicode.org/reports/tr35/#LanguageMatching for inspiration + + QStringList languages = locale.uiLanguages(); +#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) + for (int i = languages.size()-1; i >= 0; --i) { + QString lang = languages.at(i); + QString lowerLang = lang.toLower(); + if (lang != lowerLang) + languages.insert(i+1, lowerLang); + } +#endif + + // try explicit locales names first + foreach (QString localeName, languages) { + localeName.replace(QLatin1Char('-'), QLatin1Char('_')); + + realname = path + filename + prefix + localeName + (suffix.isNull() ? QLatin1String(".qm") : suffix); + fi.setFile(realname); + if (fi.isReadable() && fi.isFile()) + return realname; + + realname = path + filename + prefix + localeName; + fi.setFile(realname); + if (fi.isReadable() && fi.isFile()) + return realname; + + fuzzyLocales.append(localeName); + } + + // start guessing + foreach (QString localeName, fuzzyLocales) { + for (;;) { + int rightmost = localeName.lastIndexOf(QLatin1Char('_')); + // no truncations? fail + if (rightmost <= 0) + break; + localeName.truncate(rightmost); + + realname = path + filename + prefix + localeName + (suffix.isNull() ? QLatin1String(".qm") : suffix); + fi.setFile(realname); + if (fi.isReadable() && fi.isFile()) + return realname; + + realname = path + filename + prefix + localeName; + fi.setFile(realname); + if (fi.isReadable() && fi.isFile()) + return realname; + } + } + + if (!suffix.isNull()) { + realname = path + filename + suffix; + fi.setFile(realname); + if (fi.isReadable() && fi.isFile()) + return realname; + } + + realname = path + filename + prefix; + fi.setFile(realname); + if (fi.isReadable() && fi.isFile()) + return realname; + + realname = path + filename; + fi.setFile(realname); + if (fi.isReadable() && fi.isFile()) + return realname; + + return QString(); +} + +/*! + \since 4.8 + + Loads \a filename + \a prefix + \a locale name + \a suffix (".qm" if the \a + suffix is not specified), which may be an absolute file name or relative to + \a directory. Returns true if the translation is successfully loaded; + otherwise returns false. + + The previous contents of this translator object are discarded. + + If the file name does not exist, other file names are tried + in the following order: + + \list 1 + \o File name without \a suffix appended. + \o File name with locale part after a "_" character stripped and \a suffix. + \o File name with locale part stripped without \a suffix appended. + \o File name with locale part stripped further, etc. + \endlist + + For example, an application running in the locale with the following + l{QLocale::uiLanguages()}{ui languages} - "es", "fr-CA", "de" might call + load(QLocale::system(), "foo", ".", "/opt/foolib", ".qm"). load() would + then try to open the first existing readable file from this list: + + \list 1 + \o \c /opt/foolib/foo.es.qm + \o \c /opt/foolib/foo.es + \o \c /opt/foolib/foo.fr_CA.qm + \o \c /opt/foolib/foo.fr_CA + \o \c /opt/foolib/foo.de.qm + \o \c /opt/foolib/foo.de + \o \c /opt/foolib/foo.fr.qm + \o \c /opt/foolib/foo.fr + \o \c /opt/foolib/foo.qm + \o \c /opt/foolib/foo. + \o \c /opt/foolib/foo + \endlist + + For OSs where file system is case sensitive, QTranslator also tries to load + a lower-cased version of the locale name. +*/ +bool QTranslator::load(const QLocale & locale, + const QString & filename, + const QString & prefix, + const QString & directory, + const QString & suffix) +{ + Q_D(QTranslator); + d->clear(); + QString fname = find_translation(locale, filename, prefix, directory, suffix); + return !fname.isEmpty() && d->do_load(fname); +} + /*! \overload load() \fn bool QTranslator::load(const uchar *data, int len) diff --git a/src/corelib/kernel/qtranslator.h b/src/corelib/kernel/qtranslator.h index 33e7f03..7a4fa00 100644 --- a/src/corelib/kernel/qtranslator.h +++ b/src/corelib/kernel/qtranslator.h @@ -53,6 +53,7 @@ QT_MODULE(Core) #ifndef QT_NO_TRANSLATION +class QLocale; class QTranslatorPrivate; class Q_CORE_EXPORT QTranslator : public QObject @@ -77,6 +78,11 @@ public: const QString & directory = QString(), const QString & search_delimiters = QString(), const QString & suffix = QString()); + bool load(const QLocale & locale, + const QString & filename, + const QString & prefix = QString(), + const QString & directory = QString(), + const QString & suffix = QString()); bool load(const uchar *data, int len); #ifdef QT3_SUPPORT |