From 9ff5e56e1fe3a64d6a568cfeaac4b8e1091ec18d Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Mon, 24 Apr 2023 13:12:38 +0200 Subject: [PATCH] neon: fix unsupported locale Signed-off-by: Nikolas Rimikis --- .../neon/neon/lib/src/utils/localizations.dart | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/neon/neon/lib/src/utils/localizations.dart b/packages/neon/neon/lib/src/utils/localizations.dart index e3b95c84..3f04cad6 100644 --- a/packages/neon/neon/lib/src/utils/localizations.dart +++ b/packages/neon/neon/lib/src/utils/localizations.dart @@ -1,7 +1,14 @@ part of '../../neon.dart'; -Future appLocalizationsFromSystem() async { - final parts = - (await findSystemLocale()).split('_').map((final a) => a.split('.')).reduce((final a, final b) => [...a, ...b]); - return AppLocalizations.delegate.load(Locale(parts[0], parts.length > 1 ? parts[1] : null)); +/// Loads the [AppLocalizations] for the system [Locale]. +/// +/// When the system locale is not supported [fallbackLocale] will be used. +Future appLocalizationsFromSystem([final Locale fallbackLocale = const Locale('en', 'US')]) async { + final systemLocale = await findSystemLocale(); + final parts = systemLocale.split('_').map((final a) => a.split('.')).reduce((final a, final b) => [...a, ...b]); + final locale = Locale(parts[0], parts.length > 1 ? parts[1] : null); + + final isSupported = AppLocalizations.delegate.isSupported(locale); + + return AppLocalizations.delegate.load(isSupported ? locale : fallbackLocale); }