-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"title": "This is the main page", | ||
"description": "Check the todo item below to open the menu above to check more pages." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"title": "Esta é a página inicial.", | ||
"description": "Clica no item abaixo para abrir o menu acima para ver mais páginas." | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:app/settings.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.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<AppLocalization>(context, AppLocalization)!; | ||
} | ||
|
||
late Map<String, String> _localizedValues; | ||
|
||
// This function will load requested language `.json` file and will assign it to the `_localizedValues` map | ||
Future loadLanguage() async { | ||
String jsonStringValues = await rootBundle.loadString("assets/i18n/${_locale.languageCode}.json"); | ||
|
||
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> | ||
} | ||
|
||
String? getTranslatedValue(String key) { | ||
return _localizedValues[key]; | ||
} | ||
|
||
static const LocalizationsDelegate<AppLocalization> delegate = _AppLocalizationDelegate(); | ||
} | ||
|
||
/// Private overriden delegate class | ||
class _AppLocalizationDelegate extends LocalizationsDelegate<AppLocalization> { | ||
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<AppLocalization> load(Locale locale) async { | ||
AppLocalization appLocalization = AppLocalization(locale); | ||
await appLocalization.loadLanguage(); | ||
return appLocalization; | ||
} | ||
|
||
@override | ||
bool shouldReload(_AppLocalizationDelegate old) => false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters