refactor: use user set locale first

This commit is contained in:
Rongjian Zhang 2021-02-14 13:01:09 +08:00
parent 6fe2a6541c
commit 3f6955dd7f
8 changed files with 53 additions and 93 deletions

View File

@ -4,85 +4,47 @@ import 'package:git_touch/home.dart';
import 'package:git_touch/models/auth.dart';
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 = [
const Locale('en'),
const Locale('es'),
const Locale('hi'),
const Locale('nb', 'NO'),
const Locale('pt', 'BR'),
const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'),
];
static Locale localeResolutionCallback(
Locale locale, Iterable<Locale> supportedLocales) {
for (final supportedLocale in supportedLocales) {
if (locale.languageCode == supportedLocale.languageCode) {
return supportedLocale;
}
}
return supportedLocales.first;
}
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(
theme: CupertinoThemeData(brightness: theme.brightness),
home: Home(),
localeResolutionCallback: localeResolutionCallback,
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
locale: Locale.fromSubtags(
languageCode: parsedLocale.languageCode,
countryCode: parsedLocale.countryCode,
scriptCode: parsedLocale.scriptCode),
);
default:
return MaterialApp(
theme: ThemeData(
brightness: theme.brightness,
primaryColor:
theme.brightness == Brightness.dark ? null : Colors.white,
accentColor: theme.palette.primary,
scaffoldBackgroundColor: theme.palette.background,
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: ZoomPageTransitionsBuilder(),
},
),
),
home: Home(),
localeResolutionCallback: localeResolutionCallback,
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: supportedLocales,
locale: Locale.fromSubtags(
languageCode: parsedLocale.languageCode,
countryCode: parsedLocale.countryCode,
scriptCode: parsedLocale.scriptCode),
);
}
}
@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
return Container(key: auth.rootKey, child: _buildChild(context));
final theme = Provider.of<ThemeModel>(context);
final LocaleListResolutionCallback localeListResolutionCallback =
(locales, supportedLocales) {
return theme.userSetLocale ?? locales.first;
};
return Container(
key: auth.rootKey,
child: theme.theme == AppThemeType.cupertino
? CupertinoApp(
theme: CupertinoThemeData(brightness: theme.brightness),
home: Home(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
localeListResolutionCallback: localeListResolutionCallback,
)
: MaterialApp(
theme: ThemeData(
brightness: theme.brightness,
primaryColor:
theme.brightness == Brightness.dark ? null : Colors.white,
accentColor: theme.palette.primary,
scaffoldBackgroundColor: theme.palette.background,
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: ZoomPageTransitionsBuilder(),
},
),
),
home: Home(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
localeListResolutionCallback: localeListResolutionCallback,
),
);
}
}

View File

@ -40,6 +40,7 @@ class _HomeState extends State<Home> {
final GlobalKey<NavigatorState> tab5 = GlobalKey<NavigatorState>();
_buildScreen(int index) {
// print(Localizations.localeOf(context).toString());
// return GlProjectScreen(32221);
// return GhIssuesScreen('flutter', 'flutter', isPullRequest: true);
// return GhIssueScreen('reactjs', 'rfcs', 29);

View File

@ -1,8 +1,4 @@
{
"helloWorld": "Hello World!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
},
"news": "News",
"@news": {
"description": "The News tab"

View File

@ -346,9 +346,5 @@
"news": "Noticias",
"@news": {
"description": "The News tab"
},
"helloWorld": "¡Hola, Mundo!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
}
}

View File

@ -1,5 +1,4 @@
{
"helloWorld": "नमस्ते दुनिया",
"news": "समाचार",
"notification": "अधिसूचना",
"trending": "ट्रेंडिंग",
@ -88,4 +87,4 @@
"fontFamily": "फॉण्ट परिवार",
"fontSize": "फॉण्ट आकार",
"fontStyle": "फॉण्ट प्रकार"
}
}

View File

@ -71,10 +71,6 @@
"@follow": {
"description": "follow someone"
},
"helloWorld": "Hei verden!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
},
"permissionRequiredMessage": "GitTouch trenger disse tilgangene",
"@permissionRequiredMessage": {
"description": "Permission Required Message"

View File

@ -346,9 +346,5 @@
"news": "Notícias",
"@news": {
"description": "The News tab"
},
"helloWorld": "Olá Mundo!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
}
}

View File

@ -9,6 +9,7 @@ import 'package:git_touch/widgets/action_button.dart';
import 'package:primer/primer.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/services.dart';
import 'package:intl/locale.dart' as l;
class DialogOption<T> {
final T value;
@ -37,7 +38,7 @@ class SupportedLocales {
'es': 'Español',
'nb_NO': 'Norsk bokmål (Norge) ',
'pt_BR': 'Portugues (brasil)',
'zh_Hans': '中文(简体汉字'
'zh_Hans': '简体中文'
};
}
@ -188,6 +189,19 @@ class ThemeModel with ChangeNotifier {
// supported languages
String _locale;
String get locale => _locale;
Locale get userSetLocale {
try {
final parsedLocale = l.Locale.parse(_locale);
return Locale.fromSubtags(
languageCode: parsedLocale.languageCode,
countryCode: parsedLocale.countryCode,
scriptCode: parsedLocale.scriptCode,
);
} catch (err) {
return null;
}
}
Future<void> setLocale(String v) async {
_locale = v;
final prefs = await SharedPreferences.getInstance();