From 3dfb331de947d0e67e324eddd9953052a84a2743 Mon Sep 17 00:00:00 2001 From: Karol Stolarczyk Date: Wed, 17 Jul 2024 11:46:50 +0200 Subject: [PATCH] fixes review/13 --- analysis_options.yaml | 3 +- lib/ i18n/strings.g.dart | 314 ++++++++++++++------------------- lib/ i18n/strings.i18n.json | 40 +++-- lib/ i18n/strings_en.i18n.json | 44 ++--- lib/barcode_item.dart | 18 +- lib/barcodes_panel.dart | 12 +- lib/main.dart | 2 +- 7 files changed, 200 insertions(+), 233 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 0d29021..28af58c 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -25,4 +25,5 @@ linter: # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule # Additional information about this file can be found at -# https://dart.dev/guides/language/analysis-options +# https://dart.dev/guides/language/analysis-option + \ No newline at end of file diff --git a/lib/ i18n/strings.g.dart b/lib/ i18n/strings.g.dart index 1477d19..9c8adac 100644 --- a/lib/ i18n/strings.g.dart +++ b/lib/ i18n/strings.g.dart @@ -6,7 +6,7 @@ /// Locales: 1 /// Strings: 19 /// -/// Built on 2024-07-16 at 09:22 UTC +/// Built on 2024-07-17 at 09:30 UTC // coverage:ignore-file // ignore_for_file: type=lint @@ -25,26 +25,17 @@ const AppLocale _baseLocale = AppLocale.en; /// - Locale locale = AppLocale.en.flutterLocale // get flutter locale from enum /// - if (LocaleSettings.currentLocale == AppLocale.en) // locale check enum AppLocale with BaseAppLocale { - en(languageCode: 'en', build: Translations.build); - - const AppLocale( - {required this.languageCode, - this.scriptCode, - this.countryCode, - required this.build}); // ignore: unused_element - - @override - final String languageCode; - @override - final String? scriptCode; - @override - final String? countryCode; - @override - final TranslationBuilder build; - - /// Gets current instance managed by [LocaleSettings]. - Translations get translations => - LocaleSettings.instance.translationMap[this]!; + en(languageCode: 'en', build: Translations.build); + + const AppLocale({required this.languageCode, this.scriptCode, this.countryCode, required this.build}); // ignore: unused_element + + @override final String languageCode; + @override final String? scriptCode; + @override final String? countryCode; + @override final TranslationBuilder build; + + /// Gets current instance managed by [LocaleSettings]. + Translations get translations => LocaleSettings.instance.translationMap[this]!; } /// Method A: Simple @@ -73,14 +64,10 @@ Translations get t => LocaleSettings.instance.currentTranslations; /// final t = Translations.of(context); // Get t variable. /// String a = t.someKey.anotherKey; // Use t variable. /// String b = t['someKey.anotherKey']; // Only for edge cases! -class TranslationProvider - extends BaseTranslationProvider { - TranslationProvider({required super.child}) - : super(settings: LocaleSettings.instance); - - static InheritedLocaleData of( - BuildContext context) => - InheritedLocaleData.of(context); +class TranslationProvider extends BaseTranslationProvider { + TranslationProvider({required super.child}) : super(settings: LocaleSettings.instance); + + static InheritedLocaleData of(BuildContext context) => InheritedLocaleData.of(context); } /// Method B shorthand via [BuildContext] extension method. @@ -89,171 +76,144 @@ class TranslationProvider /// Usage (e.g. in a widget's build method): /// context.t.someKey.anotherKey extension BuildContextTranslationsExtension on BuildContext { - Translations get t => TranslationProvider.of(this).translations; + Translations get t => TranslationProvider.of(this).translations; } /// Manages all translation instances and the current locale -class LocaleSettings - extends BaseFlutterLocaleSettings { - LocaleSettings._() : super(utils: AppLocaleUtils.instance); - - static final instance = LocaleSettings._(); - - // static aliases (checkout base methods for documentation) - static AppLocale get currentLocale => instance.currentLocale; - static Stream getLocaleStream() => instance.getLocaleStream(); - static AppLocale setLocale(AppLocale locale, - {bool? listenToDeviceLocale = false}) => - instance.setLocale(locale, listenToDeviceLocale: listenToDeviceLocale); - static AppLocale setLocaleRaw(String rawLocale, - {bool? listenToDeviceLocale = false}) => - instance.setLocaleRaw(rawLocale, - listenToDeviceLocale: listenToDeviceLocale); - static AppLocale useDeviceLocale() => instance.useDeviceLocale(); - @Deprecated('Use [AppLocaleUtils.supportedLocales]') - static List get supportedLocales => instance.supportedLocales; - @Deprecated('Use [AppLocaleUtils.supportedLocalesRaw]') - static List get supportedLocalesRaw => instance.supportedLocalesRaw; - static void setPluralResolver( - {String? language, - AppLocale? locale, - PluralResolver? cardinalResolver, - PluralResolver? ordinalResolver}) => - instance.setPluralResolver( - language: language, - locale: locale, - cardinalResolver: cardinalResolver, - ordinalResolver: ordinalResolver, - ); +class LocaleSettings extends BaseFlutterLocaleSettings { + LocaleSettings._() : super(utils: AppLocaleUtils.instance); + + static final instance = LocaleSettings._(); + + // static aliases (checkout base methods for documentation) + static AppLocale get currentLocale => instance.currentLocale; + static Stream getLocaleStream() => instance.getLocaleStream(); + static AppLocale setLocale(AppLocale locale, {bool? listenToDeviceLocale = false}) => instance.setLocale(locale, listenToDeviceLocale: listenToDeviceLocale); + static AppLocale setLocaleRaw(String rawLocale, {bool? listenToDeviceLocale = false}) => instance.setLocaleRaw(rawLocale, listenToDeviceLocale: listenToDeviceLocale); + static AppLocale useDeviceLocale() => instance.useDeviceLocale(); + @Deprecated('Use [AppLocaleUtils.supportedLocales]') static List get supportedLocales => instance.supportedLocales; + @Deprecated('Use [AppLocaleUtils.supportedLocalesRaw]') static List get supportedLocalesRaw => instance.supportedLocalesRaw; + static void setPluralResolver({String? language, AppLocale? locale, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver}) => instance.setPluralResolver( + language: language, + locale: locale, + cardinalResolver: cardinalResolver, + ordinalResolver: ordinalResolver, + ); } /// Provides utility functions without any side effects. class AppLocaleUtils extends BaseAppLocaleUtils { - AppLocaleUtils._() - : super(baseLocale: _baseLocale, locales: AppLocale.values); - - static final instance = AppLocaleUtils._(); - - // static aliases (checkout base methods for documentation) - static AppLocale parse(String rawLocale) => instance.parse(rawLocale); - static AppLocale parseLocaleParts( - {required String languageCode, - String? scriptCode, - String? countryCode}) => - instance.parseLocaleParts( - languageCode: languageCode, - scriptCode: scriptCode, - countryCode: countryCode); - static AppLocale findDeviceLocale() => instance.findDeviceLocale(); - static List get supportedLocales => instance.supportedLocales; - static List get supportedLocalesRaw => instance.supportedLocalesRaw; + AppLocaleUtils._() : super(baseLocale: _baseLocale, locales: AppLocale.values); + + static final instance = AppLocaleUtils._(); + + // static aliases (checkout base methods for documentation) + static AppLocale parse(String rawLocale) => instance.parse(rawLocale); + static AppLocale parseLocaleParts({required String languageCode, String? scriptCode, String? countryCode}) => instance.parseLocaleParts(languageCode: languageCode, scriptCode: scriptCode, countryCode: countryCode); + static AppLocale findDeviceLocale() => instance.findDeviceLocale(); + static List get supportedLocales => instance.supportedLocales; + static List get supportedLocalesRaw => instance.supportedLocalesRaw; } // translations // Path: class Translations implements BaseTranslations { - /// Returns the current translations of the given [context]. - /// - /// Usage: - /// final t = Translations.of(context); - static Translations of(BuildContext context) => - InheritedLocaleData.of(context).translations; - - /// You can call this constructor and build your own translation instance of this locale. - /// Constructing via the enum [AppLocale.build] is preferred. - Translations.build( - {Map? overrides, - PluralResolver? cardinalResolver, - PluralResolver? ordinalResolver}) - : assert(overrides == null, - 'Set "translation_overrides: true" in order to enable this feature.'), - $meta = TranslationMetadata( - locale: AppLocale.en, - overrides: overrides ?? {}, - cardinalResolver: cardinalResolver, - ordinalResolver: ordinalResolver, - ) { - $meta.setFlatMapFunction(_flatMapFunction); - } - - /// Metadata for the translations of . - @override - final TranslationMetadata $meta; - - /// Access flat map - dynamic operator [](String key) => $meta.getTranslation(key); - - late final Translations _root = this; // ignore: unused_field - - // Translations - String get app_title => 'poly test codes'; - String get title1 => 'Barcode List'; - String get opis => 'Description'; - String get kod => 'Barcode'; - String get kodplus => 'Add Barcode'; - String get typ => 'Code Type'; - String get error1 => 'Description cannot be empty'; - String get error2 => 'Barcode cannot be empty'; - String get error3 => 'Invalid barcode'; - String get codetype => 'Code type'; - String get dsc1 => 'Company not verified'; - String get dsc2 => 'Company verified with full scores'; - String get dsc3 => 'Company verified with incomplete scores'; - String get dsc4 => 'Internal Code'; - String get dsc5 => 'Company registered outside Poland'; - String get dsc6 => 'Company registered in...'; - String get dsc7 => 'Lidl\'s own brand'; - String get dsc8 => 'Pola\'s Friend'; - String get dsc9 => 'Extended company description'; + /// Returns the current translations of the given [context]. + /// + /// Usage: + /// final t = Translations.of(context); + static Translations of(BuildContext context) => InheritedLocaleData.of(context).translations; + + /// You can call this constructor and build your own translation instance of this locale. + /// Constructing via the enum [AppLocale.build] is preferred. + Translations.build({Map? overrides, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver}) + : assert(overrides == null, 'Set "translation_overrides: true" in order to enable this feature.'), + $meta = TranslationMetadata( + locale: AppLocale.en, + overrides: overrides ?? {}, + cardinalResolver: cardinalResolver, + ordinalResolver: ordinalResolver, + ) { + $meta.setFlatMapFunction(_flatMapFunction); + } + + /// Metadata for the translations of . + @override final TranslationMetadata $meta; + + /// Access flat map + dynamic operator[](String key) => $meta.getTranslation(key); + + late final Translations _root = this; // ignore: unused_field + + // Translations + String get appTitle => 'poly test codes'; + String get title1 => 'Barcode List'; + String get description => 'Description'; + String get code => 'Barcode'; + String get addCode => 'Add Barcode'; + String get type => 'Code Type'; + late final _StringsErrorEn error = _StringsErrorEn._(_root); + String get codetype => 'Code type'; + late final _StringsBarcodesDescriptionEn barcodesDescription = _StringsBarcodesDescriptionEn._(_root); +} + +// Path: error +class _StringsErrorEn { + _StringsErrorEn._(this._root); + + final Translations _root; // ignore: unused_field + + // Translations + String get emptyDescription => 'Description cannot be empty'; + String get emptyCode => 'Barcode cannot be empty'; + String get invalidCode => 'Invalid barcode'; +} + +// Path: barcodesDescription +class _StringsBarcodesDescriptionEn { + _StringsBarcodesDescriptionEn._(this._root); + + final Translations _root; // ignore: unused_field + + // Translations + String get companyNotVerified => 'Company not verified'; + String get companyVerifiedWithFullScores => 'Company verified with full scores'; + String get companyVerifiedWithIncompleteScores => 'Company verified with incomplete scores'; + String get internalCode => 'Internal Code'; + String get companyRegisteredOutsidePoland => 'Company registered outside Poland'; + String get companyRegisteredIn => 'Company registered in...'; + String get lidlOwnBrand => 'Lidl\'s own brand'; + String get polaFriend => 'Pola\'s Friend'; + String get extendedCompanyDescription => 'Extended company description'; } /// Flat map(s) containing all translations. /// Only for edge cases! For simple maps, use the map function of this library. extension on Translations { - dynamic _flatMapFunction(String path) { - switch (path) { - case 'app_title': - return 'poly test codes'; - case 'title1': - return 'Barcode List'; - case 'opis': - return 'Description'; - case 'kod': - return 'Barcode'; - case 'kodplus': - return 'Add Barcode'; - case 'typ': - return 'Code Type'; - case 'error1': - return 'Description cannot be empty'; - case 'error2': - return 'Barcode cannot be empty'; - case 'error3': - return 'Invalid barcode'; - case 'codetype': - return 'Code type'; - case 'dsc1': - return 'Company not verified'; - case 'dsc2': - return 'Company verified with full scores'; - case 'dsc3': - return 'Company verified with incomplete scores'; - case 'dsc4': - return 'Internal Code'; - case 'dsc5': - return 'Company registered outside Poland'; - case 'dsc6': - return 'Company registered in...'; - case 'dsc7': - return 'Lidl\'s own brand'; - case 'dsc8': - return 'Pola\'s Friend'; - case 'dsc9': - return 'Extended company description'; - default: - return null; - } - } + dynamic _flatMapFunction(String path) { + switch (path) { + case 'appTitle': return 'poly test codes'; + case 'title1': return 'Barcode List'; + case 'description': return 'Description'; + case 'code': return 'Barcode'; + case 'addCode': return 'Add Barcode'; + case 'type': return 'Code Type'; + case 'error.emptyDescription': return 'Description cannot be empty'; + case 'error.emptyCode': return 'Barcode cannot be empty'; + case 'error.invalidCode': return 'Invalid barcode'; + case 'codetype': return 'Code type'; + case 'barcodesDescription.companyNotVerified': return 'Company not verified'; + case 'barcodesDescription.companyVerifiedWithFullScores': return 'Company verified with full scores'; + case 'barcodesDescription.companyVerifiedWithIncompleteScores': return 'Company verified with incomplete scores'; + case 'barcodesDescription.internalCode': return 'Internal Code'; + case 'barcodesDescription.companyRegisteredOutsidePoland': return 'Company registered outside Poland'; + case 'barcodesDescription.companyRegisteredIn': return 'Company registered in...'; + case 'barcodesDescription.lidlOwnBrand': return 'Lidl\'s own brand'; + case 'barcodesDescription.polaFriend': return 'Pola\'s Friend'; + case 'barcodesDescription.extendedCompanyDescription': return 'Extended company description'; + default: return null; + } + } } diff --git a/lib/ i18n/strings.i18n.json b/lib/ i18n/strings.i18n.json index 09f22c0..ddc3575 100644 --- a/lib/ i18n/strings.i18n.json +++ b/lib/ i18n/strings.i18n.json @@ -1,23 +1,25 @@ { - "app_title": "Kody testowe poli", + "appTitle": "Kody testowe poli", "title1": "Lista Kodów Kreskowych", - "opis": "Opis", - "kod": "Kod kreskowy", - "kodplus": "Dodaj kod kreskowy", - "typ": "Typ kodu", - "error1": "Opis nie może być pusty", - "error2": "Kod kreskowy nie może być pusty", - "error3": "Nieprawidłowy kod kreskowy", - "codetype": "Typ kodu", + "description": "Opis", + "code": "Kod kreskowy", + "addCode": "Dodaj kod kreskowy", + "type": "Typ kodu", + "error": { + "emptyDescription": "Opis nie może być pusty", + "emptoCode": "Kod kreskowy nie może być pusty", + "invalidCode": "Nieprawidłowy kod kreskowy" + }, + "codeType": "Typ kodu", "barcodesDescription": { - "dsc1": "Firma nie zweryfikowana", - "dsc2": "Firma zweryfikowana z pełną punktacją", - "dsc3": "Firma zweryfikowana z niepełną punktacją", - "dsc4": "Kod wewnętrzny", - "dsc5": "Firma zarejestrowana poza Polską", - "dsc6": "Firma zarejerstrowana w ...", - "dsc7": "Marka własna Lidla", - "dsc8": "Przyjaciel Poli", - "dsc9": "Rozszerzony opis firmy" + "companyNotVerified": "Firma nie zweryfikowana", + "companyVerifiedWithFullScores": "Firma zweryfikowana z pełną punktacją", + "companyVerifiedWithIncompleteScores": "Firma zweryfikowana z niepełną punktacją", + "internalCode": "Kod wewnętrzny", + "companyRegisteredOutsidePoland": "Firma zarejestrowana poza Polską", + "companyRegisteredIn": "Firma zarejerstrowana w ...", + "lidlOwnBrand": "Marka własna Lidla", + "polaFriend": "Przyjaciel Poli", + "extendedCompanyDescription": "Rozszerzony opis firmy" + } } -} \ No newline at end of file diff --git a/lib/ i18n/strings_en.i18n.json b/lib/ i18n/strings_en.i18n.json index b730be1..e7e4e7a 100644 --- a/lib/ i18n/strings_en.i18n.json +++ b/lib/ i18n/strings_en.i18n.json @@ -1,21 +1,25 @@ { -"app_title": "poly test codes", -"title1": "Barcode List", -"opis": "Description", -"kod": "Barcode", -"kodplus": "Add Barcode", -"typ": "Code Type", -"error1": "Description cannot be empty", -"error2": "Barcode cannot be empty", -"error3": "Invalid barcode", -"codetype": "Code type", -"dsc1": "Company not verified", -"dsc2": "Company verified with full scores", -"dsc3": "Company verified with incomplete scores", -"dsc4": "Internal Code", -"dsc5": "Company registered outside Poland", -"dsc6": "Company registered in...", -"dsc7": "Lidl's own brand", -"dsc8": "Pola's Friend", -"dsc9": "Extended company description" -} \ No newline at end of file + "appTitle": "poly test codes", + "title1": "Barcode List", + "description": "Description", + "code": "Barcode", + "addCode": "Add Barcode", + "type": "Code Type", + "error": { + "emptyDescription": "Description cannot be empty", + "emptyCode": "Barcode cannot be empty", + "invalidCode": "Invalid barcode" + }, + "codetype": "Code type", + "barcodesDescription": { + "companyNotVerified": "Company not verified", + "companyVerifiedWithFullScores": "Company verified with full scores", + "companyVerifiedWithIncompleteScores": "Company verified with incomplete scores", + "internalCode": "Internal Code", + "companyRegisteredOutsidePoland": "Company registered outside Poland", + "companyRegisteredIn": "Company registered in...", + "lidlOwnBrand": "Lidl's own brand", + "polaFriend": "Pola's Friend", + "extendedCompanyDescription": "Extended company description" + } +} diff --git a/lib/barcode_item.dart b/lib/barcode_item.dart index b6c00c8..2870d30 100644 --- a/lib/barcode_item.dart +++ b/lib/barcode_item.dart @@ -15,39 +15,39 @@ class BarcodeItem { static List barcodes(Translations translations) { return [ BarcodeItem( - description: translations.dsc1, + description: translations.barcodesDescription.companyNotVerified, data: "5905499300707", type: Barcode.ean13()), BarcodeItem( - description: translations.dsc2, + description: translations.barcodesDescription.companyVerifiedWithFullScores, data: "5907632637572", type: Barcode.ean13()), BarcodeItem( - description: translations.dsc3, + description: translations.barcodesDescription.companyVerifiedWithIncompleteScores, data: "5900497025454", type: Barcode.ean13()), BarcodeItem( - description: translations.dsc4, + description: translations.barcodesDescription.internalCode, data: "00000000", type: Barcode.ean8()), BarcodeItem( - description: translations.dsc5, + description: translations.barcodesDescription.companyRegisteredOutsidePoland, data: "8680861069075", type: Barcode.ean13()), BarcodeItem( - description: translations.dsc6, + description: translations.barcodesDescription.companyRegisteredIn, data: "5090000000006", type: Barcode.ean13()), BarcodeItem( - description: translations.dsc7, + description: translations.barcodesDescription.lidlOwnBrand, data: "20982515", type: Barcode.ean8()), BarcodeItem( - description: translations.dsc8, + description: translations.barcodesDescription.polaFriend, data: "5906395053018", type: Barcode.ean13()), BarcodeItem( - description: translations.dsc9, + description: translations.barcodesDescription.extendedCompanyDescription, data: "9771644705002", type: Barcode.ean13()), ]; diff --git a/lib/barcodes_panel.dart b/lib/barcodes_panel.dart index 7c60782..4e8fb67 100644 --- a/lib/barcodes_panel.dart +++ b/lib/barcodes_panel.dart @@ -38,8 +38,8 @@ class BarcodesPanelState extends State { setState(() { _descriptionError = - description.isEmpty ? Translations.of(context).error1 : null; - _dataError = data.isEmpty ? Translations.of(context).error2 : null; + description.isEmpty ? Translations.of(context).error.emptyDescription : null; + _dataError = data.isEmpty ? Translations.of(context).error.emptyCode : null; if (_descriptionError == null && _dataError == null) { if (barcodeType.barcode.isValid(data)) { @@ -47,7 +47,7 @@ class BarcodesPanelState extends State { _descriptionController.clear(); _dataController.clear(); } else { - _dataError = Translations.of(context).error3; + _dataError = Translations.of(context).error.invalidCode; } } }); @@ -119,11 +119,11 @@ class BarcodesPanelState extends State { Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ - _textField(_descriptionController, Translations.of(context).opis, + _textField(_descriptionController, Translations.of(context).description, _descriptionError), const SizedBox(width: Constants.fieldSpacing), _textField( - _dataController, Translations.of(context).kod, _dataError), + _dataController, Translations.of(context).code, _dataError), const SizedBox(width: Constants.fieldSpacing), _dropdownField(context), const SizedBox(width: Constants.fieldSpacing), @@ -131,7 +131,7 @@ class BarcodesPanelState extends State { padding: const EdgeInsets.only(top: Constants.errorHeight), child: ElevatedButton( onPressed: _handleAddBarcode, - child: Text(Translations.of(context).kodplus), + child: Text(Translations.of(context).addCode), ), ), ], diff --git a/lib/main.dart b/lib/main.dart index c63e4d0..079204d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -15,7 +15,7 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: (Translations.of(context).app_title), + title: (Translations.of(context).appTitle), home: BarcodesPage( barcodes: BarcodeItem.barcodes(Translations.of(context))), );