Skip to content

Commit f4930b2

Browse files
estrutura de internacionalização finalizada
1 parent fabaa81 commit f4930b2

12 files changed

+178
-19
lines changed

lang/en.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"home": "Home",
3+
"article_details": "Article Details",
4+
"menu": "Menu",
5+
"settings": "Settings",
6+
"theme": "Tema",
7+
"light": "Light",
8+
"dark": "Dark",
9+
"font_size": "Font Size",
10+
"small": "Small",
11+
"medium": "Medium",
12+
"large": "Large",
13+
"extra_large": "Extra Large"
14+
}

lang/es.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"home": "Pagina Principal",
3+
"article_details": "Detalles del Artículo",
4+
"menu": "Opciones",
5+
"settings": "Ajustes",
6+
"theme": "Tema",
7+
"light": "Claro",
8+
"dark": "Oscuro",
9+
"font_size": "Tamaño de fuente",
10+
"small": "Pequeña",
11+
"medium": "Medio",
12+
"large": "Grande",
13+
"extra_large": "Extra Grande"
14+
}

lang/pt.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"home": "Início",
3+
"article_details": "Detalhes do artigo",
4+
"menu": "Opções",
5+
"settings": "Configurações",
6+
"theme": "Tema",
7+
"light": "Claro",
8+
"dark": "Escuro",
9+
"font_size": "Tamanho da fonte",
10+
"small": "Pequeno",
11+
"medium": "Médio",
12+
"large": "Grande",
13+
"extra_large": "Extra Grande"
14+
}

lib/components/main_drawer.dart

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:appbasepe/blocs/theme.dart';
22
import 'package:appbasepe/components/radio_form.dart';
3+
import 'package:appbasepe/core/internationalization/app_translate.dart';
34
import 'package:flutter/material.dart';
45
import 'package:provider/provider.dart';
56

@@ -9,20 +10,27 @@ class MainDrawer extends StatefulWidget {
910
}
1011

1112
class _MainDrawerState extends State<MainDrawer> {
12-
static const themeProps = [
13-
{"label": "Light", "value": ThemeColorProps.light},
14-
{"label": "Dark", "value": ThemeColorProps.dark}
15-
];
16-
17-
static const fontProps = [
18-
{"label": 'Small', "value": ThemeTextProps.small},
19-
{"label": 'Medium', "value": ThemeTextProps.medium},
20-
{"label": 'Large', "value": ThemeTextProps.large},
21-
{"label": 'Extra large', "value": ThemeTextProps.extraLarge}
22-
];
23-
2413
@override
2514
Widget build(BuildContext context) {
15+
final strLight = AppTranslate(context).text("light");
16+
final strDark = AppTranslate(context).text("dark");
17+
18+
final strSmall = AppTranslate(context).text("small");
19+
final strMedium = AppTranslate(context).text("medium");
20+
final strLarge = AppTranslate(context).text("large");
21+
final strExtraLarge = AppTranslate(context).text("extra_large");
22+
23+
final themeProps = [
24+
{"label": strLight, "value": ThemeColorProps.light},
25+
{"label": strDark, "value": ThemeColorProps.dark}
26+
];
27+
final fontProps = [
28+
{"label": strSmall, "value": ThemeTextProps.small},
29+
{"label": strMedium, "value": ThemeTextProps.medium},
30+
{"label": strLarge, "value": ThemeTextProps.large},
31+
{"label": strExtraLarge, "value": ThemeTextProps.extraLarge}
32+
];
33+
2634
final theme = Provider.of<ThemeChanger>(context);
2735
ThemeColorProps _valueTheme = theme.getThemeColor();
2836
ThemeTextProps _valueThemeText = theme.getThemeText();
@@ -44,7 +52,7 @@ class _MainDrawerState extends State<MainDrawer> {
4452
return Drawer(
4553
child: Scaffold(
4654
appBar: AppBar(
47-
title: Text("Home"),
55+
title: Text(AppTranslate(context).text("menu")),
4856
centerTitle: true,
4957
automaticallyImplyLeading: false,
5058
),
@@ -57,7 +65,7 @@ class _MainDrawerState extends State<MainDrawer> {
5765
Container(
5866
margin: EdgeInsets.only(bottom: 10),
5967
child: Text(
60-
"Settings",
68+
AppTranslate(context).text("settings"),
6169
style: Theme.of(context).textTheme.headline5,
6270
),
6371
),
@@ -67,7 +75,7 @@ class _MainDrawerState extends State<MainDrawer> {
6775
crossAxisAlignment: CrossAxisAlignment.start,
6876
children: <Widget>[
6977
Text(
70-
"Theme",
78+
AppTranslate(context).text("theme"),
7179
style: Theme.of(context).textTheme.headline6,
7280
),
7381
RadioForm(
@@ -78,7 +86,7 @@ class _MainDrawerState extends State<MainDrawer> {
7886
),
7987
SizedBox(height: 10),
8088
Text(
81-
"Font Size",
89+
AppTranslate(context).text("font_size"),
8290
style: Theme.of(context).textTheme.headline6,
8391
),
8492
RadioForm(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'app_localizations.dart';
4+
5+
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
6+
const AppLocalizationsDelegate();
7+
8+
@override
9+
bool isSupported(Locale locale) {
10+
return ['pt', 'en', 'es'].contains(locale.languageCode);
11+
}
12+
13+
@override
14+
Future<AppLocalizations> load(Locale locale) async {
15+
var localizations = AppLocalizations(Locale('es', 'BR'));
16+
await localizations.load();
17+
return localizations;
18+
}
19+
20+
@override
21+
bool shouldReload(LocalizationsDelegate<AppLocalizations> old) => false;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'dart:convert';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter/services.dart';
4+
5+
import 'app_localization_delegate.dart';
6+
7+
class AppLocalizations {
8+
final Locale locale;
9+
10+
AppLocalizations(this.locale);
11+
12+
Map<String, dynamic> _localizationsStrings; // Mudança de String para Dynamic
13+
14+
Future<bool> load() async {
15+
var jsonString =
16+
await rootBundle.loadString('lang/${locale.languageCode}.json');
17+
Map<String, dynamic> jsonMap =
18+
json.decode(jsonString); // Mudança de String para Dynamic
19+
20+
_localizationsStrings = jsonMap.map((key, value) {
21+
return MapEntry(key, value); // Remover toString
22+
});
23+
return true;
24+
}
25+
26+
String translate(String key) {
27+
return _localizationsStrings[key];
28+
}
29+
30+
static AppLocalizations of(BuildContext context) {
31+
return Localizations.of<AppLocalizations>(context, AppLocalizations);
32+
}
33+
34+
static const LocalizationsDelegate<AppLocalizations> delegate =
35+
AppLocalizationsDelegate();
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'app_localizations.dart';
4+
5+
class AppTranslate {
6+
final BuildContext context;
7+
8+
AppTranslate(this.context);
9+
10+
String text(String key) {
11+
return AppLocalizations.of(context).translate(key);
12+
}
13+
}

lib/main.dart

+20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'package:appbasepe/blocs/theme.dart';
2+
import 'package:appbasepe/core/internationalization/app_localizations.dart';
23
import 'package:flutter/material.dart';
34
import 'package:appbasepe/screans/home_screen.dart';
45
import 'package:provider/provider.dart';
6+
import 'package:flutter_localizations/flutter_localizations.dart';
57

68
void main() {
79
runApp(MyApp());
@@ -26,6 +28,24 @@ class MaterialAppWithTheme extends StatelessWidget {
2628
title: 'Flutter Demo',
2729
theme: theme.getTheme(),
2830
home: HomeScreen(),
31+
localizationsDelegates: [
32+
AppLocalizations.delegate,
33+
GlobalMaterialLocalizations.delegate,
34+
GlobalWidgetsLocalizations.delegate,
35+
],
36+
supportedLocales: [
37+
const Locale('pt', ''), // Portuguese, no country code
38+
const Locale('en', ''), // English, no country code
39+
const Locale('es', ''), // Spanish, no country code
40+
],
41+
localeResolutionCallback: (locale, supportedLocales) {
42+
for (var supportedLocale in supportedLocales) {
43+
if (supportedLocale.languageCode == locale.languageCode) {
44+
return supportedLocale;
45+
}
46+
}
47+
return supportedLocales.first;
48+
},
2949
);
3050
}
3151
}

lib/screans/article_details_screen.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:appbasepe/core/internationalization/app_translate.dart';
12
import 'package:appbasepe/models/article.dart';
23
import 'package:flutter/material.dart';
34

@@ -10,7 +11,7 @@ class ArticleDetails extends StatelessWidget {
1011
Widget build(BuildContext context) {
1112
return Scaffold(
1213
appBar: AppBar(
13-
title: Text('Article details'),
14+
title: Text(AppTranslate(context).text("article_details")),
1415
),
1516
body: Column(
1617
children: <Widget>[

lib/screans/home_screen.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:appbasepe/components/article_list.dart';
22
import 'package:appbasepe/components/main_drawer.dart';
3+
import 'package:appbasepe/core/internationalization/app_translate.dart';
34
import 'package:appbasepe/data/dummy_data.dart';
45
import 'package:flutter/material.dart';
56
import 'package:appbasepe/models/article.dart';
@@ -11,7 +12,7 @@ class HomeScreen extends StatelessWidget {
1112
Widget build(BuildContext context) {
1213
return Scaffold(
1314
appBar: AppBar(
14-
title: Text("Home"),
15+
title: Text(AppTranslate(context).text("home")),
1516
),
1617
drawer: MainDrawer(),
1718
body: Container(

pubspec.lock

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ packages:
6969
description: flutter
7070
source: sdk
7171
version: "0.0.0"
72+
flutter_localizations:
73+
dependency: "direct main"
74+
description: flutter
75+
source: sdk
76+
version: "0.0.0"
7277
flutter_test:
7378
dependency: "direct dev"
7479
description: flutter
@@ -81,6 +86,13 @@ packages:
8186
url: "https://pub.dartlang.org"
8287
source: hosted
8388
version: "2.1.12"
89+
intl:
90+
dependency: transitive
91+
description:
92+
name: intl
93+
url: "https://pub.dartlang.org"
94+
source: hosted
95+
version: "0.16.1"
8496
matcher:
8597
dependency: transitive
8698
description:

pubspec.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ dependencies:
2424
flutter:
2525
sdk: flutter
2626

27+
flutter_localizations:
28+
sdk: flutter
29+
2730
# The following adds the Cupertino Icons font to your application.
2831
# Use with the CupertinoIcons class for iOS style icons.
2932
cupertino_icons: ^0.1.3
@@ -43,7 +46,8 @@ flutter:
4346
# the material Icons class.
4447
uses-material-design: true
4548
# To add assets to your application, add an assets section, like this:
46-
# assets:
49+
assets:
50+
- lang/
4751
# - images/a_dot_burr.jpeg
4852
# - images/a_dot_ham.jpeg
4953
# An image asset can refer to one or more resolution-specific "variants", see

0 commit comments

Comments
 (0)