diff --git a/lib/app.dart b/lib/app.dart index 02f50166..e85ab4bc 100644 --- a/lib/app.dart +++ b/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/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 supportedLocales) { - for (final supportedLocale in supportedLocales) { - if (locale.languageCode == supportedLocale.languageCode) { - return supportedLocale; - } - } - return supportedLocales.first; - } - - 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( - 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(context); - return Container(key: auth.rootKey, child: _buildChild(context)); + final theme = Provider.of(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, + ), + ); } } diff --git a/lib/home.dart b/lib/home.dart index 67e9c77b..2acc1811 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -40,6 +40,7 @@ class _HomeState extends State { final GlobalKey tab5 = GlobalKey(); _buildScreen(int index) { + // print(Localizations.localeOf(context).toString()); // return GlProjectScreen(32221); // return GhIssuesScreen('flutter', 'flutter', isPullRequest: true); // return GhIssueScreen('reactjs', 'rfcs', 29); diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb index 23b122eb..b6f1a7fa 100644 --- a/lib/l10n/intl_en.arb +++ b/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" diff --git a/lib/l10n/intl_es.arb b/lib/l10n/intl_es.arb index b40798d3..a2b531b6 100644 --- a/lib/l10n/intl_es.arb +++ b/lib/l10n/intl_es.arb @@ -346,9 +346,5 @@ "news": "Noticias", "@news": { "description": "The News tab" - }, - "helloWorld": "¡Hola, Mundo!", - "@helloWorld": { - "description": "The conventional newborn programmer greeting" } } diff --git a/lib/l10n/intl_hi.arb b/lib/l10n/intl_hi.arb index 75408fc3..43711c45 100644 --- a/lib/l10n/intl_hi.arb +++ b/lib/l10n/intl_hi.arb @@ -1,5 +1,4 @@ { - "helloWorld": "नमस्ते दुनिया", "news": "समाचार", "notification": "अधिसूचना", "trending": "ट्रेंडिंग", @@ -88,4 +87,4 @@ "fontFamily": "फॉण्ट परिवार", "fontSize": "फॉण्ट आकार", "fontStyle": "फॉण्ट प्रकार" -} \ No newline at end of file +} diff --git a/lib/l10n/intl_nb_NO.arb b/lib/l10n/intl_nb_NO.arb index b3ae15dc..14ee8a9f 100644 --- a/lib/l10n/intl_nb_NO.arb +++ b/lib/l10n/intl_nb_NO.arb @@ -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" diff --git a/lib/l10n/intl_pt_BR.arb b/lib/l10n/intl_pt_BR.arb index 6703bfcd..f31f4dd0 100644 --- a/lib/l10n/intl_pt_BR.arb +++ b/lib/l10n/intl_pt_BR.arb @@ -346,9 +346,5 @@ "news": "Notícias", "@news": { "description": "The News tab" - }, - "helloWorld": "Olá Mundo!", - "@helloWorld": { - "description": "The conventional newborn programmer greeting" } } diff --git a/lib/models/theme.dart b/lib/models/theme.dart index 62ca5e05..c5dd81ac 100644 --- a/lib/models/theme.dart +++ b/lib/models/theme.dart @@ -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 { 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 setLocale(String v) async { _locale = v; final prefs = await SharedPreferences.getInstance();