Flutter Engine
system_utils_win32.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/windows/system_utils.h"
6 
7 #include <Windows.h>
8 
9 #include <sstream>
10 
11 #include "flutter/shell/platform/windows/string_conversion.h"
12 
13 namespace flutter {
14 
15 std::vector<LanguageInfo> GetPreferredLanguageInfo() {
16  std::vector<std::wstring> languages = GetPreferredLanguages();
17  std::vector<LanguageInfo> language_info;
18  language_info.reserve(languages.size());
19 
20  for (auto language : languages) {
21  language_info.push_back(ParseLanguageName(language));
22  }
23  return language_info;
24 }
25 
26 std::vector<std::wstring> GetPreferredLanguages() {
27  std::vector<std::wstring> languages;
28  DWORD flags = MUI_LANGUAGE_NAME | MUI_UI_FALLBACK;
29 
30  // Get buffer length.
31  ULONG count = 0;
32  ULONG buffer_size = 0;
33  if (!::GetThreadPreferredUILanguages(flags, &count, nullptr, &buffer_size)) {
34  return languages;
35  }
36 
37  // Get the list of null-separated languages.
38  std::wstring buffer(buffer_size, '\0');
39  if (!::GetThreadPreferredUILanguages(flags, &count, buffer.data(),
40  &buffer_size)) {
41  return languages;
42  }
43 
44  // Extract the individual languages from the buffer.
45  size_t start = 0;
46  while (true) {
47  // The buffer is terminated by an empty string (i.e., a double null).
48  if (buffer[start] == L'\0') {
49  break;
50  }
51  // Read the next null-terminated language.
52  std::wstring language(buffer.c_str() + start);
53  if (language.size() == 0) {
54  break;
55  }
56  languages.push_back(language);
57  // Skip past that language and its terminating null in the buffer.
58  start += language.size() + 1;
59  }
60  return languages;
61 }
62 
63 LanguageInfo ParseLanguageName(std::wstring language_name) {
64  LanguageInfo info;
65 
66  // Split by '-', discarding any suplemental language info (-x-foo).
67  std::vector<std::string> components;
68  std::istringstream stream(Utf8FromUtf16(language_name));
69  std::string component;
70  while (getline(stream, component, '-')) {
71  if (component == "x") {
72  break;
73  }
74  components.push_back(component);
75  }
76 
77  // Determine which components are which.
78  info.language = components[0];
79  if (components.size() == 3) {
80  info.script = components[1];
81  info.region = components[2];
82  } else if (components.size() == 2) {
83  // A script code will always be four characters long.
84  if (components[1].size() == 4) {
85  info.script = components[1];
86  } else {
87  info.region = components[1];
88  }
89  }
90  return info;
91 }
92 
93 } // namespace flutter
std::string language
Definition: system_utils.h:17
std::vector< std::wstring > GetPreferredLanguages()
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
std::string Utf8FromUtf16(const std::wstring &utf16_string)
LanguageInfo ParseLanguageName(std::wstring language_name)
std::vector< LanguageInfo > GetPreferredLanguageInfo()
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol Prefix for the symbols representing ICU data linked into the Flutter library dart flags
Definition: switches.h:66