From 59730bbf4809bd883a8b9d4bd25539bc7abe0613 Mon Sep 17 00:00:00 2001 From: Jacob Moura Date: Tue, 27 Aug 2024 14:54:29 -0300 Subject: [PATCH] Update doc --- CHANGELOG.md | 4 ++ README.md | 55 +++++++++++++++++++ .../domain/validations/language_manager.dart | 10 ++++ .../register_param_validation.dart | 2 +- example/lib/main.dart | 4 ++ .../register_page/register_page.dart | 22 ++++++++ lib/src/localization/language_manager.dart | 40 +++++++++++++- .../languages/spanish_language.dart | 30 ++++++++++ pubspec.yaml | 6 +- 9 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 example/lib/domain/validations/language_manager.dart create mode 100644 lib/src/localization/languages/spanish_language.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index e99b64f..99ec8f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.0 + +* Production release + ## 0.0.7 * Added `when` and `setValidator` diff --git a/README.md b/README.md index d315550..49996cb 100644 --- a/README.md +++ b/README.md @@ -316,7 +316,62 @@ LucidValidation.global.languageManager = CustomLanguageManager(); ``` +## Flutter Configuration +Podemos criar um `Delegate` para automatizar a internacionalização diretamente no Flutter. + +Para criar um `Delegate` siga esses passos: + +```dart +class LucidLocalizationDelegate extends LocalizationsDelegate { + const LucidLocalizationDelegate(); + + static final delegate = LucidLocalizationDelegate(); + + @override + bool isSupported(Locale locale) { + return LucidValidation.global.languageManager.isSupported( + locale.languageCode, + locale.countryCode, + ); + } + + @override + Future load(Locale locale) async { + print(locale); + final culture = Culture(locale.languageCode, locale.countryCode ?? ''); + LucidValidation.global.culture = culture; + return culture; + } + + @override + bool shouldReload(LocalizationsDelegate old) { + return true; + } +} +``` + +Agora basta adicionar no `MaterialApp` ou `CupertinoApp`: + +```dart + @override + Widget build(BuildContext context) { + return MaterialApp( + supportedLocales: const [ + Locale('en', 'US'), + Locale('pt', 'BR'), + ], + localizationsDelegates: [ + LucidLocalizationDelegate.delegate, + // + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + + ], + ... + ); + } +``` ## Creating Custom Rules diff --git a/example/lib/domain/validations/language_manager.dart b/example/lib/domain/validations/language_manager.dart new file mode 100644 index 0000000..09a7722 --- /dev/null +++ b/example/lib/domain/validations/language_manager.dart @@ -0,0 +1,10 @@ +import 'package:lucid_validation/lucid_validation.dart'; + +class CustomLanguageManager extends LanguageManager { + CustomLanguageManager() { + addTranslation(Culture('pt', 'BR'), 'passwordEqualTo', "'{PropertyName}' deve ser igual."); + addTranslation(Culture('pt'), 'passwordEqualTo', "'{PropertyName}' deve ser igual."); + addTranslation(Culture('en', 'US'), 'passwordEqualTo', "'{PropertyName}' must be equal."); + addTranslation(Culture('en'), 'passwordEqualTo', "'{PropertyName}' must be equal."); + } +} diff --git a/example/lib/domain/validations/register_param_validation.dart b/example/lib/domain/validations/register_param_validation.dart index 6805edc..f698da9 100644 --- a/example/lib/domain/validations/register_param_validation.dart +++ b/example/lib/domain/validations/register_param_validation.dart @@ -13,7 +13,7 @@ class RegisterParamValidation extends LucidValidator { ruleFor((registerParamDto) => registerParamDto.confirmPassword, key: 'confirmPassword') // .customValidPassword() - .equalTo((registerParamDto) => registerParamDto.password); + .equalTo((registerParamDto) => registerParamDto.password, code: 'passwordEqualTo'); ruleFor((registerParamDto) => registerParamDto.phone, key: 'phone') // .customValidPhone(); diff --git a/example/lib/main.dart b/example/lib/main.dart index 2f2d2da..70e5e43 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -3,7 +3,11 @@ import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:lucid_validation/lucid_validation.dart'; +import 'domain/validations/language_manager.dart'; + void main() { + LucidValidation.global.languageManager = CustomLanguageManager(); + runApp(const MyApp()); } diff --git a/example/lib/presentation/register_page/register_page.dart b/example/lib/presentation/register_page/register_page.dart index aa03b8b..467626c 100644 --- a/example/lib/presentation/register_page/register_page.dart +++ b/example/lib/presentation/register_page/register_page.dart @@ -1,5 +1,6 @@ import 'package:example/domain/dtos/register_param_dto.dart'; import 'package:example/domain/validations/register_param_validation.dart'; +import 'package:example/main.dart'; import 'package:flutter/material.dart'; class RegisterPage extends StatefulWidget { @@ -53,6 +54,27 @@ class _RegisterPageState extends State { @override Widget build(BuildContext context) { return Scaffold( + appBar: AppBar( + title: const Text('Login'), + actions: [ + Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text('Is English'), + ValueListenableBuilder( + valueListenable: globalLocale, + builder: (context, _, __) { + return Switch( + value: globalLocale.value.languageCode == 'en', + onChanged: (value) { + globalLocale.value = value ? Locale('en', 'US') : Locale('pt', 'BR'); + }, + ); + }), + ], + ) + ], + ), body: Padding( padding: const EdgeInsets.all(12.0), child: Column( diff --git a/lib/src/localization/language_manager.dart b/lib/src/localization/language_manager.dart index 53dcd2a..1a6d227 100644 --- a/lib/src/localization/language_manager.dart +++ b/lib/src/localization/language_manager.dart @@ -1,11 +1,49 @@ import '../../lucid_validation.dart'; import 'languages/portuguese_brazillian_language.dart'; +import 'languages/spanish_language.dart'; final _avaliableLanguages = { - Culture('pt', 'BR'): PortugueseBrasillianLanguage(), Culture('pt'): PortugueseBrasillianLanguage(), + Culture('pt', 'BR'): PortugueseBrasillianLanguage(), Culture('en'): EnglishLanguage(), Culture('en', 'US'): EnglishLanguage(), + Culture('en', 'UA'): EnglishLanguage(), + Culture('en', 'NZ'): EnglishLanguage(), + Culture('en', 'ZA'): EnglishLanguage(), + Culture('en', 'GB'): EnglishLanguage(), + Culture('en', 'CA'): EnglishLanguage(), + Culture('en', 'AU'): EnglishLanguage(), + Culture('en', 'IE'): EnglishLanguage(), + Culture('en', 'IN'): EnglishLanguage(), + Culture('en', 'SG'): EnglishLanguage(), + Culture('en', 'PH'): EnglishLanguage(), + Culture('en', 'MY'): EnglishLanguage(), + Culture('en', 'PK'): EnglishLanguage(), + Culture('en', 'NG'): EnglishLanguage(), + Culture('en', 'KE'): EnglishLanguage(), + Culture('en', 'GH'): EnglishLanguage(), + Culture('en', 'UG'): EnglishLanguage(), + Culture('en', 'TT'): EnglishLanguage(), + Culture('es'): SpanishLanguage(), + Culture('es', 'ES'): SpanishLanguage(), + Culture('es', 'MX'): SpanishLanguage(), + Culture('es', 'AR'): SpanishLanguage(), + Culture('es', 'CR'): SpanishLanguage(), + Culture('es', 'DO'): SpanishLanguage(), + Culture('es', 'CO'): SpanishLanguage(), + Culture('es', 'CL'): SpanishLanguage(), + Culture('es', 'PY'): SpanishLanguage(), + Culture('es', 'SV'): SpanishLanguage(), + Culture('es', 'NI'): SpanishLanguage(), + Culture('es', 'GT'): SpanishLanguage(), + Culture('es', 'PA'): SpanishLanguage(), + Culture('es', 'VE'): SpanishLanguage(), + Culture('es', 'PE'): SpanishLanguage(), + Culture('es', 'EC'): SpanishLanguage(), + Culture('es', 'UY'): SpanishLanguage(), + Culture('es', 'PO'): SpanishLanguage(), + Culture('es', 'HN'): SpanishLanguage(), + Culture('es', 'PR'): SpanishLanguage(), }; abstract class LanguageManager { diff --git a/lib/src/localization/languages/spanish_language.dart b/lib/src/localization/languages/spanish_language.dart new file mode 100644 index 0000000..82749fa --- /dev/null +++ b/lib/src/localization/languages/spanish_language.dart @@ -0,0 +1,30 @@ +import '../language.dart'; + +class SpanishLanguage extends Language { + SpanishLanguage() + : super({ + Language.code.equalTo: "'{PropertyName}' debe ser igual a '{ComparisonValue}'.", + Language.code.greaterThan: "'{PropertyName}' debe ser mayor que '{ComparisonValue}'.", + Language.code.isEmpty: "'{PropertyName}' debe estar vacío.", + Language.code.isNotNull: "'{PropertyName}' no puede ser nulo.", + Language.code.isNull: "'{PropertyName}' debe ser nulo.", + Language.code.lessThan: "'{PropertyName}' debe ser menor que '{ComparisonValue}'.", + Language.code.matchesPattern: "'{PropertyName}' no tiene el formato correcto.", + Language.code.max: "'{PropertyName}' debe ser menor o igual a {MaxValue}. Has ingresado {PropertyValue}.", + Language.code.maxLength: "El tamaño de '{PropertyName}' debe ser de {MaxLength} caracteres o menos. Has ingresado {TotalLength} caracteres.", + Language.code.min: "'{PropertyName}' debe ser mayor o igual a {MinValue}. Has ingresado {PropertyValue}.", + Language.code.minLength: "El tamaño de '{PropertyName}' debe ser de al menos {MinLength} caracteres. Has ingresado {TotalLength} caracteres.", + Language.code.mustHaveLowercase: "'{PropertyName}' debe tener al menos una letra minúscula.", + Language.code.mustHaveNumber: "'{PropertyName}' debe tener al menos un dígito ('0'-'9').", + Language.code.mustHaveSpecialCharacter: "'{PropertyName}' debe tener al menos un carácter no alfanumérico.", + Language.code.mustHaveUppercase: "'{PropertyName}' debe tener al menos una letra mayúscula.", + Language.code.notEmpty: "'{PropertyName}' no puede estar vacío.", + Language.code.notEqualTo: "'{PropertyName}' no puede ser igual a '{ComparisonValue}'.", + Language.code.range: "'{PropertyName}' debe estar entre {From} y {To}. Has ingresado {PropertyValue}.", + Language.code.validCEP: "'{PropertyName}' no es un CEP válido.", + Language.code.validCPF: "'{PropertyName}' no es un CPF válido.", + Language.code.validCNPJ: "'{PropertyName}' no es un CNPJ válido.", + Language.code.validCreditCard: "'{PropertyName}' no es un número de tarjeta de crédito válido.", + Language.code.validEmail: "'{PropertyName}' no es una dirección de correo electrónico válida.", + }); +} diff --git a/pubspec.yaml b/pubspec.yaml index 6f88ecf..0d9eb0b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: "A Dart/Flutter package for building strongly typed validation rule repository: https://github.com/Flutterando/lucid_validation homepage: https://pub.dev/documentation/lucid_validation/latest/ -version: 0.0.7 +version: 1.0.0 environment: sdk: ">=3.0.0 <4.0.0" @@ -12,5 +12,5 @@ environment: dev_dependencies: - lints: ^3.0.0 - test: ^1.25.8 + lints: ">=3.0.0 <4.0.0" + test: ">=1.20.0 <2.0.0"