Skip to content

Commit

Permalink
refactor: use user set locale first
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Feb 14, 2021
1 parent 6fe2a65 commit 3f6955d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 93 deletions.
110 changes: 36 additions & 74 deletions lib/app.dart
Expand Up @@ -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,
),
);
}
}
1 change: 1 addition & 0 deletions lib/home.dart
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions lib/l10n/intl_en.arb
@@ -1,8 +1,4 @@
{
"helloWorld": "Hello World!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
},
"news": "News",
"@news": {
"description": "The News tab"
Expand Down
4 changes: 0 additions & 4 deletions lib/l10n/intl_es.arb
Expand Up @@ -346,9 +346,5 @@
"news": "Noticias",
"@news": {
"description": "The News tab"
},
"helloWorld": "¡Hola, Mundo!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
}
}
3 changes: 1 addition & 2 deletions lib/l10n/intl_hi.arb
@@ -1,5 +1,4 @@
{
"helloWorld": "नमस्ते दुनिया",
"news": "समाचार",
"notification": "अधिसूचना",
"trending": "ट्रेंडिंग",
Expand Down Expand Up @@ -88,4 +87,4 @@
"fontFamily": "फॉण्ट परिवार",
"fontSize": "फॉण्ट आकार",
"fontStyle": "फॉण्ट प्रकार"
}
}
4 changes: 0 additions & 4 deletions lib/l10n/intl_nb_NO.arb
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions lib/l10n/intl_pt_BR.arb
Expand Up @@ -346,9 +346,5 @@
"news": "Notícias",
"@news": {
"description": "The News tab"
},
"helloWorld": "Olá Mundo!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
}
}
16 changes: 15 additions & 1 deletion lib/models/theme.dart
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +38,7 @@ class SupportedLocales {
'es': 'Español',
'nb_NO': 'Norsk bokmål (Norge) ',
'pt_BR': 'Portugues (brasil)',
'zh_Hans': '中文(简体汉字'
'zh_Hans': '简体中文'
};
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3f6955d

Please sign in to comment.