KCoreAddons

kjsonutils.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Alex Richardson <arichardson.kde@gmail.com>
3 SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kjsonutils.h"
9
10#include <QJsonObject>
11
12QJsonValue KJsonUtils::readTranslatedValue(const QJsonObject &jo, const QString &key, const QJsonValue &defaultValue)
13{
14 QString languageWithCountry = QLocale().name();
15 auto it = jo.constFind(key + QLatin1Char('[') + languageWithCountry + QLatin1Char(']'));
16 if (it != jo.constEnd()) {
17 return it.value();
18 }
19 const QStringView language = QStringView(languageWithCountry).mid(0, languageWithCountry.indexOf(QLatin1Char('_')));
20 it = jo.constFind(key + QLatin1Char('[') + language + QLatin1Char(']'));
21 if (it != jo.constEnd()) {
22 return it.value();
23 }
24 // no translated value found -> check key
25 it = jo.constFind(key);
26 if (it != jo.constEnd()) {
27 return jo.value(key);
28 }
29 return defaultValue;
30}
31
32QString KJsonUtils::readTranslatedString(const QJsonObject &jo, const QString &key, const QString &defaultValue)
33{
34 return KJsonUtils::readTranslatedValue(jo, key, defaultValue).toString(defaultValue);
35}
const_iterator constEnd() const const
const_iterator constFind(QLatin1StringView key) const const
QJsonValue value(QLatin1StringView key) const const
QString toString() const const
QString name() const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QStringView mid(qsizetype start, qsizetype length) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.