Skip to content

Commit

Permalink
Merge pull request #80 from allanlucio/feature/Internationalization
Browse files Browse the repository at this point in the history
Feature/internationalization
  • Loading branch information
lucioeduardo authored May 2, 2020
2 parents ec45a59 + c0937b8 commit 2ba6eb6
Show file tree
Hide file tree
Showing 31 changed files with 536 additions and 115 deletions.
48 changes: 46 additions & 2 deletions lib/app/app_widget.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'package:corona_data/app/shared/config/config.dart';
import 'package:corona_data/app/shared/utils/localization/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:i18n_extension/i18n_widget.dart';
import 'package:mobx/mobx.dart';

import 'app_controller.dart';
Expand All @@ -11,21 +15,61 @@ class AppWidget extends StatefulWidget {

class _AppWidgetState extends State<AppWidget> {
final AppController controller = Modular.get();
final GlobalKey _rootKey = GlobalKey();
List<ReactionDisposer> disposers = [];

@override
void initState() {
super.initState();
reaction((_) => controller.globalSettingsController.theme, (_) => (setState((){})));

disposers.add(
reaction(
(_) => controller.globalSettingsController.theme,
(_) => (setState(() {})),
),
);

disposers.add(
reaction(
(_) => controller.globalSettingsController.locale,
(_) {
I18n.of(_rootKey.currentContext).locale =
controller.globalSettingsController.locale.getLocale();
},
),
);
}

@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: Modular.navigatorKey,
title: 'Flutter Slidy',
title: 'C19 Tracker',
theme: controller.globalSettingsController.theme.themeData,
initialRoute: '/',
onGenerateRoute: Modular.generateRoute,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', "US"),
const Locale('pt', "BR"),
],
builder: (context, widget) {
return I18n(
initialLocale:
controller.globalSettingsController.locale.getLocale(),
child: Container(key: _rootKey, child: widget));
},
);
}

@override
void dispose() {
disposers.forEach((disposer) => disposer());

super.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class _WorldCasesControllerBase with Store implements IChartController
final HistoricalRepository historicalRepository;

_WorldCasesControllerBase(this.historicalRepository){
print("W---");

fetchGraphData();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/app/modules/country/country_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:corona_data/app/shared/widgets/try_again_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_modular/flutter_modular.dart';

import 'country_controller.dart';


class CountryPage extends StatefulWidget {
final String title;
final AnimationController controller;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';

import '../../app_controller.dart';

import "translations/country_page.i18n.dart";
class CoutryPageStaggerAnimation extends StatelessWidget {
final AnimationController controller;
final InfoModel info;
Expand Down
12 changes: 12 additions & 0 deletions lib/app/modules/country/translations/country_page.i18n.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:i18n_extension/i18n_extension.dart';

extension Localization on String {

static var _t = Translations("en_us") +
{
"en_us": "Brazil",
"pt_br": "Brasil",
};

String get i18n => localize(this, _t);
}
7 changes: 5 additions & 2 deletions lib/app/modules/home/home_controller.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:corona_data/app/modules/country/country_module.dart';
import 'package:corona_data/app/modules/settings/global_settings_controller.dart';
import 'package:corona_data/app/modules/states_map/states_map_module.dart';
import 'package:corona_data/app/modules/world/world_module.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:mobx/mobx.dart';

part 'home_controller.g.dart';
Expand All @@ -11,11 +13,12 @@ class HomeController = _HomeControllerBase with _$HomeController;
abstract class _HomeControllerBase with Store {
@observable
int selectedIndex;
GlobalSettingsController globalSettingsController = Modular.get();

final _titleName = ["country", "Mundo", "Mapa"];
final _titleName = ["country", "World", "Map"];

@computed
String get title => _titleName[selectedIndex];
String get title => _titleName[selectedIndex]=='country'?globalSettingsController.country.value.name:_titleName[selectedIndex];

final List<Widget> _pages = [
CountryModule(),
Expand Down
12 changes: 9 additions & 3 deletions lib/app/modules/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:google_fonts/google_fonts.dart';
import 'translations/home_page.i18n.dart';

class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
Expand All @@ -15,9 +16,16 @@ class HomePage extends StatefulWidget {

class _HomePageState extends ModularState<HomePage, HomeController> {
final AppController appController = Modular.get();
@override
void initState() {
// TODO: implement initState
super.initState();

}

@override
Widget build(BuildContext context) {


return Observer(
builder: (BuildContext context) {
Expand All @@ -26,9 +34,7 @@ class _HomePageState extends ModularState<HomePage, HomeController> {
backgroundColor: Theme.of(context).backgroundColor,
appBar: AppBar(
title: Text(
controller.title == 'country'
? appController.globalSettingsController.country.value.name
: controller.title,
controller.title.i18n,
style: GoogleFonts.robotoSlab(
fontSize: 24,
letterSpacing: -1.9,
Expand Down
24 changes: 24 additions & 0 deletions lib/app/modules/home/translations/home_page.i18n.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:i18n_extension/i18n_extension.dart';

extension Localization on String {

static var _t = Translations("en_us") +
{
"en_us": "Brazil",
"pt_br": "Brasil",
}+
{
"en_us": "United States",
"pt_br": "Estados Unidos",
}+
{
"en_us": "Map",
"pt_br": "Mapa",
}+
{
"en_us": "World",
"pt_br": "Mundo",
};

String get i18n => localize(this, _t);
}
35 changes: 32 additions & 3 deletions lib/app/modules/settings/global_settings_controller.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:corona_data/app/shared/models/country_model.dart';
import 'package:corona_data/app/shared/repositories/local_storage_interface.dart';
import 'package:corona_data/app/shared/utils/localization/localization_interface.dart';
import 'package:corona_data/app/shared/utils/localization/localization_utils.dart';
import 'package:corona_data/app/shared/utils/theme/theme_interface.dart';
import 'package:corona_data/app/shared/utils/theme/theme_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:mobx/mobx.dart';

Expand All @@ -17,28 +18,40 @@ abstract class _GlobalSettingsControllerBase with Store {
@observable
ObservableFuture<String> themeName;

@observable
ObservableFuture<String> localeKey;

@observable
ObservableFuture<CountryModel> country;

@computed
ITheme get theme {
return ThemeUtils.getTheme(themeName?.value);
}

@computed
ILocalization get locale {

return LocalizationUtils.getLocale(localeKey?.value);
}

@computed
bool get isReady {
return themeName.value != null && country.value != null;

return themeName.value != null && country.value != null && localeKey.value != null;
}

@action
void init() {
getTheme();
getCountry();
getLocale();
}

@computed
int get isChanged =>
(country.value.name + themeName.value.toString()).hashCode;

(country.value.name + themeName.value.toString() + localeKey.value).hashCode;

@action
void getTheme() {
Expand Down Expand Up @@ -67,6 +80,22 @@ abstract class _GlobalSettingsControllerBase with Store {
this.country = localStorage.getCountry().asObservable();
}

@action
void setLocale(String localeKey) {
if (localeKey!= this.localeKey.value) {
this.localeKey = ObservableFuture.value(localeKey);
localStorage.setLocale(localeKey);

}
}

@action
void getLocale() {
this.localeKey = localStorage.getLocale().asObservable();

}


}


Expand Down
46 changes: 45 additions & 1 deletion lib/app/modules/settings/global_settings_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/app/modules/settings/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ abstract class _SettingsControllerBase with Store implements Disposable {

@override
void dispose() {
print("Settings dispose");
// print("Settings dispose");
}
}
Loading

0 comments on commit 2ba6eb6

Please sign in to comment.