From 28e5cd8a7f2fdfe09d0a3c1fc580d0b4a7a0cebb Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Sun, 8 Mar 2020 23:30:49 +0100 Subject: [PATCH] fix localization --- CHANGELOG.md | 4 ++++ README.md | 18 ++++++++++++++++++ example/lib/main.dart | 2 +- lib/country_code.dart | 6 ++++++ lib/country_code_picker.dart | 8 ++++---- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 477e5ee..0fb070a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.3.3 + +- Fix a bug in localization + ## 1.3.2 - Add `enable` property in order to use the disable the button diff --git a/README.md b/README.md index dfa1581..e12d084 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,24 @@ void _onCountryChange(CountryCode countryCode) { ``` +### i18n + +Just add the `CountryLocalizations.delegate` in the list of your app delegates + +```dart + return new MaterialApp( + supportedLocales: [ + Locale('en'), + Locale('it'), + Locale('en'), + ], + localizationsDelegates: [ + CountryLocalizations.delegate, + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + ], +``` + ## Contributions Contributions of any kind are more than welcome! Feel free to fork and improve country_code_picker in any way you want, make a pull request, or open an issue. diff --git a/example/lib/main.dart b/example/lib/main.dart index 1f105e6..fd3aa67 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -83,7 +83,7 @@ class _MyAppState extends State { padding: const EdgeInsets.all(8.0), child: CountryCodePicker( enabled: false, - onChanged: print, + onChanged: (c) => c.name, initialSelection: 'TF', showCountryOnly: true, showOnlyCountryWhenClosed: true, diff --git a/lib/country_code.dart b/lib/country_code.dart index 63997b3..13c1381 100644 --- a/lib/country_code.dart +++ b/lib/country_code.dart @@ -41,6 +41,12 @@ class CountryCode { return CountryCode.fromJson(jsonCode); } + CountryCode localize(BuildContext context) { + return this + ..name = + CountryLocalizations.of(context)?.translate(this.code) ?? this.name; + } + factory CountryCode.fromJson(Map json) { return CountryCode( name: json['name'], diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index 3df019b..d2b422c 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -90,7 +90,7 @@ class _CountryCodePickerState extends State { if (widget.builder != null) _widget = InkWell( onTap: _showSelectionDialog, - child: widget.builder(selectedItem), + child: widget.builder(selectedItem.localize(context)), ); else { _widget = FlatButton( @@ -200,13 +200,13 @@ class _CountryCodePickerState extends State { void _publishSelection(CountryCode e) { if (widget.onChanged != null) { - widget.onChanged(e); + widget.onChanged(e.localize(context)); } } - void _onInit(CountryCode initialData) { + void _onInit(CountryCode e) { if (widget.onInit != null) { - widget.onInit(initialData); + widget.onInit(e.localize(context)); } } }