CWA-174 | added localeDetection() to Language class, moved languages from change_language.dart to language.dart, localeDetection() called in the settings store

This commit is contained in:
Oleksandr Sobol 2020-02-26 19:18:58 +02:00
parent 35f1498331
commit 90f66274a2
5 changed files with 40 additions and 22 deletions

View file

@ -1,4 +1,21 @@
import 'package:flutter/material.dart';
import 'package:devicelocale/devicelocale.dart';
import 'package:intl/intl.dart';
const Map<String, String> 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<String> localeDetection() async {
String locale = await Devicelocale.currentLocale;
locale = Intl.shortLocale(locale);
return languages.keys.contains(locale) ? locale : 'en';
}
}

View file

@ -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<String, String> _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: <Widget>[
@ -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)),

View file

@ -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(

View file

@ -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:

View file

@ -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.