From c467e179ad183e2db45394c52d8f2e9d3de9c37c Mon Sep 17 00:00:00 2001 From: derdilla Date: Thu, 20 Jul 2023 15:36:18 +0200 Subject: [PATCH 1/3] add locale selection --- lib/components/measurement_list.dart | 8 ++++---- lib/l10n/app_en.arb | 3 ++- lib/main.dart | 1 + lib/model/ram_only_implementations.dart | 10 ++++++++++ lib/model/settings_store.dart | 11 +++++++++++ lib/screens/settings.dart | 15 +++++++++++++++ pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + 8 files changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/components/measurement_list.dart b/lib/components/measurement_list.dart index 6a774cf4..b9f2ceb5 100644 --- a/lib/components/measurement_list.dart +++ b/lib/components/measurement_list.dart @@ -10,8 +10,8 @@ import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; class MeasurementList extends StatelessWidget { - late final _tableElementsSizes; - late final _sideFlex; + late final List _tableElementsSizes; + late final int _sideFlex; MeasurementList(BuildContext context, {super.key}) { if (MediaQuery.of(context).size.width < 1000) { @@ -37,7 +37,7 @@ class MeasurementList extends StatelessWidget { ), Expanded( flex: _tableElementsSizes[0], - child: Text(AppLocalizations.of(context)!.time, style: TextStyle(fontWeight: FontWeight.bold))), + child: Text(AppLocalizations.of(context)!.time, style: const TextStyle(fontWeight: FontWeight.bold))), Expanded( flex: _tableElementsSizes[1], child: Text(AppLocalizations.of(context)!.sysShort, style: TextStyle(fontWeight: FontWeight.bold, color: settings.sysColor))), @@ -49,7 +49,7 @@ class MeasurementList extends StatelessWidget { child: Text(AppLocalizations.of(context)!.pulShort, style: TextStyle(fontWeight: FontWeight.bold, color: settings.pulColor))), Expanded( flex: _tableElementsSizes[4], - child: Text(AppLocalizations.of(context)!.notes, style: TextStyle(fontWeight: FontWeight.bold))), + child: Text(AppLocalizations.of(context)!.notes, style: const TextStyle(fontWeight: FontWeight.bold))), Expanded( flex: _sideFlex, child: const SizedBox(), diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 5860081a..52064936 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -337,5 +337,6 @@ "@last30Days": {}, "allowMissingValues": "Allow missing values", "@allowMissingValues": {}, - "errTimeAfterNow": "The selected time of day is after this moment. We have automatically reset it to the current time. You can disable this validation in the settings!" + "errTimeAfterNow": "The selected time of day is after this moment. We have automatically reset it to the current time. You can disable this validation in the settings!", + "language": "Language" } diff --git a/lib/main.dart b/lib/main.dart index 3dc0259c..233deddc 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -50,6 +50,7 @@ class AppRoot extends StatelessWidget { GlobalCupertinoLocalizations.delegate, ], supportedLocales: AppLocalizations.supportedLocales, + locale: settings.language, home: const AppHome(), ); }); diff --git a/lib/model/ram_only_implementations.dart b/lib/model/ram_only_implementations.dart index e39c7cb0..4cfacade 100644 --- a/lib/model/ram_only_implementations.dart +++ b/lib/model/ram_only_implementations.dart @@ -70,6 +70,7 @@ class RamSettings extends ChangeNotifier implements Settings { String _defaultExportDir = ''; bool _exportAfterEveryEntry = false; bool _allowMissingValues = false; + Locale? _language; RamSettings() { _accentColor = createMaterialColor(0xFF009688); @@ -363,6 +364,15 @@ class RamSettings extends ChangeNotifier implements Settings { notifyListeners(); } + @override + Locale? get language => _language; + + @override + set language(Locale? value) { + _language = value; + notifyListeners(); + } + @override void changeStepSize(TimeStep value) { graphStepSize = value; diff --git a/lib/model/settings_store.dart b/lib/model/settings_store.dart index 06f6f158..13faa775 100644 --- a/lib/model/settings_store.dart +++ b/lib/model/settings_store.dart @@ -474,6 +474,17 @@ class Settings extends ChangeNotifier { _prefs.setBool('exportAfterEveryEntry', value); notifyListeners(); } + + Locale? get language { + final value = _prefs.getString('language'); + if (value?.isEmpty ?? true) return null; + return Locale(value ?? 'en'); + } + + set language (Locale? value) { + _prefs.setString('language', value?.languageCode ?? ''); + notifyListeners(); + } } enum TimeStep { diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index b49f4511..541893a4 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -9,6 +9,7 @@ import 'package:blood_pressure_app/screens/subsettings/warn_about.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:locale_names/locale_names.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -79,6 +80,20 @@ class SettingsPage extends StatelessWidget { } }, ), + DropDownSettingsTile( + key: const Key('language'), + leading: const Icon(Icons.language), + title: Text(AppLocalizations.of(context)!.language), + value: settings.language, + items: [ + DropdownMenuItem(value: null, child: Text(AppLocalizations.of(context)!.system)), + for (final l in AppLocalizations.supportedLocales) + DropdownMenuItem(value: l, child: Text(l.nativeDisplayLanguage ?? l.languageCode)), + ], + onChanged: (Locale? value) { + settings.language = value; + }, + ), SliderSettingsTile( key: const Key('iconSize'), title: Text(AppLocalizations.of(context)!.iconSize), diff --git a/pubspec.lock b/pubspec.lock index 06e7f580..a8539517 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -381,6 +381,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" + locale_names: + dependency: "direct main" + description: + name: locale_names + sha256: b536de2e2e46b34a69687e637ca95cdb60fe931a27bb3ad66344c527ebe8b432 + url: "https://pub.dev" + source: hosted + version: "1.0.0" logging: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 99e2c9e9..c4bc5db3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -49,6 +49,7 @@ dependencies: pdf: ^3.10.4 jsaver: ^1.2.0 package_info_plus: ^4.0.2 + locale_names: ^1.0.0 dev_dependencies: flutter_test: From 3b046abd8b30bae5a3f9acaf59f5cb20d70fd0f9 Mon Sep 17 00:00:00 2001 From: derdilla Date: Thu, 20 Jul 2023 15:44:05 +0200 Subject: [PATCH 2/3] update tests --- test/model/settings_test.dart | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/model/settings_test.dart b/test/model/settings_test.dart index f6da8457..67cc36d2 100644 --- a/test/model/settings_test.dart +++ b/test/model/settings_test.dart @@ -1,3 +1,5 @@ +import 'dart:ui'; + import 'package:blood_pressure_app/model/ram_only_implementations.dart'; import 'package:blood_pressure_app/model/settings_store.dart'; import 'package:file_saver/file_saver.dart'; @@ -47,6 +49,7 @@ void main() { expect(s.defaultExportDir.isEmpty, true); expect(s.exportAfterEveryEntry, false); expect(s.allowMissingValues, false); + expect(s.language, null); }); test('setting fields should save changes', () async { @@ -91,6 +94,7 @@ void main() { s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; s.allowMissingValues = true; + s.language = const Locale('de'); expect(s.displayDataStart, DateTime.fromMillisecondsSinceEpoch(10000)); expect(s.displayDataEnd, DateTime.fromMillisecondsSinceEpoch(200000)); @@ -118,6 +122,7 @@ void main() { expect(s.defaultExportDir, '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'); expect(s.exportAfterEveryEntry, true); expect(s.allowMissingValues, true); + expect(s.language, const Locale('de')); }); test('setting fields should notify listeners and change values', () async { @@ -156,8 +161,9 @@ void main() { s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; s.allowMissingValues = true; + s.language = const Locale('de'); - expect(i, 28); + expect(i, 29); }); }); @@ -199,6 +205,7 @@ void main() { expect(s.defaultExportDir.isEmpty, true); expect(s.exportAfterEveryEntry, false); expect(s.allowMissingValues, false); + expect(s.language, null); }); test('setting fields should save changes', () async { @@ -243,6 +250,7 @@ void main() { s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; s.allowMissingValues = true; + s.language = const Locale('de'); expect(s.displayDataStart, DateTime.fromMillisecondsSinceEpoch(10000)); expect(s.displayDataEnd, DateTime.fromMillisecondsSinceEpoch(200000)); @@ -270,7 +278,7 @@ void main() { expect(s.defaultExportDir, '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'); expect(s.exportAfterEveryEntry, true); expect(s.allowMissingValues, true); - + expect(s.language, const Locale('de')); }); test('setting fields should notify listeners and change values', () async { @@ -309,8 +317,9 @@ void main() { s.defaultExportDir = '/storage/emulated/0/Android/data/com.derdilla.bloodPressureApp/files/file.csv'; s.exportAfterEveryEntry = true; s.allowMissingValues = true; + s.language = const Locale('de'); - expect(i, 28); + expect(i, 29); }); }); } From e993f682eb4c0a97bffcc2b1a5cb5f4b6750c951 Mon Sep 17 00:00:00 2001 From: derdilla Date: Thu, 20 Jul 2023 16:21:49 +0200 Subject: [PATCH 3/3] replace unjustifiably large library --- lib/model/iso_lang_names.dart | 190 ++++++++++++++++++++++++++++++++++ lib/screens/settings.dart | 4 +- pubspec.lock | 8 -- pubspec.yaml | 1 - 4 files changed, 192 insertions(+), 11 deletions(-) create mode 100644 lib/model/iso_lang_names.dart diff --git a/lib/model/iso_lang_names.dart b/lib/model/iso_lang_names.dart new file mode 100644 index 00000000..9c9a0817 --- /dev/null +++ b/lib/model/iso_lang_names.dart @@ -0,0 +1,190 @@ +import 'package:flutter/material.dart'; + +final _isoLangs = { + "ab": "аҧсуа", + "aa": "Afaraf", + "af": "Afrikaans", + "ak": "Akan", + "sq": "Shqip", + "am": "አማርኛ", + "ar": "العربية", + "an": "Aragonés", + "hy": "Հայերեն", + "as": "অসমীয়া", + "av": "авар мацӀ, магӀарул мацӀ", + "ae": "avesta", + "ay": "aymar aru", + "az": "azərbaycan dili", + "bm": "bamanankan", + "ba": "башҡорт теле", + "eu": "euskara, euskera", + "be": "Беларуская", + "bn": "বাংলা", + "bh": "भोजपुरी", + "bi": "Bislama", + "bs": "bosanski jezik", + "br": "brezhoneg", + "bg": "български език", + "my": "ဗမာစာ", + "ca": "Català", + "ch": "Chamoru", + "ce": "нохчийн мотт", + "ny": "chiCheŵa, chinyanja", + "zh": "中文 (Zhōngwén)", + "cv": "чӑваш чӗлхи", + "kw": "Kernewek", + "co": "corsu, lingua corsa", + "cr": "ᓀᐦᐃᔭᐍᐏᐣ", + "hr": "hrvatski", + "cs": "česky, čeština", + "da": "dansk", + "dv": "ދިވެހި", + "nl": "Nederlands, Vlaams", + "en": "English", + "eo": "Esperanto", + "et": "eesti, eesti keel", + "ee": "Eʋegbe", + "fo": "føroyskt", + "fj": "vosa Vakaviti", + "fi": "suomi, suomen kieli", + "fr": "française", + "ff": "Fulfulde, Pulaar, Pular", + "gl": "Galego", + "ka": "ქართული", + "de": "Deutsch", + "el": "Ελληνικά", + "gn": "Avañeẽ", + "gu": "ગુજરાતી", + "ht": "Kreyòl ayisyen", + "ha": "Hausa, هَوُسَ", + "he": "עברית", + "hz": "Otjiherero", + "hi": "हिन्दी, हिंदी", + "ho": "Hiri Motu", + "hu": "Magyar", + "ia": "Interlingua", + "id": "Bahasa Indonesia", + "ie": "Occidental (Interlingue)", + "ga": "Gaeilge", + "ig": "Asụsụ Igbo", + "ik": "Iñupiaq, Iñupiatun", + "io": "Ido", + "is": "Íslenska", + "it": "Italiano", + "iu": "ᐃᓄᒃᑎᑐᑦ", + "ja": "日本語 (にほんご/にっぽんご)", + "jv": "basa Jawa", + "kl": "kalaallisut, kalaallit oqaasii", + "kn": "ಕನ್ನಡ", + "kr": "Kanuri", + "ks": "कश्मीरी, كشميري‎", + "kk": "Қазақ тілі", + "km": "ភាសាខ្មែរ", + "ki": "Gĩkũyũ", + "rw": "Ikinyarwanda", + "ky": "кыргыз тили", + "kv": "коми кыв", + "kg": "KiKongo", + "ko": "한국어 (韓國語), 조선말 (朝鮮語)", + "ku": "Kurdî, كوردی‎", + "kj": "Kuanyama", + "la": "latine, lingua latina", + "lb": "Lëtzebuergesch", + "lg": "Luganda", + "li": "Limburgs", + "ln": "Lingála", + "lo": "ພາສາລາວ", + "lt": "lietuvių kalba", + "lu": "", + "lv": "latviešu valoda", + "gv": "Gaelg, Gailck", + "mk": "македонски јазик", + "mg": "Malagasy fiteny", + "ms": "bahasa Melayu, بهاس ملايو‎", + "ml": "മലയാളം", + "mt": "Malti", + "mi": "te reo Māori", + "mr": "मराठी", + "mh": "Kajin M̧ajeļ", + "mn": "монгол", + "na": "Ekakairũ Naoero", + "nv": "Diné bizaad, Dinékʼehǰí", + "nb": "Norsk bokmål", + "nd": "isiNdebele", + "ne": "नेपाली", + "ng": "Owambo", + "nn": "Norsk nynorsk", + "no": "Norsk", + "ii": "ꆈꌠ꒿ Nuosuhxop", + "nr": "isiNdebele", + "oc": "Occitan", + "oj": "ᐊᓂᔑᓈᐯᒧᐎᓐ", + "cu": "ѩзыкъ словѣньскъ", + "om": "Afaan Oromoo", + "or": "ଓଡ଼ିଆ", + "os": "ирон æвзаг", + "pa": "ਪੰਜਾਬੀ, پنجابی‎", + "pi": "पाऴि", + "fa": "فارسی", + "pl": "polski", + "ps": "پښتو", + "pt": "Português", + "qu": "Runa Simi, Kichwa", + "rm": "rumantsch grischun", + "rn": "kiRundi", + "ro": "română", + "ru": "русский язык", + "sa": "संस्कृतम्", + "sc": "sardu", + "sd": "सिन्धी, سنڌي، سندھی‎", + "se": "Davvisámegiella", + "sm": "gagana faa Samoa", + "sg": "yângâ tî sängö", + "sr": "српски језик", + "gd": "Gàidhlig", + "sn": "chiShona", + "si": "සිංහල", + "sk": "slovenčina", + "sl": "slovenščina", + "so": "Soomaaliga, af Soomaali", + "st": "Sesotho", + "es": "español, castellano", + "su": "Basa Sunda", + "sw": "Kiswahili", + "ss": "SiSwati", + "sv": "svenska", + "ta": "தமிழ்", + "te": "తెలుగు", + "tg": "тоҷикӣ, toğikī, تاجیکی‎", + "th": "ไทย", + "ti": "ትግርኛ", + "bo": "བོད་ཡིག", + "tk": "Türkmen, Түркмен", + "tl": "Wikang Tagalog, ᜏᜒᜃᜅ᜔ ᜆᜄᜎᜓᜄ᜔", + "tn": "Setswana", + "to": "faka Tonga", + "tr": "Türkçe", + "ts": "Xitsonga", + "tt": "татарча, tatarça, تاتارچا‎", + "tw": "Twi", + "ty": "Reo Tahiti", + "ug": "Uyƣurqə, ئۇيغۇرچە‎", + "uk": "українська", + "ur": "اردو", + "uz": "zbek, Ўзбек, أۇزبېك‎", + "ve": "Tshivenḓa", + "vi": "Tiếng Việt", + "vo": "Volapük", + "wa": "Walon", + "cy": "Cymraeg", + "wo": "Wollof", + "fy": "Frysk", + "xh": "isiXhosa", + "yi": "ייִדיש", + "yo": "Yorùbá", + "za": "Saɯ cueŋƅ, Saw cuengh" +}; + +String? getDisplayLanguage(Locale l) { + return _isoLangs[l.languageCode]; +} diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index 541893a4..5af4a7f2 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -1,6 +1,7 @@ import 'package:blood_pressure_app/components/consistent_future_builder.dart'; import 'package:blood_pressure_app/components/settings_widgets.dart'; import 'package:blood_pressure_app/model/blood_pressure.dart'; +import 'package:blood_pressure_app/model/iso_lang_names.dart'; import 'package:blood_pressure_app/model/settings_store.dart'; import 'package:blood_pressure_app/screens/subsettings/enter_timeformat.dart'; import 'package:blood_pressure_app/screens/subsettings/export_import_screen.dart'; @@ -9,7 +10,6 @@ import 'package:blood_pressure_app/screens/subsettings/warn_about.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:locale_names/locale_names.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -88,7 +88,7 @@ class SettingsPage extends StatelessWidget { items: [ DropdownMenuItem(value: null, child: Text(AppLocalizations.of(context)!.system)), for (final l in AppLocalizations.supportedLocales) - DropdownMenuItem(value: l, child: Text(l.nativeDisplayLanguage ?? l.languageCode)), + DropdownMenuItem(value: l, child: Text(getDisplayLanguage(l) ?? l.languageCode)), ], onChanged: (Locale? value) { settings.language = value; diff --git a/pubspec.lock b/pubspec.lock index a8539517..06e7f580 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -381,14 +381,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" - locale_names: - dependency: "direct main" - description: - name: locale_names - sha256: b536de2e2e46b34a69687e637ca95cdb60fe931a27bb3ad66344c527ebe8b432 - url: "https://pub.dev" - source: hosted - version: "1.0.0" logging: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c4bc5db3..99e2c9e9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -49,7 +49,6 @@ dependencies: pdf: ^3.10.4 jsaver: ^1.2.0 package_info_plus: ^4.0.2 - locale_names: ^1.0.0 dev_dependencies: flutter_test: