forked from microsoft/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalization.cpp
More file actions
70 lines (54 loc) · 1.38 KB
/
Localization.cpp
File metadata and controls
70 lines (54 loc) · 1.38 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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
Localization.cpp
Abstract:
This file contains the Linux localization logic.
--*/
#include "Localization.h"
std::string FormatLanguage(const char* Input)
{
// Expected format: en_US.UTF-8
std::string output(Input);
auto dot = output.find(".");
if (dot != std::string::npos && dot != 0)
{
output = output.substr(0, dot);
}
std::replace(output.begin(), output.end(), '_', '-');
return output;
}
std::optional<std::string> GetUserLanguage()
{
for (const auto* e : {"LANGUAGE", "LANG", "LC_ALL"})
{
if (const auto* lang = getenv(e))
{
return lang;
}
}
return {};
}
const char* wsl::shared::Localization::LookupString(const std::vector<std::pair<std::string, const char*>>& strings, Options options)
{
try
{
static const auto language = GetUserLanguage();
if (language.has_value())
{
for (const auto& e : strings)
{
if (wsl::shared::string::IsEqual(e.first, language.value(), true))
{
return e.second;
}
}
}
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
}
// Default to English is string is not found (English is always the first entry)
return strings[0].second;
}