exit early when finding system locale on web
This commit is contained in:
parent
1e1cac7c54
commit
d30e2fc99c
@ -4,23 +4,27 @@ import 'package:intl/intl_standalone.dart';
|
|||||||
import 'package:wonders/common_libs.dart';
|
import 'package:wonders/common_libs.dart';
|
||||||
|
|
||||||
class LocaleLogic {
|
class LocaleLogic {
|
||||||
|
final Locale _defaultLocal = Locale('en');
|
||||||
|
|
||||||
AppLocalizations? _strings;
|
AppLocalizations? _strings;
|
||||||
AppLocalizations get strings => _strings!;
|
AppLocalizations get strings => _strings!;
|
||||||
|
|
||||||
bool get isLoaded => _strings != null;
|
bool get isLoaded => _strings != null;
|
||||||
|
|
||||||
bool get isEnglish => strings.localeName == 'en';
|
bool get isEnglish => strings.localeName == 'en';
|
||||||
|
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
final localeCode = settingsLogic.currentLocale.value ?? await findSystemLocale();
|
Locale locale = _defaultLocal;
|
||||||
Locale locale = Locale(localeCode.split('_')[0]);
|
if (kIsWeb) return; // exit early on web as [findSystemLocale] throws errors as of Dec, 2022
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
// Uncomment for testing in chinese
|
// locale = Locale('zh'); // uncomment to test chinese
|
||||||
// locale = Locale('zh');
|
|
||||||
}
|
}
|
||||||
|
final localeCode = settingsLogic.currentLocale.value ?? await findSystemLocale();
|
||||||
|
// Try and find a supported locale
|
||||||
|
locale = Locale(localeCode.split('_')[0]);
|
||||||
|
// Fall back to default
|
||||||
if (AppLocalizations.supportedLocales.contains(locale) == false) {
|
if (AppLocalizations.supportedLocales.contains(locale) == false) {
|
||||||
locale = Locale('en');
|
locale = _defaultLocal;
|
||||||
}
|
}
|
||||||
|
|
||||||
settingsLogic.currentLocale.value = locale.languageCode;
|
settingsLogic.currentLocale.value = locale.languageCode;
|
||||||
_strings = await AppLocalizations.delegate.load(locale);
|
_strings = await AppLocalizations.delegate.load(locale);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user