localizationinfo.cpp
1 //-----------------------------------------------------------------------------
2 // localizationinfo.cpp
3 //-----------------------------------------------------------------------------
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 // 02110-1301 USA
18 //-----------------------------------------------------------------------------
19 // Copyright (C) 2012 "Zalewa" <zalewapl@gmail.com>
20 //-----------------------------------------------------------------------------
21 #include "localizationinfo.h"
22 
23 #include <QLocale>
24 #include <QStringList>
25 
27  // Unlike other translations, the "display name" in this one needs
28  // to be displayed in the currently active language. We cannot use
29  // tr() here because that would be init exactly once and also most
30  // likely before the translations are loaded.
31  "", "system", "system default"
32 };
33 
35  "GBR", "en", "English"
36 };
37 
38 LocalizationInfo LocalizationInfo::findBestMatch(
39  const QList<LocalizationInfo> &candidates,
40  const QString &localeName)
41 {
42  LocalizationInfo matchLanguage;
43  for (const LocalizationInfo &candidate : candidates)
44  {
45  LocaleMatch matchScore = matchLocale(candidate.localeName, localeName);
46  switch (matchScore)
47  {
48  case LocaleMatchCompletely:
49  return candidate;
50  case LocaleMatchLanguage:
51  matchLanguage = candidate;
52  break;
53  default:
54  // do nothing
55  break;
56  }
57  }
58  return matchLanguage;
59 }
60 
61 bool LocalizationInfo::isValid() const
62 {
63  return !localeName.isEmpty();
64 }
65 
66 bool LocalizationInfo::operator==(const LocalizationInfo &o2) const
67 {
68  return countryCodeName == o2.countryCodeName
69  && QLocale(localeName) == QLocale(o2.localeName)
70  && niceName == o2.niceName;
71 }
72 
73 QString LocalizationInfo::toString() const
74 {
75  return QString("%1;%2;%3").arg(countryCodeName, localeName, niceName);
76 }
77 
79 
80 LocaleMatch matchLocale(const QString &localeName1, const QString &localeName2)
81 {
82  if (localeName1.compare(localeName2, Qt::CaseInsensitive) == 0)
83  return LocaleMatchCompletely;
84  else if (localeName1.split("_")[0].compare(localeName2.split("_")[0], Qt::CaseInsensitive) == 0)
85  return LocaleMatchLanguage;
86  else
87  return LocaleNoMatch;
88 }