From 717b7cd3369e8fddca0699b5732726cde37066d7 Mon Sep 17 00:00:00 2001 From: Shreyas Thirumalai Date: Sun, 14 Feb 2021 09:35:41 +0530 Subject: [PATCH] feat: choose locale in settings (#189) --- lib/app.dart | 12 +++++++++++- lib/models/theme.dart | 39 +++++++++++++++++++++++++++++++++++++++ lib/screens/settings.dart | 17 +++++++++++++++++ lib/utils/utils.dart | 1 + 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/app.dart b/lib/app.dart index cfb390cb..02f50166 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -6,6 +6,7 @@ import 'package:git_touch/models/theme.dart'; import 'package:provider/provider.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_gen/gen_l10n/S.dart'; +import 'package:intl/locale.dart' as l; class MyApp extends StatelessWidget { static const supportedLocales = [ @@ -14,7 +15,7 @@ class MyApp extends StatelessWidget { const Locale('hi'), const Locale('nb', 'NO'), const Locale('pt', 'BR'), - const Locale('zh'), + const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), ]; static Locale localeResolutionCallback( @@ -29,6 +30,7 @@ class MyApp extends StatelessWidget { Widget _buildChild(BuildContext context) { final theme = Provider.of(context); + final parsedLocale = l.Locale.parse(theme.locale ?? 'en'); switch (theme.theme) { case AppThemeType.cupertino: return CupertinoApp( @@ -42,6 +44,10 @@ class MyApp extends StatelessWidget { GlobalCupertinoLocalizations.delegate, ], supportedLocales: supportedLocales, + locale: Locale.fromSubtags( + languageCode: parsedLocale.languageCode, + countryCode: parsedLocale.countryCode, + scriptCode: parsedLocale.scriptCode), ); default: return MaterialApp( @@ -66,6 +72,10 @@ class MyApp extends StatelessWidget { GlobalCupertinoLocalizations.delegate, ], supportedLocales: supportedLocales, + locale: Locale.fromSubtags( + languageCode: parsedLocale.languageCode, + countryCode: parsedLocale.countryCode, + scriptCode: parsedLocale.scriptCode), ); } } diff --git a/lib/models/theme.dart b/lib/models/theme.dart index eec9690e..f67f3ade 100644 --- a/lib/models/theme.dart +++ b/lib/models/theme.dart @@ -16,6 +16,31 @@ class DialogOption { DialogOption({this.value, this.widget}); } +class SupportedLocales { + static const en = 'en'; + static const hi = 'hi'; + static const es = 'es'; + static const nb_NO = 'nb_NO'; + static const pt_BR = 'pt_BR'; + static const zh_Hans = 'zh_Hans'; + static const values = [ + SupportedLocales.en, + SupportedLocales.hi, + SupportedLocales.es, + SupportedLocales.nb_NO, + SupportedLocales.pt_BR, + SupportedLocales.zh_Hans, + ]; + static const Map languageNameExpanded = { + 'en': 'English', + 'hi': 'हिन्दी', + 'es': 'Español', + 'nb_NO': 'Norsk bokmål (Norge) ', + 'pt_BR': 'Portugues (brasil)', + 'zh_Hans': '中文(简体汉字' + }; +} + class AppThemeType { static const material = 0; static const cupertino = 1; @@ -160,6 +185,16 @@ class ThemeModel with ChangeNotifier { return Platform.isMacOS || markdown == AppMarkdownType.flutter; } + // supported languages + String _locale; + String get locale => _locale; + Future setLocale(String v) async { + _locale = v; + final prefs = await SharedPreferences.getInstance(); + await prefs.setString(StorageKeys.locale, v); + notifyListeners(); + } + final router = FluroRouter(); final paletteLight = Palette( @@ -214,6 +249,10 @@ class ThemeModel with ChangeNotifier { if (AppMarkdownType.values.contains(m)) { _markdown = m; } + final l = prefs.getString(StorageKeys.locale); + if (SupportedLocales.values.contains(l)) { + _locale = l; + } notifyListeners(); } diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index a4169b4c..3d86656a 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -73,6 +73,23 @@ class SettingsScreen extends StatelessWidget { url: '/login', 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); + } + }, + ) + ]); + }) ]), CommonStyle.verticalGap, TableView(headerText: 'theme', items: [ diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 4f69e776..f07c67b5 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -27,6 +27,7 @@ class StorageKeys { static const iCodeFontSize = 'code-font-size'; static const codeFontFamily = 'code-font-family'; static const markdown = 'markdown'; + static const locale = 'locale'; static const defaultAccount = 'default-account'; static getDefaultStartTabKey(String platform) =>