Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 160b268

Browse files
Set locale in Linux shell (#19470)
1 parent f10f172 commit 160b268

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

shell/platform/linux/fl_engine.cc

+71
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,75 @@ typedef struct {
5555
FlutterTask task;
5656
} FlutterSource;
5757

58+
// Parse a locale into its components.
59+
static void parse_locale(const gchar* locale,
60+
gchar** language,
61+
gchar** territory,
62+
gchar** codeset,
63+
gchar** modifier) {
64+
gchar* l = g_strdup(locale);
65+
66+
// Locales are in the form "language[_territory][.codeset][@modifier]"
67+
gchar* match = strrchr(l, '@');
68+
if (match != nullptr) {
69+
*modifier = g_strdup(match + 1);
70+
*match = '\0';
71+
} else {
72+
*modifier = nullptr;
73+
}
74+
75+
match = strrchr(l, '.');
76+
if (match != nullptr) {
77+
*codeset = g_strdup(match + 1);
78+
*match = '\0';
79+
} else {
80+
*codeset = nullptr;
81+
}
82+
83+
match = strrchr(l, '_');
84+
if (match != nullptr) {
85+
*territory = g_strdup(match + 1);
86+
*match = '\0';
87+
} else {
88+
*territory = nullptr;
89+
}
90+
91+
*language = l;
92+
}
93+
94+
// Passes locale information to the Flutter engine.
95+
static void setup_locales(FlEngine* self) {
96+
const gchar* const* languages = g_get_language_names();
97+
g_autoptr(GPtrArray) locales = g_ptr_array_new_with_free_func(g_free);
98+
// Helper array to take ownership of the strings passed to Flutter.
99+
g_autoptr(GPtrArray) locale_strings = g_ptr_array_new_with_free_func(g_free);
100+
for (int i = 0; languages[i] != nullptr; i++) {
101+
gchar *language, *territory, *codeset, *modifier;
102+
parse_locale(languages[i], &language, &territory, &codeset, &modifier);
103+
if (language != nullptr)
104+
g_ptr_array_add(locale_strings, language);
105+
if (territory != nullptr)
106+
g_ptr_array_add(locale_strings, territory);
107+
if (codeset != nullptr)
108+
g_ptr_array_add(locale_strings, codeset);
109+
if (modifier != nullptr)
110+
g_ptr_array_add(locale_strings, modifier);
111+
112+
FlutterLocale* locale =
113+
static_cast<FlutterLocale*>(g_malloc0(sizeof(FlutterLocale)));
114+
g_ptr_array_add(locales, locale);
115+
locale->struct_size = sizeof(FlutterLocale);
116+
locale->language_code = language;
117+
locale->country_code = territory;
118+
locale->script_code = codeset;
119+
locale->variant_code = modifier;
120+
}
121+
FlutterEngineResult result = FlutterEngineUpdateLocales(
122+
self->engine, (const FlutterLocale**)locales->pdata, locales->len);
123+
if (result != kSuccess)
124+
g_warning("Failed to set up Flutter locales");
125+
}
126+
58127
// Callback to run a Flutter task in the GLib main loop.
59128
static gboolean flutter_source_dispatch(GSource* source,
60129
GSourceFunc callback,
@@ -323,6 +392,8 @@ gboolean fl_engine_start(FlEngine* self, GError** error) {
323392
return FALSE;
324393
}
325394

395+
setup_locales(self);
396+
326397
return TRUE;
327398
}
328399

shell/platform/linux/testing/mock_engine.cc

+7
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,10 @@ FlutterEngineResult FlutterEngineRunTask(FLUTTER_API_SYMBOL(FlutterEngine)
382382
bool FlutterEngineRunsAOTCompiledDartCode() {
383383
return false;
384384
}
385+
386+
FlutterEngineResult FlutterEngineUpdateLocales(FLUTTER_API_SYMBOL(FlutterEngine)
387+
engine,
388+
const FlutterLocale** locales,
389+
size_t locales_count) {
390+
return kSuccess;
391+
}

0 commit comments

Comments
 (0)