-
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.
feat: Adding different locales that switch according to the given tit…
…le. #4
- Loading branch information
1 parent
5755e01
commit 0dd3783
Showing
6 changed files
with
84 additions
and
6 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,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<AppLocalization>(context, AppLocalization)!; | ||
} | ||
|
||
String getMenuItemTitle(MenuItemInfo item) { | ||
final Map<String, dynamic> title = item.title; | ||
|
||
return title[_locale.languageCode] ?? ""; | ||
} | ||
|
||
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); | ||
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
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