-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLocalization.cpp
More file actions
118 lines (91 loc) · 3.94 KB
/
Copy pathLocalization.cpp
File metadata and controls
118 lines (91 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Created by OpenAI Codex on 29/04/26.
//
#include "Localization.hpp"
#include <QStringList>
#include "Configuration.hpp"
namespace fairwindsk::localization {
namespace {
QLocale englishFallbackLocale() {
return QLocale(QLocale::English, QLocale::UnitedStates);
}
QLocale defaultLocaleForLanguage(const QString &languageCode) {
if (languageCode == QStringLiteral("it")) {
return QLocale(QLocale::Italian, QLocale::Italy);
}
return englishFallbackLocale();
}
bool isSupportedSystemLanguage(const QLocale &locale) {
return locale.language() == QLocale::English || locale.language() == QLocale::Italian;
}
}
QString normalizeLanguageSelection(const QString &value) {
QString normalized = value.trimmed().toLower();
normalized.replace(QLatin1Char('-'), QLatin1Char('_'));
if (normalized.isEmpty() || normalized == QStringLiteral("system")) {
return QStringLiteral("system");
}
const QString languageCode = normalized.section(QLatin1Char('_'), 0, 0);
if (languageCode == QStringLiteral("en") || languageCode == QStringLiteral("it")) {
return languageCode;
}
return QStringLiteral("en");
}
QString effectiveLanguageCodeForSelection(const QString &value) {
const QString normalized = normalizeLanguageSelection(value);
if (normalized == QStringLiteral("en") || normalized == QStringLiteral("it")) {
return normalized;
}
return QLocale::system().language() == QLocale::Italian ? QStringLiteral("it") : QStringLiteral("en");
}
QString effectiveLanguageCode(const Configuration &configuration) {
return effectiveLanguageCodeForSelection(configuration.getLanguage());
}
QLocale effectiveLocaleForSelection(const QString &value) {
const QString normalized = normalizeLanguageSelection(value);
if (normalized == QStringLiteral("system")) {
const QLocale systemLocale = QLocale::system();
return isSupportedSystemLanguage(systemLocale) ? systemLocale : englishFallbackLocale();
}
return defaultLocaleForLanguage(normalized);
}
QLocale effectiveLocale(const Configuration &configuration) {
return effectiveLocaleForSelection(configuration.getLanguage());
}
QString languageTag(const QLocale &locale) {
const QString tag = locale.bcp47Name().trimmed();
if (tag.isEmpty() || tag == QStringLiteral("C")) {
return QStringLiteral("en-US");
}
return tag;
}
QString cultureName(const QLocale &locale) {
const QString name = locale.name().trimmed();
if (name.isEmpty() || name == QStringLiteral("C")) {
return QStringLiteral("en_US");
}
return name;
}
QString webAcceptLanguageHeader(const QLocale &locale) {
const QString tag = languageTag(locale);
const QString primaryLanguage = tag.section(QLatin1Char('-'), 0, 0).toLower();
QStringList languages;
languages.append(tag);
if (tag.compare(primaryLanguage, Qt::CaseInsensitive) != 0) {
languages.append(primaryLanguage == QStringLiteral("en")
? QStringLiteral("en;q=0.9")
: QStringLiteral("%1;q=0.9").arg(primaryLanguage));
}
if (primaryLanguage != QStringLiteral("en")) {
languages.append(QStringLiteral("en;q=0.8"));
}
return languages.join(QLatin1Char(','));
}
QString translationResourcePath(const QString &languageCode) {
const QString normalized = normalizeLanguageSelection(languageCode);
if (normalized == QStringLiteral("en") || normalized == QStringLiteral("system")) {
return QString();
}
return QStringLiteral(":/resources/i18n/fairwindsk_%1.qm").arg(normalized);
}
}