Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions shell/platform/linux/fl_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,42 @@ typedef struct {
FlutterTask task;
} FlutterSource;

// Parse a locale into its components.
static void parse_locale(const gchar* locale,
gchar** language,
gchar** territory,
gchar** codeset,
gchar** modifier) {
gchar* l = g_strdup(locale);

// Locales are in the form "language[_territory][.codeset][@modifier]"
gchar* match = strrchr(l, '@');
if (match != nullptr) {
*modifier = g_strdup(match + 1);
*match = '\0';
} else {
*modifier = nullptr;
}

match = strrchr(l, '.');
if (match != nullptr) {
*codeset = g_strdup(match + 1);
*match = '\0';
} else {
*codeset = nullptr;
}

match = strrchr(l, '_');
if (match != nullptr) {
*territory = g_strdup(match + 1);
*match = '\0';
} else {
*territory = nullptr;
}

*language = l;
}

// Callback to run a Flutter task in the GLib main loop.
static gboolean flutter_source_dispatch(GSource* source,
GSourceFunc callback,
Expand Down Expand Up @@ -323,6 +359,35 @@ gboolean fl_engine_start(FlEngine* self, GError** error) {
return FALSE;
}

const gchar* const* languages = g_get_language_names();
g_autoptr(GPtrArray) locales = g_ptr_array_new_with_free_func(g_free);
g_autoptr(GPtrArray) locale_strings = g_ptr_array_new_with_free_func(g_free);
Comment thread
robert-ancell marked this conversation as resolved.
Outdated
for (int i = 0; languages[i] != nullptr; i++) {
gchar *language, *territory, *codeset, *modifier;
parse_locale(languages[i], &language, &territory, &codeset, &modifier);
if (language != nullptr)
g_ptr_array_add(locale_strings, language);
if (territory != nullptr)
g_ptr_array_add(locale_strings, territory);
if (codeset != nullptr)
g_ptr_array_add(locale_strings, codeset);
if (modifier != nullptr)
g_ptr_array_add(locale_strings, modifier);

FlutterLocale* locale =
static_cast<FlutterLocale*>(g_malloc0(sizeof(FlutterLocale)));
g_ptr_array_add(locales, locale);
locale->struct_size = sizeof(FlutterLocale);
locale->language_code = language;
locale->country_code = territory;
locale->script_code = codeset;
locale->variant_code = modifier;
}
result = FlutterEngineUpdateLocales(
self->engine, (const FlutterLocale**)locales->pdata, locales->len);
if (result != kSuccess)
g_warning("Failed to set locale");
Comment thread
robert-ancell marked this conversation as resolved.
Outdated

return TRUE;
}

Expand Down
7 changes: 7 additions & 0 deletions shell/platform/linux/testing/mock_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,10 @@ FlutterEngineResult FlutterEngineRunTask(FLUTTER_API_SYMBOL(FlutterEngine)
bool FlutterEngineRunsAOTCompiledDartCode() {
return false;
}

FlutterEngineResult FlutterEngineUpdateLocales(FLUTTER_API_SYMBOL(FlutterEngine)
engine,
const FlutterLocale** locales,
size_t locales_count) {
return kSuccess;
}