5#include "flutter/shell/platform/glfw/system_utils.h"
14const char* GetLocaleStringFromEnvironment() {
16 retval =
getenv(
"LANGUAGE");
17 if ((retval != NULL) && (retval[0] !=
'\0')) {
21 if ((retval != NULL) && (retval[0] !=
'\0')) {
24 retval =
getenv(
"LC_MESSAGES");
25 if ((retval != NULL) && (retval[0] !=
'\0')) {
29 if ((retval != NULL) && (retval[0] !=
'\0')) {
44int ComputeVariantMask(
const LanguageInfo&
info) {
46 if (!
info.territory.empty()) {
49 if (!
info.codeset.empty()) {
52 if (!
info.modifier.empty()) {
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) {
67 variant.language =
info.language;
70 variant.territory =
info.territory;
73 variant.codeset =
info.codeset;
76 variant.modifier =
info.modifier;
78 languages.push_back(variant);
84LanguageInfo ParseLocale(
const std::string& locale) {
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);
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);
100 std::string::size_type territory_pos = locale.rfind(
'_', end);
101 if (territory_pos != std::string::npos) {
103 locale.substr(territory_pos + 1, end - territory_pos - 1);
107 result.language = locale.substr(0, end);
115 const char* locale_string;
116 locale_string = GetLocaleStringFromEnvironment();
117 if (!locale_string || locale_string[0] ==
'\0') {
121 std::istringstream locales_stream(locale_string);
122 std::vector<LanguageInfo> languages;
124 while (getline(locales_stream,
s,
':')) {
126 AppendLocaleVariants(languages,
info);
132 const std::vector<LanguageInfo>& languages) {
133 std::vector<FlutterLocale> flutter_locales;
134 flutter_locales.reserve(languages.size());
135 for (
const auto&
info : languages) {
139 if (!
info.territory.empty()) {
142 if (!
info.codeset.empty()) {
145 if (!
info.modifier.empty()) {
148 flutter_locales.push_back(locale);
151 return flutter_locales;
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
std::vector< FlutterLocale > ConvertToFlutterLocale(const std::vector< LanguageInfo > &languages)
std::vector< LanguageInfo > GetPreferredLanguageInfo()
const char * language_code
size_t struct_size
This size of this struct. Must be sizeof(FlutterLocale).
const char * country_code
const char * variant_code