Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
system_utils.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/platform/glfw/system_utils.h"
6
7#include <cstdlib>
8#include <sstream>
9
10namespace flutter {
11
12namespace {
13
14const char* GetLocaleStringFromEnvironment() {
15 const char* retval;
16 retval = getenv("LANGUAGE");
17 if ((retval != NULL) && (retval[0] != '\0')) {
18 return retval;
19 }
20 retval = getenv("LC_ALL");
21 if ((retval != NULL) && (retval[0] != '\0')) {
22 return retval;
23 }
24 retval = getenv("LC_MESSAGES");
25 if ((retval != NULL) && (retval[0] != '\0')) {
26 return retval;
27 }
28 retval = getenv("LANG");
29 if ((retval != NULL) && (retval[0] != '\0')) {
30 return retval;
31 }
32
33 return NULL;
34}
35
36// The least specific to most specific components of a locale.
37enum Component {
38 kCodeset = 1 << 0,
39 kTerritory = 1 << 1,
40 kModifier = 1 << 2,
41};
42
43// Construct a mask indicating which of the components in |info| are set.
44int ComputeVariantMask(const LanguageInfo& info) {
45 int mask = 0;
46 if (!info.territory.empty()) {
47 mask |= kTerritory;
48 }
49 if (!info.codeset.empty()) {
50 mask |= kCodeset;
51 }
52 if (!info.modifier.empty()) {
53 mask |= kModifier;
54 }
55 return mask;
56}
57
58// Appends most specific to least specific variants of |info| to |languages|.
59// For example, "de_DE@euro" would append "de_DE@euro", "de@euro", "de_DE",
60// and "de".
61void AppendLocaleVariants(std::vector<LanguageInfo>& languages,
62 const LanguageInfo& info) {
63 int mask = ComputeVariantMask(info);
64 for (int i = mask; i >= 0; --i) {
65 if ((i & ~mask) == 0) {
66 LanguageInfo variant;
67 variant.language = info.language;
68
69 if (i & kTerritory) {
70 variant.territory = info.territory;
71 }
72 if (i & kCodeset) {
73 variant.codeset = info.codeset;
74 }
75 if (i & kModifier) {
76 variant.modifier = info.modifier;
77 }
78 languages.push_back(variant);
79 }
80 }
81}
82
83// Parses a locale into its components.
84LanguageInfo ParseLocale(const std::string& locale) {
85 // Locales are of the form "language[_territory][.codeset][@modifier]"
86 LanguageInfo result;
87 std::string::size_type end = locale.size();
88 std::string::size_type modifier_pos = locale.rfind('@');
89 if (modifier_pos != std::string::npos) {
90 result.modifier = locale.substr(modifier_pos + 1, end - modifier_pos - 1);
91 end = modifier_pos;
92 }
93
94 std::string::size_type codeset_pos = locale.rfind('.', end);
95 if (codeset_pos != std::string::npos) {
96 result.codeset = locale.substr(codeset_pos + 1, end - codeset_pos - 1);
97 end = codeset_pos;
98 }
99
100 std::string::size_type territory_pos = locale.rfind('_', end);
101 if (territory_pos != std::string::npos) {
102 result.territory =
103 locale.substr(territory_pos + 1, end - territory_pos - 1);
104 end = territory_pos;
105 }
106
107 result.language = locale.substr(0, end);
108
109 return result;
110}
111
112} // namespace
113
114std::vector<LanguageInfo> GetPreferredLanguageInfo() {
115 const char* locale_string;
116 locale_string = GetLocaleStringFromEnvironment();
117 if (!locale_string || locale_string[0] == '\0') {
118 // This is the default locale if none is specified according to ISO C.
119 locale_string = "C";
120 }
121 std::istringstream locales_stream(locale_string);
122 std::vector<LanguageInfo> languages;
123 std::string s;
124 while (getline(locales_stream, s, ':')) {
125 LanguageInfo info = ParseLocale(s);
126 AppendLocaleVariants(languages, info);
127 }
128 return languages;
129}
130
131std::vector<FlutterLocale> ConvertToFlutterLocale(
132 const std::vector<LanguageInfo>& languages) {
133 std::vector<FlutterLocale> flutter_locales;
134 flutter_locales.reserve(languages.size());
135 for (const auto& info : languages) {
136 FlutterLocale locale = {};
137 locale.struct_size = sizeof(FlutterLocale);
138 locale.language_code = info.language.c_str();
139 if (!info.territory.empty()) {
140 locale.country_code = info.territory.c_str();
141 }
142 if (!info.codeset.empty()) {
143 locale.script_code = info.codeset.c_str();
144 }
145 if (!info.modifier.empty()) {
146 locale.variant_code = info.modifier.c_str();
147 }
148 flutter_locales.push_back(locale);
149 }
150
151 return flutter_locales;
152}
153
154} // namespace flutter
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
struct MyStruct s
glong glong end
GAsyncResult * result
std::vector< FlutterLocale > ConvertToFlutterLocale(const std::vector< LanguageInfo > &languages)
std::vector< LanguageInfo > GetPreferredLanguageInfo()
const char * language_code
Definition embedder.h:1939
size_t struct_size
This size of this struct. Must be sizeof(FlutterLocale).
Definition embedder.h:1935
const char * script_code
Definition embedder.h:1949
const char * country_code
Definition embedder.h:1944
const char * variant_code
Definition embedder.h:1954