fix: fallback to supported locale if easy_localization doesn't support device locale

This commit is contained in:
appflowy 2022-02-05 21:50:49 +08:00
parent 0d47397851
commit e9ba1e1886
2 changed files with 14 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import 'package:app_flowy/user/infrastructure/repos/user_setting_repo.dart';
import 'package:equatable/equatable.dart';
import 'package:flowy_infra/theme.dart';
import 'package:flowy_log/flowy_log.dart';
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_setting.pb.dart';
import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';
@ -46,6 +47,12 @@ class AppearanceSettingModel extends ChangeNotifier with EquatableMixin {
void setLocale(BuildContext context, Locale newLocale) {
if (_locale != newLocale) {
if (context.supportedLocales.contains(newLocale)) {
Log.error("Unsupported locale: $newLocale");
newLocale = const Locale('en');
Log.debug("Fall back to locale: $newLocale");
}
context.setLocale(newLocale);
_locale = newLocale;
setting.locale.languageCode = _locale.languageCode;

View File

@ -59,7 +59,13 @@ pub async fn get_appearance_setting() -> DataResult<AppearanceSettings, FlowyErr
match KV::get_str(APPEARANCE_SETTING_CACHE_KEY) {
None => data_result(AppearanceSettings::default()),
Some(s) => {
let setting: AppearanceSettings = serde_json::from_str(&s)?;
let setting = match serde_json::from_str(&s) {
Ok(setting) => setting,
Err(e) => {
tracing::error!("Deserialize AppearanceSettings failed: {:?}, fallback to default", e);
AppearanceSettings::default()
}
};
data_result(setting)
}
}