34 lines
1.1 KiB
Dart
34 lines
1.1 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
import 'package:intl/intl_standalone.dart';
|
|
import 'package:wonders/common_libs.dart';
|
|
|
|
class LocaleLogic {
|
|
AppLocalizations? _strings;
|
|
AppLocalizations get strings => _strings!;
|
|
|
|
bool get isLoaded => _strings != null;
|
|
|
|
bool get isEnglish => strings.localeName == 'en';
|
|
|
|
Future<void> load() async {
|
|
final localeCode = await findSystemLocale();
|
|
Locale locale = Locale(localeCode.split('_')[0]);
|
|
if (kDebugMode) {
|
|
// Uncomment for testing in chinese
|
|
// locale = Locale('zh');
|
|
}
|
|
if (AppLocalizations.supportedLocales.contains(locale) == false) {
|
|
locale = Locale('en');
|
|
}
|
|
settingsLogic.currentLocale.value = locale.languageCode;
|
|
_strings = await AppLocalizations.delegate.load(locale);
|
|
}
|
|
|
|
Future<void> refreshIfChanged(Locale locale) async {
|
|
if (_strings?.localeName != locale.languageCode && AppLocalizations.supportedLocales.contains(locale)) {
|
|
_strings = await AppLocalizations.delegate.load(locale);
|
|
}
|
|
}
|
|
}
|