File indexing completed on 2024-05-19 05:49:21

0001 /***************************************************************************
0002  *   Copyright © 2015 Harald Sitter <sitter@kde.org>                       *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or         *
0005  *   modify it under the terms of the GNU General Public License as        *
0006  *   published by the Free Software Foundation; either version 2 of        *
0007  *   the License or (at your option) version 3 or any later version        *
0008  *   accepted by the membership of KDE e.V. (or its successor approved     *
0009  *   by the membership of KDE e.V.), which shall act as a proxy            *
0010  *   defined in Section 14 of version 3 of the license.                    *
0011  *                                                                         *
0012  *   This program is distributed in the hope that it will be useful,       *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0015  *   GNU General Public License for more details.                          *
0016  *                                                                         *
0017  *   You should have received a copy of the GNU General Public License     *
0018  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0019  ***************************************************************************/
0020 
0021 #include "locale.h"
0022 
0023 #include <algorithm>
0024 
0025 Locale::Locale(const QString &locale)
0026     : m_language(QString())
0027     , m_country(QString())
0028     , m_variant(QString())
0029     , m_encoding(QString())
0030 {
0031     QString tmp = locale;
0032     QStringList parts;
0033 
0034     parts = tmp.split(QChar('.'));
0035     if (parts.size() > 1) {
0036         m_encoding = parts.last();
0037     }
0038     tmp = parts.first();
0039 
0040     parts = tmp.split(QChar('@'));
0041     if (parts.size() > 1) {
0042         m_variant = parts.last();
0043     }
0044     tmp = parts.first();
0045 
0046     parts = tmp.split(QChar('_'));
0047     if (parts.size() > 1) {
0048         m_country = parts.last();
0049     }
0050     m_language = parts.first();
0051 }
0052 
0053 // This function returns a combination of possible locale values.
0054 // The way this works is that it fills a list with all possibly combination on
0055 // top of language. All combiners can return a noop version of the string
0056 // passed into the combiner if the combinee is empty. This ultimately results
0057 // in duplicated entries (e.g. if variant is empty the list would contain
0058 // language and langauge.encoding twice as their respective variantified version
0059 // comes back without variant).
0060 // Once all combinations are attempted the by-design-duplicates are removed
0061 // and we have a nice clean list of language combinations.
0062 // This has slight overhead since we are partially working with duplicated
0063 // strings at times, it does however need substantially less code and less
0064 // iffing.
0065 QStringList Locale::combinations()
0066 {
0067     QStringList list;
0068     // language_country@variant.encoding
0069     // language_country@variant
0070     // language@variant.encoding
0071     // language@variant
0072     // language_country.encoding
0073     // language_country
0074     // language.encoding
0075     // language
0076     // ^ all built in reverse order for improved readability
0077     QString tmp;
0078     if (!m_language.isEmpty()) {
0079         list << (tmp = m_language);
0080         list << encodify(tmp);
0081 
0082         list << (tmp = countryfy(m_language));
0083         list << encodify(tmp);
0084 
0085         list << (tmp = variantify(m_language));
0086         list << encodify(tmp);
0087 
0088         list << (tmp = variantify(countryfy(m_language)));
0089         list << encodify(tmp);
0090     }
0091     list.removeDuplicates();
0092     std::reverse(std::begin(list), std::end(list));
0093     return list;
0094 }
0095 
0096 QString Locale::countryfy(const QString &str)
0097 {
0098     if (m_country.isEmpty())
0099         return str;
0100     return str + '_' + m_country;
0101 }
0102 
0103 QString Locale::variantify(const QString &str)
0104 {
0105     if (m_variant.isEmpty())
0106         return str;
0107     return str + '@' + m_variant;
0108 }
0109 
0110 QString Locale::encodify(const QString &str)
0111 {
0112     if (m_encoding.isEmpty())
0113         return str;
0114     return str + '.' + m_encoding;
0115 }