From 0dd37836c687ceb8b24a8ee77e6040224706d57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Arteiro?= Date: Tue, 9 May 2023 17:31:12 +0100 Subject: [PATCH] feat: Adding different locales that switch according to the given title. #4 --- lib/app_localization.dart | 42 +++++++++++++++++++++++++++++++++++++++ lib/dynamic_menu.dart | 5 +++-- lib/main.dart | 20 +++++++++++++++++++ lib/settings.dart | 8 ++++---- pubspec.lock | 13 ++++++++++++ pubspec.yaml | 2 ++ 6 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 lib/app_localization.dart diff --git a/lib/app_localization.dart b/lib/app_localization.dart new file mode 100644 index 0000000..87eade7 --- /dev/null +++ b/lib/app_localization.dart @@ -0,0 +1,42 @@ +import 'package:app/settings.dart'; +import 'package:flutter/material.dart'; + +/// Utils class for app localization with delegate +class AppLocalization { + late final Locale _locale; + + AppLocalization(this._locale); + + static AppLocalization of(BuildContext context) { + return Localizations.of(context, AppLocalization)!; + } + + String getMenuItemTitle(MenuItemInfo item) { + final Map title = item.title; + + return title[_locale.languageCode] ?? ""; + } + + static const LocalizationsDelegate delegate = _AppLocalizationDelegate(); +} + +/// Private overriden delegate class +class _AppLocalizationDelegate extends LocalizationsDelegate { + const _AppLocalizationDelegate(); + + // It will check if the user's locale is supported by our App or not + @override + bool isSupported(Locale locale) { + return ["en", "pt"].contains(locale.languageCode); + } + + // It will load the equivalent json file requested by the user + @override + Future load(Locale locale) async { + AppLocalization appLocalization = AppLocalization(locale); + return appLocalization; + } + + @override + bool shouldReload(_AppLocalizationDelegate old) => false; +} diff --git a/lib/dynamic_menu.dart b/lib/dynamic_menu.dart index 907fb02..368221f 100644 --- a/lib/dynamic_menu.dart +++ b/lib/dynamic_menu.dart @@ -2,6 +2,7 @@ import 'dart:ui'; import 'package:flutter/material.dart'; +import 'app_localization.dart'; import 'settings.dart'; /// Proxy decorator function that overrides the background color @@ -146,7 +147,7 @@ class _MenuItemState extends State { child: ListTile( contentPadding: EdgeInsets.only(left: widget.leftPadding), leading: widget.info.getIcon(), - title: Text(widget.info.title, + title: Text(AppLocalization.of(context).getMenuItemTitle(widget.info), style: TextStyle( fontSize: 25, color: widget.info.textColor, @@ -164,7 +165,7 @@ class _MenuItemState extends State { // so they can be reordered on the same level. child: ExpansionTile( tilePadding: EdgeInsets.only(left: widget.leftPadding), - title: Text(widget.info.title, + title: Text(AppLocalization.of(context).getMenuItemTitle(widget.info), style: TextStyle( fontSize: 25, color: widget.info.textColor, diff --git a/lib/main.dart b/lib/main.dart index 2d46563..c1caa75 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'app_localization.dart'; import 'menu.dart'; const iconKey = Key("menu_icon"); @@ -23,6 +25,24 @@ class App extends StatelessWidget { primarySwatch: Colors.blue, ), debugShowCheckedModeBanner: false, + supportedLocales: const [ + Locale('en', 'US'), + Locale('pt', 'PT'), + ], + localeResolutionCallback: (deviceLocale, supportedLocales) { + for (var locale in supportedLocales) { + if (locale.languageCode == deviceLocale!.languageCode && locale.countryCode == deviceLocale.countryCode) { + return deviceLocale; + } + } + return supportedLocales.first; + }, + localizationsDelegates: const [ + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + AppLocalization.delegate + ], home: const HomePage()); } } diff --git a/lib/settings.dart b/lib/settings.dart index a45ce92..c33e06a 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -34,11 +34,11 @@ class MenuItemInfoIcon { } } -/// Class holding the information of the tile +/// Class holding the information of the menu item tile class MenuItemInfo { late int id; late int indexInLevel; - late String title; + late Map title; late Color textColor; late MenuItemInfoIcon? _icon; late List tiles; @@ -118,8 +118,8 @@ class MenuItemInfo { }); } - _icon = null; // Decoding `icon` field + _icon = null; if (json['icon'] != null) { _icon = MenuItemInfoIcon.fromJson(json['icon']); } @@ -154,7 +154,7 @@ class MenuItemInfo { Future> loadMenuItems() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); - //await prefs.remove(storageKey); + await prefs.remove(storageKey); final String? jsonStringFromLocalStorage = prefs.getString(storageKey); diff --git a/pubspec.lock b/pubspec.lock index 36234cb..52221d4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -174,6 +174,11 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" flutter_test: dependency: "direct dev" description: flutter @@ -192,6 +197,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + intl: + dependency: transitive + description: + name: intl + sha256: "910f85bce16fb5c6f614e117efa303e85a1731bb0081edf3604a2ae6e9a3cc91" + url: "https://pub.dev" + source: hosted + version: "0.17.0" js: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 128ddfa..3131dab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,6 +31,8 @@ environment: dependencies: flutter: sdk: flutter + flutter_localizations: + sdk: flutter # The following adds the Cupertino Icons font to your application.