master
cpp 70 lines 1.38 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 Localization.cpp
8
9 Abstract:
10
11 This file contains the Linux localization logic.
12
13 --*/
14
15 #include "Localization.h"
16
17 std::string FormatLanguage(const char* Input)
18 {
19 // Expected format: en_US.UTF-8
20
21 std::string output(Input);
22 auto dot = output.find(".");
23
24 if (dot != std::string::npos && dot != 0)
25 {
26 output = output.substr(0, dot);
27 }
28
29 std::replace(output.begin(), output.end(), '_', '-');
30
31 return output;
32 }
33
34 std::optional<std::string> GetUserLanguage()
35 {
36 for (const auto* e : {"LANGUAGE", "LANG", "LC_ALL"})
37 {
38 if (const auto* lang = getenv(e))
39 {
40 return lang;
41 }
42 }
43
44 return {};
45 }
46
47 const char* wsl::shared::Localization::LookupString(const std::vector<std::pair<std::string, const char*>>& strings, Options options)
48 {
49 try
50 {
51 static const auto language = GetUserLanguage();
52 if (language.has_value())
53 {
54 for (const auto& e : strings)
55 {
56 if (wsl::shared::string::IsEqual(e.first, language.value(), true))
57 {
58 return e.second;
59 }
60 }
61 }
62 }
63 catch (...)
64 {
65 LOG_CAUGHT_EXCEPTION();
66 }
67
68 // Default to English is string is not found (English is always the first entry)
69 return strings[0].second;
70 }