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: key + QLatin1Char('[') + languageWithCountry + QLatin1Char(']'));
16 if (it != jo.constEnd()) {
17 return it.value();
18 }
19 const QStringView language = QStringView(languageWithCountry).mid(pos: 0, n: languageWithCountry.indexOf(c: QLatin1Char('_')));
20 it = jo.constFind(key: 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}
36

source code of kcoreaddons/src/lib/text/kjsonutils.cpp