mirror of
https://github.com/git-touch/git-touch
synced 2025-03-12 01:00:16 +01:00
refactor: use user set locale first
This commit is contained in:
parent
6fe2a6541c
commit
3f6955dd7f
110
lib/app.dart
110
lib/app.dart
@ -4,85 +4,47 @@ import 'package:git_touch/home.dart';
|
|||||||
import 'package:git_touch/models/auth.dart';
|
import 'package:git_touch/models/auth.dart';
|
||||||
import 'package:git_touch/models/theme.dart';
|
import 'package:git_touch/models/theme.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
||||||
import 'package:flutter_gen/gen_l10n/S.dart';
|
import 'package:flutter_gen/gen_l10n/S.dart';
|
||||||
import 'package:intl/locale.dart' as l;
|
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final auth = Provider.of<AuthModel>(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,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ class _HomeState extends State<Home> {
|
|||||||
final GlobalKey<NavigatorState> tab5 = GlobalKey<NavigatorState>();
|
final GlobalKey<NavigatorState> tab5 = GlobalKey<NavigatorState>();
|
||||||
|
|
||||||
_buildScreen(int index) {
|
_buildScreen(int index) {
|
||||||
|
// print(Localizations.localeOf(context).toString());
|
||||||
// return GlProjectScreen(32221);
|
// return GlProjectScreen(32221);
|
||||||
// return GhIssuesScreen('flutter', 'flutter', isPullRequest: true);
|
// return GhIssuesScreen('flutter', 'flutter', isPullRequest: true);
|
||||||
// return GhIssueScreen('reactjs', 'rfcs', 29);
|
// return GhIssueScreen('reactjs', 'rfcs', 29);
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
{
|
{
|
||||||
"helloWorld": "Hello World!",
|
|
||||||
"@helloWorld": {
|
|
||||||
"description": "The conventional newborn programmer greeting"
|
|
||||||
},
|
|
||||||
"news": "News",
|
"news": "News",
|
||||||
"@news": {
|
"@news": {
|
||||||
"description": "The News tab"
|
"description": "The News tab"
|
||||||
|
@ -346,9 +346,5 @@
|
|||||||
"news": "Noticias",
|
"news": "Noticias",
|
||||||
"@news": {
|
"@news": {
|
||||||
"description": "The News tab"
|
"description": "The News tab"
|
||||||
},
|
|
||||||
"helloWorld": "¡Hola, Mundo!",
|
|
||||||
"@helloWorld": {
|
|
||||||
"description": "The conventional newborn programmer greeting"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
{
|
{
|
||||||
"helloWorld": "नमस्ते दुनिया",
|
|
||||||
"news": "समाचार",
|
"news": "समाचार",
|
||||||
"notification": "अधिसूचना",
|
"notification": "अधिसूचना",
|
||||||
"trending": "ट्रेंडिंग",
|
"trending": "ट्रेंडिंग",
|
||||||
@ -88,4 +87,4 @@
|
|||||||
"fontFamily": "फॉण्ट परिवार",
|
"fontFamily": "फॉण्ट परिवार",
|
||||||
"fontSize": "फॉण्ट आकार",
|
"fontSize": "फॉण्ट आकार",
|
||||||
"fontStyle": "फॉण्ट प्रकार"
|
"fontStyle": "फॉण्ट प्रकार"
|
||||||
}
|
}
|
||||||
|
@ -71,10 +71,6 @@
|
|||||||
"@follow": {
|
"@follow": {
|
||||||
"description": "follow someone"
|
"description": "follow someone"
|
||||||
},
|
},
|
||||||
"helloWorld": "Hei verden!",
|
|
||||||
"@helloWorld": {
|
|
||||||
"description": "The conventional newborn programmer greeting"
|
|
||||||
},
|
|
||||||
"permissionRequiredMessage": "GitTouch trenger disse tilgangene",
|
"permissionRequiredMessage": "GitTouch trenger disse tilgangene",
|
||||||
"@permissionRequiredMessage": {
|
"@permissionRequiredMessage": {
|
||||||
"description": "Permission Required Message"
|
"description": "Permission Required Message"
|
||||||
|
@ -346,9 +346,5 @@
|
|||||||
"news": "Notícias",
|
"news": "Notícias",
|
||||||
"@news": {
|
"@news": {
|
||||||
"description": "The News tab"
|
"description": "The News tab"
|
||||||
},
|
|
||||||
"helloWorld": "Olá Mundo!",
|
|
||||||
"@helloWorld": {
|
|
||||||
"description": "The conventional newborn programmer greeting"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import 'package:git_touch/widgets/action_button.dart';
|
|||||||
import 'package:primer/primer.dart';
|
import 'package:primer/primer.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:intl/locale.dart' as l;
|
||||||
|
|
||||||
class DialogOption<T> {
|
class DialogOption<T> {
|
||||||
final T value;
|
final T value;
|
||||||
@ -37,7 +38,7 @@ class SupportedLocales {
|
|||||||
'es': 'Español',
|
'es': 'Español',
|
||||||
'nb_NO': 'Norsk bokmål (Norge) ',
|
'nb_NO': 'Norsk bokmål (Norge) ',
|
||||||
'pt_BR': 'Portugues (brasil)',
|
'pt_BR': 'Portugues (brasil)',
|
||||||
'zh_Hans': '中文(简体汉字'
|
'zh_Hans': '简体中文'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,6 +189,19 @@ class ThemeModel with ChangeNotifier {
|
|||||||
// supported languages
|
// supported languages
|
||||||
String _locale;
|
String _locale;
|
||||||
String get locale => _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 {
|
Future<void> setLocale(String v) async {
|
||||||
_locale = v;
|
_locale = v;
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user