-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsn_get_system_language.cc
More file actions
42 lines (38 loc) · 1.13 KB
/
sn_get_system_language.cc
File metadata and controls
42 lines (38 loc) · 1.13 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
#include "sn.hh"
#include <array>
// assumes already lowercase
bool SN::Context::AcceptableLanguage(const std::string& language) {
auto it = langinfo.find(language);
if(it != langinfo.end()) return true;
std::string fallback_code;
if(SN::SimpleFallback(language, fallback_code))
return AcceptableLanguage(fallback_code);
else
return false;
}
static const std::array<const char*, 5> LOCALE_VARS
{{"LANG","LANGSPEC","LANGUAGE","LC_MESSAGES","LC_ALL"}};
std::string SN::Context::GetSystemLanguage(const std::string& default_choice) {
MaybeGetLanguageList();
// TODO: on Windows, use GetUserPreferredUILanguages
for(const char* env : LOCALE_VARS) {
auto val = getenv(env);
if(val) {
std::string code(val);
auto it = code.begin();
while(it != code.end()) {
if(*it == '_') *it++ = '-';
else if(*it == '.') {
code.resize(it - code.begin());
break;
}
else if(*it >= 'A' && *it <= 'Z')
*it++ |= 0x20;
else ++it;
}
if(SN::IsValidLanguageCode(code) && AcceptableLanguage(code))
return code;
}
}
return default_choice;
}