Skip to content

Commit b338e26

Browse files
committed
Create system_utils and add the desktop implementation
1 parent d83ad12 commit b338e26

File tree

5 files changed

+160
-71
lines changed

5 files changed

+160
-71
lines changed

shell/platform/tizen/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ template("embedder_for_profile") {
131131
"channels/settings_channel.cc",
132132
"external_texture_pixel_gl.cc",
133133
"external_texture_surface_gl.cc",
134+
"system_utils_tizen.cc",
134135
"tizen_log.cc",
135136
]
136137

@@ -246,6 +247,7 @@ template("embedder_executable") {
246247
"channels/settings_channel_stub.cc",
247248
"external_texture_pixel_gl_stub.cc",
248249
"external_texture_surface_gl_stub.cc",
250+
"system_utils_linux.cc",
249251
"tizen_log_stub.cc",
250252
"tizen_renderer_evas_gl.cc",
251253
]

shell/platform/tizen/flutter_tizen_engine.cc

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55

66
#include "flutter_tizen_engine.h"
77

8-
#ifndef __X64_SHELL__
9-
#include <utils_i18n.h>
10-
#endif
11-
128
#include <algorithm>
139
#include <string>
1410
#include <vector>
1511

12+
#include "flutter/shell/platform/tizen/system_utils.h"
1613
#include "flutter/shell/platform/tizen/tizen_log.h"
1714

1815
namespace flutter {
@@ -35,17 +32,9 @@ constexpr double kProfileFactor = 2.0;
3532
constexpr double kProfileFactor = 1.0;
3633
#endif
3734

38-
#ifndef __X64_SHELL__
39-
struct LocaleInfo {
40-
std::string language;
41-
std::string country;
42-
std::string script;
43-
std::string variant;
44-
};
45-
46-
// Converts a LocaleInfo struct to a FlutterLocale struct. |info| must outlive
35+
// Converts a LanguageInfo struct to a FlutterLocale struct. |info| must outlive
4736
// the returned value, since the returned FlutterLocale has pointers into it.
48-
FlutterLocale CovertToFlutterLocale(const LocaleInfo& info) {
37+
FlutterLocale CovertToFlutterLocale(const LanguageInfo& info) {
4938
FlutterLocale locale = {};
5039
locale.struct_size = sizeof(FlutterLocale);
5140
locale.language_code = info.language.c_str();
@@ -60,7 +49,6 @@ FlutterLocale CovertToFlutterLocale(const LocaleInfo& info) {
6049
}
6150
return locale;
6251
}
63-
#endif
6452

6553
} // namespace
6654

@@ -416,64 +404,13 @@ void FlutterTizenEngine::OnVsync(intptr_t baton,
416404
}
417405

418406
void FlutterTizenEngine::SetupLocales() {
419-
#ifndef __X64_SHELL__
420-
i18n_ulocale_set_default(getenv("LANG"));
421-
422-
const char* locale_id;
423-
int ret = i18n_ulocale_get_default(&locale_id);
424-
if (ret != I18N_ERROR_NONE) {
425-
FT_LOGE("i18n_ulocale_get_default() failed.");
426-
return;
427-
}
428-
std::string preferred_locale(locale_id);
429-
preferred_locale = preferred_locale.substr(0, preferred_locale.find("."));
430-
431-
std::vector<LocaleInfo> locales;
432-
int32_t count = i18n_ulocale_count_available();
433-
locales.reserve(count);
434-
for (int i = 0; i < count; i++) {
435-
LocaleInfo locale;
436-
int ret;
437-
char buffer[128] = {0};
438-
int32_t size;
439-
440-
// The "language" field is required.
441-
locale_id = i18n_ulocale_get_available(i);
442-
ret = i18n_ulocale_get_language(locale_id, buffer, sizeof(buffer), &size);
443-
if (ret != I18N_ERROR_NONE || size == 0) {
444-
continue;
445-
}
446-
locale.language = std::string(buffer, size);
447-
448-
// "country", "script", and "variant" are optional.
449-
size = i18n_ulocale_get_country(locale_id, buffer, sizeof(buffer), &ret);
450-
if (ret == I18N_ERROR_NONE && size > 0) {
451-
locale.country = std::string(buffer, size);
452-
}
453-
size = i18n_ulocale_get_script(locale_id, buffer, sizeof(buffer));
454-
if (size > 0) {
455-
locale.script = std::string(buffer, size);
456-
}
457-
size = i18n_ulocale_get_variant(locale_id, buffer, sizeof(buffer));
458-
if (size > 0) {
459-
locale.variant = std::string(buffer, size);
460-
}
461-
462-
if (preferred_locale.compare(locale_id) == 0) {
463-
locales.insert(locales.begin(), locale);
464-
} else {
465-
locales.push_back(locale);
466-
}
467-
}
468-
FT_LOGI("Found %zu locales.", locales.size());
469-
470-
// The locale list should be converted into the FlutterLocale list, and again
471-
// to the FlutterLocale* list.
407+
std::vector<LanguageInfo> languages = GetPreferredLanguageInfo();
472408
std::vector<FlutterLocale> flutter_locales;
473-
flutter_locales.reserve(locales.size());
474-
for (const auto& info : locales) {
409+
flutter_locales.reserve(languages.size());
410+
for (const auto& info : languages) {
475411
flutter_locales.push_back(CovertToFlutterLocale(info));
476412
}
413+
// Convert the locale list to the locale pointer list that must be provided.
477414
std::vector<const FlutterLocale*> flutter_locale_list;
478415
flutter_locale_list.reserve(flutter_locales.size());
479416
std::transform(
@@ -483,7 +420,6 @@ void FlutterTizenEngine::SetupLocales() {
483420

484421
embedder_api_.UpdateLocales(engine_, flutter_locale_list.data(),
485422
flutter_locale_list.size());
486-
#endif
487423
}
488424

489425
void FlutterTizenEngine::NotifyLowMemoryWarning() {

shell/platform/tizen/system_utils.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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+
#ifndef EMBEDDER_SYSTEM_UTILS_H_
6+
#define EMBEDDER_SYSTEM_UTILS_H_
7+
8+
#include <string>
9+
#include <vector>
10+
11+
namespace flutter {
12+
13+
// Components of a system language/locale.
14+
struct LanguageInfo {
15+
std::string language;
16+
std::string country;
17+
std::string script;
18+
std::string variant;
19+
};
20+
21+
// Returns the list of user-preferred locales, in preference order,
22+
// parsed into LanguageInfo structures.
23+
std::vector<LanguageInfo> GetPreferredLanguageInfo();
24+
25+
} // namespace flutter
26+
27+
#endif // EMBEDDER_SYSTEM_UTILS_H_
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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 "system_utils.h"
6+
7+
#include <cstdlib>
8+
9+
namespace flutter {
10+
11+
namespace {
12+
13+
const char* GetLocaleStringFromEnvironment() {
14+
const char* retval;
15+
retval = getenv("LANGUAGE");
16+
if (retval && (retval[0] != '\0')) {
17+
return retval;
18+
}
19+
retval = getenv("LC_ALL");
20+
if (retval && (retval[0] != '\0')) {
21+
return retval;
22+
}
23+
retval = getenv("LC_MESSAGES");
24+
if (retval && (retval[0] != '\0')) {
25+
return retval;
26+
}
27+
retval = getenv("LANG");
28+
if (retval && (retval[0] != '\0')) {
29+
return retval;
30+
}
31+
return "";
32+
}
33+
34+
} // namespace
35+
36+
std::vector<LanguageInfo> GetPreferredLanguageInfo() {
37+
std::vector<LanguageInfo> languages;
38+
39+
std::string locale(GetLocaleStringFromEnvironment());
40+
if (locale.empty()) {
41+
// This is the default locale if none is specified according to ISO C.
42+
locale = "C";
43+
}
44+
LanguageInfo info;
45+
size_t country_pos = locale.find("_");
46+
size_t codeset_pos = locale.find(".");
47+
info.language = locale.substr(0, country_pos);
48+
if (country_pos != std::string::npos) {
49+
info.country = locale.substr(country_pos + 1, codeset_pos);
50+
}
51+
languages.push_back(info);
52+
53+
return languages;
54+
}
55+
56+
} // namespace flutter
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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 "system_utils.h"
6+
7+
#include <utils_i18n.h>
8+
9+
#include "flutter/shell/platform/tizen/tizen_log.h"
10+
11+
namespace flutter {
12+
13+
std::vector<LanguageInfo> GetPreferredLanguageInfo() {
14+
std::vector<LanguageInfo> languages;
15+
const char* locale;
16+
17+
i18n_ulocale_set_default(getenv("LANG"));
18+
int ret = i18n_ulocale_get_default(&locale);
19+
if (ret != I18N_ERROR_NONE) {
20+
FT_LOGE("i18n_ulocale_get_default() failed.");
21+
return languages;
22+
}
23+
std::string preferred_locale(locale);
24+
size_t codeset_pos = preferred_locale.find(".");
25+
preferred_locale = preferred_locale.substr(0, codeset_pos);
26+
27+
int32_t count = i18n_ulocale_count_available();
28+
languages.reserve(count);
29+
for (int i = 0; i < count; i++) {
30+
LanguageInfo info;
31+
int ret;
32+
char buffer[128] = {0};
33+
int32_t size;
34+
35+
// The "language" field is required.
36+
locale = i18n_ulocale_get_available(i);
37+
ret = i18n_ulocale_get_language(locale, buffer, sizeof(buffer), &size);
38+
if (ret != I18N_ERROR_NONE || size == 0) {
39+
continue;
40+
}
41+
info.language = std::string(buffer, size);
42+
43+
// "country", "script", and "variant" are optional.
44+
size = i18n_ulocale_get_country(locale, buffer, sizeof(buffer), &ret);
45+
if (ret == I18N_ERROR_NONE && size > 0) {
46+
info.country = std::string(buffer, size);
47+
}
48+
size = i18n_ulocale_get_script(locale, buffer, sizeof(buffer));
49+
if (size > 0) {
50+
info.script = std::string(buffer, size);
51+
}
52+
size = i18n_ulocale_get_variant(locale, buffer, sizeof(buffer));
53+
if (size > 0) {
54+
info.variant = std::string(buffer, size);
55+
}
56+
57+
if (preferred_locale.compare(locale) == 0) {
58+
languages.insert(languages.begin(), info);
59+
} else {
60+
languages.push_back(info);
61+
}
62+
}
63+
FT_LOGI("Found %zu locales.", languages.size());
64+
65+
return languages;
66+
}
67+
68+
} // namespace flutter

0 commit comments

Comments
 (0)