Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Aug 27, 2024
1 parent 3131bc6 commit 59730bb
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0

* Production release

## 0.0.7

* Added `when` and `setValidator`
Expand Down
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Culture> {
const LucidLocalizationDelegate();
static final delegate = LucidLocalizationDelegate();
@override
bool isSupported(Locale locale) {
return LucidValidation.global.languageManager.isSupported(
locale.languageCode,
locale.countryCode,
);
}
@override
Future<Culture> load(Locale locale) async {
print(locale);
final culture = Culture(locale.languageCode, locale.countryCode ?? '');
LucidValidation.global.culture = culture;
return culture;
}
@override
bool shouldReload(LocalizationsDelegate<Culture> 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
Expand Down
10 changes: 10 additions & 0 deletions example/lib/domain/validations/language_manager.dart
Original file line number Diff line number Diff line change
@@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RegisterParamValidation extends LucidValidator<RegisterParamDto> {

ruleFor((registerParamDto) => registerParamDto.confirmPassword, key: 'confirmPassword') //
.customValidPassword()
.equalTo((registerParamDto) => registerParamDto.password);
.equalTo((registerParamDto) => registerParamDto.password, code: 'passwordEqualTo');

ruleFor((registerParamDto) => registerParamDto.phone, key: 'phone') //
.customValidPhone();
Expand Down
4 changes: 4 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
22 changes: 22 additions & 0 deletions example/lib/presentation/register_page/register_page.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -53,6 +54,27 @@ class _RegisterPageState extends State<RegisterPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
actions: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('Is English'),
ValueListenableBuilder<Locale>(
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(
Expand Down
40 changes: 39 additions & 1 deletion lib/src/localization/language_manager.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
import '../../lucid_validation.dart';
import 'languages/portuguese_brazillian_language.dart';
import 'languages/spanish_language.dart';

final _avaliableLanguages = <Culture, Language>{
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 {
Expand Down
30 changes: 30 additions & 0 deletions lib/src/localization/languages/spanish_language.dart
Original file line number Diff line number Diff line change
@@ -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.",
});
}
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

0 comments on commit 59730bb

Please sign in to comment.