Skip to content

Commit

Permalink
fix: set locale and reload
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Feb 14, 2021
1 parent 3f6955d commit d488d4b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
5 changes: 5 additions & 0 deletions lib/models/auth.dart
Expand Up @@ -660,6 +660,11 @@ class AuthModel with ChangeNotifier {
}

var rootKey = UniqueKey();
reloadApp() {
rootKey = UniqueKey();
notifyListeners();
}

setActiveAccountAndReload(int index) async {
// https://stackoverflow.com/a/50116077
rootKey = UniqueKey();
Expand Down
6 changes: 4 additions & 2 deletions lib/models/theme.dart
Expand Up @@ -10,6 +10,7 @@ import 'package:primer/primer.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/services.dart';
import 'package:intl/locale.dart' as l;
import 'package:flutter_gen/gen_l10n/S.dart';

class DialogOption<T> {
final T value;
Expand Down Expand Up @@ -38,7 +39,8 @@ class SupportedLocales {
'es': 'Español',
'nb_NO': 'Norsk bokmål (Norge) ',
'pt_BR': 'Portugues (brasil)',
'zh_Hans': '简体中文'
'zh_Hans': '简体中文',
'zh_Hant': '正体中文'
};
}

Expand Down Expand Up @@ -264,7 +266,7 @@ class ThemeModel with ChangeNotifier {
_markdown = m;
}
final l = prefs.getString(StorageKeys.locale);
if (SupportedLocales.values.contains(l)) {
if (AppLocalizations.supportedLocales.any((v) => l == v.toString())) {
_locale = l;
}

Expand Down
52 changes: 36 additions & 16 deletions lib/screens/settings.dart
Expand Up @@ -21,6 +21,16 @@ class SettingsScreen extends StatelessWidget {
// return Icon(Icons.check, color: theme.palette.primary, size: 24);
// }

// After translation finished, add locale name here to display in settings
static const localeNameMap = {
'en': 'English',
'hi': 'हिन्दी',
'es': 'Español',
'nb_NO': 'Norsk bokmål (Norge)',
'pt_BR': 'Portugues (brasil)',
'zh_Hans': '简体中文',
};

@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
Expand Down Expand Up @@ -74,22 +84,32 @@ class SettingsScreen extends StatelessWidget {
rightWidget: Text(auth.activeAccount.login),
),
TableViewItem(
text: Text('App Language'),
rightWidget: Text(SupportedLocales
.languageNameExpanded[theme.locale ?? 'en']),
onTap: () {
theme.showActions(context, [
for (var t in SupportedLocales.values)
ActionItem(
text: SupportedLocales.languageNameExpanded[t],
onTap: (_) {
if (theme.locale != t) {
theme.setLocale(t);
}
},
)
]);
})
text: Text('App Language'),
rightWidget: Text(theme.locale == null
? AppLocalizations.of(context).followSystem
: localeNameMap[theme.locale] ?? 'Unknown'),
onTap: () {
theme.showActions(context, [
for (final e in [
MapEntry(null, AppLocalizations.of(context).followSystem),
...localeNameMap.entries
])
ActionItem(
text: e.value,
onTap: (_) async {
final res = await theme.showConfirm(
context,
Text('Reload the App to take effect'),
);
if (res && theme.locale != e.key) {
await theme.setLocale(e.key);
auth.reloadApp();
}
},
)
]);
},
)
]),
CommonStyle.verticalGap,
TableView(headerText: 'theme', items: [
Expand Down

0 comments on commit d488d4b

Please sign in to comment.