Skip to content

Commit

Permalink
feat: Adding translations to the rest of the app. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed May 10, 2023
1 parent 30b01a8 commit c60546c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 33 deletions.
14 changes: 13 additions & 1 deletion assets/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"title": "This is the main page",
"description": "Check the todo item below to open the menu above to check more pages."
"description": "Check the todo item below to open the menu above to check more pages.",

"menu.todo": "Todo List (Personal)",
"menu.feature": "Feature Tour",
"menu.settings": "Settings",

"feature_page.title": "This is the Tour page 🚩",
"feature_page.description": "As you can see, this is just a sample page. You can go back by pressing the button below.",

"settings_page.title": "This is the Settings page ⚙️",
"settings_page.description": "As you can say, this is just a sample page. You can go back by pressing the button below.",

"goBack_button": "Go back"
}
14 changes: 13 additions & 1 deletion assets/i18n/pt.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{
"title": "Esta é a página inicial.",
"description": "Clica no item abaixo para abrir o menu acima para ver mais páginas."
"description": "Clica no item abaixo para abrir o menu acima para ver mais páginas.",

"menu.todo": "Lista de tarefas (Pessoal)",
"menu.feature": "Visão geral",
"menu.settings": "Definições",

"feature_page.title": "Esta é a página de visão geral 🚩",
"feature_page.description": "Isto é apenas uma página de exemplo. Para voltar, clique no botão abaixo.",

"settings_page.title": "Esta é a página de configurações ⚙️",
"settings_page.description": "Isto é apenas uma página de exemplo. Para voltar, clique no botão abaixo.",

"goBack_button": "Voltar"
}

10 changes: 4 additions & 6 deletions lib/app_localization.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';

import 'package:app/settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Expand All @@ -16,15 +15,14 @@ class AppLocalization {

late Map<String, String> _localizedValues;

// This function will load requested language `.json` file and will assign it to the `_localizedValues` map
/// This function will load requested language `.json` file and will assign it to the `_localizedValues` map
Future loadLanguage() async {
final path = 'assets/i18n/${_locale.languageCode}.json';
String jsonStringValues = await rootBundle.loadString(path, cache: false);
String jsonStringValues = await rootBundle.loadString('assets/i18n/${_locale.languageCode}.json', cache: false);

Map<String, dynamic> mappedValues = json.decode(jsonStringValues);

_localizedValues = mappedValues.map((key, value) =>
MapEntry(key, value.toString())); // converting `dynamic` value to `String`, because `_localizedValues` is of type Map<String,String>
// converting `dynamic` value to `String`, because `_localizedValues` is of type Map<String,String>
_localizedValues = mappedValues.map((key, value) => MapEntry(key, value.toString()));
}

String? getTranslatedValue(String key) {
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class App extends StatelessWidget {
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
locale: const Locale('pt', 'PT'),
supportedLocales: const [
Locale('en', 'US'),
Locale('pt', 'PT'),
Expand Down
17 changes: 9 additions & 8 deletions lib/menu.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';

import 'app_localization.dart';
import 'pages.dart';
import 'dynamic_menu.dart';
import 'settings.dart';
Expand Down Expand Up @@ -60,14 +61,14 @@ class _DrawerMenuState extends State<DrawerMenu> {
Container(
padding: const EdgeInsets.only(top: 15, bottom: 15),
decoration: const BoxDecoration(border: Border(bottom: BorderSide(color: Colors.white), top: BorderSide(color: Colors.white))),
child: const ListTile(
leading: Icon(
child: ListTile(
leading: const Icon(
Icons.check_outlined,
color: Colors.white,
size: 50,
),
title: Text('Todo List (Personal)',
style: TextStyle(
title: Text(AppLocalization.of(context).getTranslatedValue("menu.todo").toString(),
style: const TextStyle(
fontSize: 30,
color: Colors.white,
)),
Expand All @@ -83,8 +84,8 @@ class _DrawerMenuState extends State<DrawerMenu> {
color: Colors.white,
size: 40,
),
title: const Text('Feature Tour',
style: TextStyle(
title: Text(AppLocalization.of(context).getTranslatedValue("menu.feature").toString(),
style: const TextStyle(
fontSize: 25,
color: Colors.white,
)),
Expand All @@ -106,8 +107,8 @@ class _DrawerMenuState extends State<DrawerMenu> {
color: Colors.white,
size: 40,
),
title: const Text('Settings',
style: TextStyle(
title: Text(AppLocalization.of(context).getTranslatedValue("menu.settings").toString(),
style: const TextStyle(
fontSize: 25,
color: Colors.white,
)),
Expand Down
30 changes: 16 additions & 14 deletions lib/pages.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';

import 'app_localization.dart';

const tourPageKey = Key("tour_page");
const settingsPageKey = Key("settings_page");

Expand All @@ -14,16 +16,16 @@ class TourPage extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
"This is the Tour page 🚩",
style: TextStyle(fontSize: 30),
Text(
AppLocalization.of(context).getTranslatedValue("feature_page.title").toString(),
style: const TextStyle(fontSize: 30),
),
const Padding(
padding: EdgeInsets.all(16),
Padding(
padding: const EdgeInsets.all(16),
child: Text(
"As you can say, this is just a sample page. You can go back by pressing the button below.",
AppLocalization.of(context).getTranslatedValue("feature_page.description").toString(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 15, color: Colors.black87),
style: const TextStyle(fontSize: 15, color: Colors.black87),
),
),
ElevatedButton(
Expand All @@ -50,16 +52,16 @@ class SettingsPage extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
"This is the Settings page ⚙️",
style: TextStyle(fontSize: 30),
Text(
AppLocalization.of(context).getTranslatedValue("settings_page.title").toString(),
style: const TextStyle(fontSize: 30),
),
const Padding(
padding: EdgeInsets.all(16),
Padding(
padding: const EdgeInsets.all(16),
child: Text(
"As you can say, this is just a sample page. You can go back by pressing the button below.",
AppLocalization.of(context).getTranslatedValue("settings_page.description").toString(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 15, color: Colors.black87),
style: const TextStyle(fontSize: 15, color: Colors.black87),
),
),
ElevatedButton(
Expand Down
3 changes: 0 additions & 3 deletions test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import 'package:app/app_localization.dart';
import 'package:app/menu.dart';
import 'package:app/pages.dart';
import 'package:app/dynamic_menu.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:app/main.dart';
Expand Down

0 comments on commit c60546c

Please sign in to comment.