fix handling of country-less languages like esperanto
authorOswald Buddenhagen <oswald.buddenhagen@digia.com>
Fri, 16 Aug 2013 19:33:22 +0000 (21:33 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Mon, 19 Aug 2013 16:46:59 +0000 (18:46 +0200)
it's impossible to instantiate QLocale objects like that, so the
language setting would be simply clobbered. instead, we now convert
between numerical and string codes directly.

unfortunately QLocale has no public api for that. but we are including
private QTranslator headers already anyway, so whatever.

Task-number: QTBUG-14592
Change-Id: I95189c1898aa1fb5520ecf7057521597ed9331f1
Reviewed-by: hjk <hjk121@nokiamail.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
src/linguist/shared/translator.cpp

index f5f4ab7..37929f3 100644 (file)
@@ -62,6 +62,7 @@
 #include <QtCore/QFileInfo>
 #include <QtCore/QTextStream>
 
+#include <private/qlocale_p.h>
 #include <private/qtranslator_p.h>
 
 QT_BEGIN_NAMESPACE
@@ -367,30 +368,19 @@ bool Translator::save(const QString &filename, ConversionData &cd, const QString
 
 QString Translator::makeLanguageCode(QLocale::Language language, QLocale::Country country)
 {
-    QLocale locale(language, country);
-    if (country == QLocale::AnyCountry) {
-        QString languageCode = locale.name().section(QLatin1Char('_'), 0, 0);
-        if (languageCode.length() <= 3)
-            return languageCode;
-        return QString();
-    } else {
-        return locale.name();
+    QString result = QLocalePrivate::languageToCode(language);
+    if (language != QLocale::C && country != QLocale::AnyCountry) {
+        result.append(QLatin1Char('_'));
+        result.append(QLocalePrivate::countryToCode(country));
     }
+    return result;
 }
 
 void Translator::languageAndCountry(const QString &languageCode,
     QLocale::Language *lang, QLocale::Country *country)
 {
-    QLocale locale(languageCode);
-    if (lang)
-        *lang = locale.language();
-
-    if (country) {
-        if (languageCode.indexOf(QLatin1Char('_')) != -1)
-            *country = locale.country();
-        else
-            *country = QLocale::AnyCountry;
-    }
+    QLocale::Script script;
+    QLocalePrivate::getLangAndCountry(languageCode, *lang, script, *country);
 }
 
 int Translator::find(const TranslatorMessage &msg) const