-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlocale.cpp
More file actions
97 lines (84 loc) · 2.87 KB
/
Copy pathlocale.cpp
File metadata and controls
97 lines (84 loc) · 2.87 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
#include "locale.hpp"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <locale>
#include <nlohmann/json.hpp>
#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
#endif
namespace acecode::desktop {
namespace {
std::string lowercase_ascii(std::string value) {
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char ch) {
return static_cast<char>(std::tolower(ch));
});
return value;
}
bool starts_with(const std::string& value, const std::string& prefix) {
return value.size() >= prefix.size() &&
value.compare(0, prefix.size(), prefix) == 0;
}
} // namespace
bool is_supported_locale(const std::string& locale) {
return locale == kLocaleZhCn || locale == kLocaleEnUs;
}
std::string locale_from_system_tag(const std::string& system_tag) {
const std::string value = lowercase_ascii(system_tag);
if (value == "zh" || starts_with(value, "zh-") ||
starts_with(value, "zh_") || starts_with(value, "chinese")) {
return kLocaleZhCn;
}
return kLocaleEnUs;
}
std::string resolve_ui_locale(const std::string& preference,
const std::string& system_tag) {
if (preference == kLocalePreferenceAuto) {
return locale_from_system_tag(system_tag);
}
return is_supported_locale(preference) ? preference : kLocaleZhCn;
}
std::string detect_system_locale_tag() {
#ifdef _WIN32
wchar_t value[LOCALE_NAME_MAX_LENGTH] = {};
const int length = ::GetUserDefaultLocaleName(value, LOCALE_NAME_MAX_LENGTH);
if (length > 1) {
std::string out;
out.reserve(static_cast<std::size_t>(length - 1));
for (int i = 0; i < length - 1; ++i) {
const wchar_t ch = value[i];
out.push_back(ch <= 0x7f ? static_cast<char>(ch) : '?');
}
return out;
}
#else
for (const char* name : {"LC_ALL", "LC_MESSAGES", "LANG"}) {
if (const char* value = std::getenv(name); value && *value) {
return value;
}
}
#endif
try {
return std::locale("").name();
} catch (...) {
return {};
}
}
std::string locale_bootstrap_script(const std::string& preference,
const std::string& effective_locale) {
const std::string normalized_preference =
preference == kLocalePreferenceAuto || is_supported_locale(preference)
? preference
: kLocaleZhCn;
const std::string normalized_locale = is_supported_locale(effective_locale)
? effective_locale
: resolve_ui_locale(normalized_preference, {});
return "window.__ACECODE_LOCALE_PREFERENCE__=" +
nlohmann::json(normalized_preference).dump() + ";\n" +
"window.__ACECODE_LOCALE__=" + nlohmann::json(normalized_locale).dump() +
";\n";
}
} // namespace acecode::desktop