From 90f66274a26992c83f6cfd9a13274b955c4b7c8e Mon Sep 17 00:00:00 2001 From: Oleksandr Sobol Date: Wed, 26 Feb 2020 19:18:58 +0200 Subject: [PATCH] CWA-174 | added localeDetection() to Language class, moved languages from change_language.dart to language.dart, localeDetection() called in the settings store --- lib/src/domain/common/language.dart | 24 +++++++++++++++++ lib/src/screens/settings/change_language.dart | 27 +++++-------------- lib/src/stores/settings/settings_store.dart | 3 ++- pubspec.lock | 7 +++++ pubspec.yaml | 1 + 5 files changed, 40 insertions(+), 22 deletions(-) diff --git a/lib/src/domain/common/language.dart b/lib/src/domain/common/language.dart index 70f7b5899..7d2860978 100644 --- a/lib/src/domain/common/language.dart +++ b/lib/src/domain/common/language.dart @@ -1,4 +1,21 @@ import 'package:flutter/material.dart'; +import 'package:devicelocale/devicelocale.dart'; +import 'package:intl/intl.dart'; + +const Map languages = { + 'en': 'English', + 'de': 'Deutsch (German)', + 'es': 'Español (Spanish)', + 'hi': 'हिंदी (Hindi)', + 'ja': '日本 (Japanese)', + 'ko': '한국어 (Korean)', + 'nl': 'Nederlands (Dutch)', + 'pl': 'Polski (Polish)', + 'pt': 'Português (Portuguese)', + 'ru': 'Русский (Russian)', + 'uk': 'Українська (Ukrainian)', + 'zh': '中文 (Chinese)' +}; class Language with ChangeNotifier { Language(this._currentLanguage); @@ -11,4 +28,11 @@ class Language with ChangeNotifier { _currentLanguage = language; notifyListeners(); } + + static Future localeDetection() async { + String locale = await Devicelocale.currentLocale; + locale = Intl.shortLocale(locale); + + return languages.keys.contains(locale) ? locale : 'en'; + } } \ No newline at end of file diff --git a/lib/src/screens/settings/change_language.dart b/lib/src/screens/settings/change_language.dart index b708f26be..317fa394a 100644 --- a/lib/src/screens/settings/change_language.dart +++ b/lib/src/screens/settings/change_language.dart @@ -6,21 +6,6 @@ import 'package:cake_wallet/src/domain/common/language.dart'; import 'package:cake_wallet/src/stores/settings/settings_store.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; -const Map _languages = { - 'en': 'English', - 'de': 'Deutsch (German)', - 'es': 'Español (Spanish)', - 'hi': 'हिंदी (Hindi)', - 'ja': '日本 (Japanese)', - 'ko': '한국어 (Korean)', - 'nl': 'Nederlands (Dutch)', - 'pl': 'Polski (Polish)', - 'pt': 'Português (Portuguese)', - 'ru': 'Русский (Russian)', - 'uk': 'Українська (Ukrainian)', - 'zh': '中文 (Chinese)' -}; - class ChangeLanguage extends BasePage { @override String get title => S.current.settings_change_language; @@ -36,11 +21,11 @@ class ChangeLanguage extends BasePage { return Container( padding: EdgeInsets.only(top: 10.0, bottom: 10.0), child: ListView.builder( - itemCount: _languages.values.length, + itemCount: languages.values.length, itemBuilder: (BuildContext context, int index) { final isCurrent = settingsStore.languageCode == null ? false - : _languages.keys.elementAt(index) == + : languages.keys.elementAt(index) == settingsStore.languageCode; return Container( @@ -48,7 +33,7 @@ class ChangeLanguage extends BasePage { color: isCurrent ? currentColor : notCurrentColor, child: ListTile( title: Text( - _languages.values.elementAt(index), + languages.values.elementAt(index), style: TextStyle( fontSize: 16.0, color: Theme.of(context).primaryTextTheme.title.color), @@ -65,7 +50,7 @@ class ChangeLanguage extends BasePage { ), content: Text( S.of(context).change_language_to( - _languages.values.elementAt(index)), + languages.values.elementAt(index)), textAlign: TextAlign.center, ), actions: [ @@ -76,9 +61,9 @@ class ChangeLanguage extends BasePage { onPressed: () { settingsStore.saveLanguageCode( languageCode: - _languages.keys.elementAt(index)); + languages.keys.elementAt(index)); currentLanguage.setCurrentLanguage( - _languages.keys.elementAt(index)); + languages.keys.elementAt(index)); Navigator.of(context).pop(); }, child: Text(S.of(context).change)), diff --git a/lib/src/stores/settings/settings_store.dart b/lib/src/stores/settings/settings_store.dart index 786d4083e..2faf106d0 100644 --- a/lib/src/stores/settings/settings_store.dart +++ b/lib/src/stores/settings/settings_store.dart @@ -13,6 +13,7 @@ import 'package:cake_wallet/src/screens/settings/items/item_headers.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/domain/common/default_settings_migration.dart'; import 'package:package_info/package_info.dart'; +import 'package:cake_wallet/src/domain/common/language.dart'; part 'settings_store.g.dart'; @@ -93,7 +94,7 @@ abstract class SettingsStoreBase with Store { : sharedPreferences.getInt(currentPinLength); final savedLanguageCode = sharedPreferences.getString(currentLanguageCode) == null - ? 'en' + ? await Language.localeDetection() : sharedPreferences.getString(currentLanguageCode); final store = SettingsStore( diff --git a/pubspec.lock b/pubspec.lock index 5c7088d15..e9687ee2b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -211,6 +211,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.6" + devicelocale: + dependency: "direct main" + description: + name: devicelocale + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.1" dio: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 58f13a7f5..b3e4f5082 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -47,6 +47,7 @@ dependencies: hive_flutter: ^0.2.1 local_auth: ^0.6.1 package_info: ^0.4.0+13 + devicelocale: ^0.2.1 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.