diff --git a/autotests/addresstest.cpp b/autotests/addresstest.cpp --- a/autotests/addresstest.cpp +++ b/autotests/addresstest.cpp @@ -32,7 +32,7 @@ #ifndef Q_OS_WIN void initLocale() { - qputenv("LC_ALL", "en_US.utf-8"); + qputenv("LC_ALL", "it_CH.utf-8"); } Q_CONSTRUCTOR_FUNCTION(initLocale) @@ -214,10 +214,24 @@ address.setLocality(QStringLiteral("Lummerstadt")); address.setCountry(QString()); - const QString result(QStringLiteral("Jim Knopf\nLummerlandstr. 1\nLummerstadt, 12345")); + const QString result(QStringLiteral("Jim Knopf\nLummerlandstr. 1\n12345 Lummerstadt")); QCOMPARE(address.formattedAddress(QStringLiteral("Jim Knopf")), result); } + + { + KContacts::Address address; + address.setStreet(QStringLiteral("Haus Randa")); + address.setPostalCode(QStringLiteral("1234")); + address.setLocality(QStringLiteral("Randa")); + address.setPostOfficeBox(QStringLiteral("5678")); + address.setCountry(QStringLiteral("Schweiz")); + + // we want the Italian variant of the Swiss format for it_CH + const QString result(QStringLiteral("Dr. Konqui\nCasella postale 5678\nHaus Randa\n1234 Randa\n\nSCHWEIZ")); + + QCOMPARE(address.formattedAddress(QStringLiteral("Dr. Konqui")), result); + } } void AddressTest::shouldExportVcard3() diff --git a/src/address.cpp b/src/address.cpp --- a/src/address.cpp +++ b/src/address.cpp @@ -576,6 +576,16 @@ return str; } +static QString countryCodeFromLocale() +{ + const auto n = QLocale().name(); // this is in the form _, with the exception of 'C' + const auto idx = n.indexOf(QLatin1Char('_')); + if (idx > 0) { + return n.mid(idx + 1).toLower(); + } + return {}; +} + QString Address::formattedAddress(const QString &realName, const QString &orgaName) const { QString ciso; @@ -587,7 +597,7 @@ ciso = countryToISO(country()); } else { // fall back to our own country - ciso = QLocale().bcp47Name(); + ciso = countryCodeFromLocale(); } KConfig entry(QStringLiteral(":/org.kde.kcontacts/addressformatrc")); @@ -617,7 +627,7 @@ // now add the country line if needed (formatting this time according to // the rules of our own system country ) if (!country().isEmpty()) { - KConfigGroup group = entry.group(QLocale().name()); + KConfigGroup group = entry.group(countryCodeFromLocale()); QString cpos = group.readEntry("AddressCountryPosition"); if (QLatin1String("BELOW") == cpos || cpos.isEmpty()) { ret = ret + QLatin1String("\n\n") + country().toUpper();