KOSMIndoorMap

languages.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "languages.h"
7
8#include <QLocale>
9
10static bool cmpLang(const std::string &lhs, QStringView rhs)
11{
12 return QAnyStringView::compare(QAnyStringView(lhs.c_str(), lhs.size()), rhs) == 0;
13}
14
15static void addLanguage(OSM::Languages &langs, QStringView langWithScript, QStringView lang)
16{
17 if (!langWithScript.isEmpty()) {
18 for (auto &it : langs.languages) {
19 if (cmpLang(it, langWithScript)) {
20 langWithScript = {};
21 break;
22 }
23 if (cmpLang(it, lang)) {
24 it = langWithScript.toUtf8().constData();
25 langWithScript = {};
26 break;
27 }
28 }
29 }
30 if (!langWithScript.isEmpty()) {
31 langs.languages.emplace_back(langWithScript.toUtf8().constData());
32 }
33
34 const auto it = std::find_if(langs.languages.begin(), langs.languages.end(), [lang](const auto &l) { return cmpLang(l, lang); });
35 if (it == langs.languages.end()) {
36 langs.languages.emplace_back(lang.toUtf8().constData());
37 }
38}
39
41{
42 Languages langs;
43
44 const auto uiLangs = locale.uiLanguages();
45 langs.languages.reserve(uiLangs.size());
46 for (const auto &uiLang : uiLangs) {
47 QStringView s(uiLang);
48 const auto dashCount = s.count(QLatin1Char('-'));
49 if (dashCount == 0) {
50 addLanguage(langs, {}, s);
51 continue;
52 }
53 if (dashCount > 1) {
54 const auto idx = s.lastIndexOf(QLatin1Char('-'));
55 s = s.left(idx);
56 }
57
58 const auto idx = s.indexOf(QLatin1Char('-'));
59 if (s.mid(idx + 1).size() == 2) {
60 // second part is a country code, that does not occur in OSM language keys
61 addLanguage(langs, {}, s.left(idx));
62 } else {
63 // second part is a script
64 addLanguage(langs, s, s.left(idx));
65 }
66 }
67
68 return langs;
69}
Languages in preference order to consider when looking up translated tag values.
Definition languages.h:25
static KOSM_EXPORT Languages fromQLocale(const QLocale &locale)
Convert QLocale::uiLanguages() into an OSM::Languages set.
Definition languages.cpp:40
int compare(QAnyStringView lhs, QAnyStringView rhs, Qt::CaseSensitivity cs)
const char * constData() const const
QStringList uiLanguages() const const
QStringView left(qsizetype length) const const
QStringView mid(qsizetype start, qsizetype length) const const
qsizetype count(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype indexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs) const const
qsizetype size() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:55:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.