mirror of
https://github.com/pd4d10/git-touch.git
synced 2026-03-11 17:49:27 -05:00
feat: choose locale in settings (#189)
This commit is contained in:
committed by
GitHub
parent
3b88ad01f7
commit
717b7cd336
12
lib/app.dart
12
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<ThemeModel>(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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,31 @@ class DialogOption<T> {
|
||||
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<String, String> 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<void> 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();
|
||||
}
|
||||
|
||||
@@ -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: [
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
Reference in New Issue
Block a user