Skip to content

Commit

Permalink
fix localization local mensage
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Aug 27, 2024
1 parent f37b927 commit 63c9154
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/src/localization/language_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ abstract class LanguageManager {
_globalTranslations[culture]![code] = value;
}

String translate(String key, [Map<String, String> parameters = const {}]) {
String translate(String key, {Map<String, String> parameters = const {}, String? defaultMessage}) {
final culture = currentLanguage.culture;
final translations = _globalTranslations[culture] ?? {};
var message = translations[key] ?? currentLanguage.getTranslation(key) ?? key;
var message = defaultMessage ?? translations[key] ?? currentLanguage.getTranslation(key) ?? key;
for (var key in parameters.keys) {
final value = parameters[key]!;
message = message.replaceAll('{$key}', value);
Expand Down
13 changes: 8 additions & 5 deletions lib/src/validations/equal_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ extension EqualValidation<T, E> on LucidValidationBuilder<T, E> {
if (value == comparison) return null;

final currentCode = code ?? Language.code.equalTo;
final currentMessage = message ??
LucidValidation.global.languageManager.translate(currentCode, {
'PropertyName': key,
'ComparisonValue': '$comparison',
});
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'ComparisonValue': '$comparison',
},
defaultMessage: message,
);

return ValidationError(message: currentMessage, code: currentCode);
},
Expand Down
13 changes: 8 additions & 5 deletions lib/src/validations/greater_than_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ extension GreaterThanValidation on SimpleValidationBuilder<num> {
if (value > minValue) return null;

final currentCode = code ?? Language.code.greaterThan;
final currentMessage = message ??
LucidValidation.global.languageManager.translate(currentCode, {
'PropertyName': key,
'ComparisonValue': '$minValue',
});
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'ComparisonValue': '$minValue',
},
defaultMessage: message,
);

return ValidationError(message: currentMessage, code: currentCode);
});
Expand Down
11 changes: 7 additions & 4 deletions lib/src/validations/is_empty_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ extension IsEmptyValidation on SimpleValidationBuilder<String> {
if (value.isEmpty) return null;

final currentCode = code ?? Language.code.isEmpty;
final currentMessage = message ??
LucidValidation.global.languageManager.translate(currentCode, {
'PropertyName': key,
});
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
},
defaultMessage: message,
);

return ValidationError(message: currentMessage, code: currentCode);
});
Expand Down
11 changes: 7 additions & 4 deletions lib/src/validations/is_not_null_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ extension IsNotNullValidation<T> on SimpleValidationBuilder<T?> {
if (value != null) return null;

final currentCode = code ?? Language.code.isNotNull;
final currentMessage = message ??
LucidValidation.global.languageManager.translate(currentCode, {
'PropertyName': key,
});
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
},
defaultMessage: message,
);

return ValidationError(message: currentMessage, code: currentCode);
},
Expand Down
18 changes: 18 additions & 0 deletions test/src/validations/equal_validation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ void main() {
final error = result.errors.first;
expect(error.message, r"'confirmPassword' must be equal to 'password'.");
});

test('equal validation with local message', () {
final validator = TestLucidValidator<UserModel>();
validator
.ruleFor((user) => user.confirmPassword, key: 'confirmPassword') //
.equalTo((user) => user.password, message: "Campo '{PropertyName}' deve ser igual ao campo '{ComparisonValue}'.");

final user = UserModel()
..password = 'password'
..confirmPassword = 'password2';
final result = validator.validate(user);

expect(result.isValid, false);
expect(result.errors.length, 1);

final error = result.errors.first;
expect(error.message, r"Campo 'confirmPassword' deve ser igual ao campo 'password'.");
});
}

0 comments on commit 63c9154

Please sign in to comment.