mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
i18n parameterized validation error messages
This commit is contained in:
@@ -63,7 +63,7 @@ public class ValidationErrorListCell extends ListCell<ValidationError> {
|
||||
|
||||
private void addContent(ValidationError validationError) {
|
||||
setText(null);
|
||||
Text text = new Text(getI18nValidatioMessage(resourceBundle, validationError.getMessages()));
|
||||
Text text = new Text(getI18nValidatioMessage(resourceBundle, validationError));
|
||||
text.setWrappingWidth(180);
|
||||
setGraphic(text);
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ public class EditableValidationCell extends TableCell<CSVRow, CSVValue> {
|
||||
setTooltip(null);
|
||||
} else if (item.getValidationError() != null) {
|
||||
setStyle("-fx-background-color: #ff8888");
|
||||
setTooltip(new Tooltip(getI18nValidatioMessage(resourceBundle, item.getValidationError().getMessages())));
|
||||
setTooltip(new Tooltip(getI18nValidatioMessage(resourceBundle, item.getValidationError())));
|
||||
}
|
||||
|
||||
if (item == null || empty) {
|
||||
|
||||
@@ -26,23 +26,32 @@
|
||||
|
||||
package ninja.javafx.smartcsv.fx.util;
|
||||
|
||||
import ninja.javafx.smartcsv.validation.ValidationError;
|
||||
import ninja.javafx.smartcsv.validation.ValidationMessage;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
/**
|
||||
* This class makes validation messages readable in supported languages
|
||||
*/
|
||||
public class I18nValidationUtil {
|
||||
|
||||
public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List<ValidationMessage> validationMessages) {
|
||||
public static String getI18nValidatioMessage(ResourceBundle resourceBundle, ValidationError error) {
|
||||
|
||||
List<ValidationMessage> validationMessages = error.getMessages();
|
||||
StringWriter message = new StringWriter();
|
||||
for (ValidationMessage validationMessage: validationMessages) {
|
||||
if (resourceBundle.containsKey(validationMessage.getKey())) {
|
||||
message.append(resourceBundle.getString(validationMessage.getKey())).append("\n");
|
||||
String resourceText = resourceBundle.getString(validationMessage.getKey());
|
||||
if (validationMessage.getParameters().length > 0) {
|
||||
message.append(format(resourceText, validationMessage.getParameters())).append("\n");
|
||||
} else {
|
||||
message.append(resourceText).append("\n");
|
||||
}
|
||||
} else {
|
||||
message.append(validationMessage.getKey()).append("\n");
|
||||
}
|
||||
|
||||
@@ -68,4 +68,12 @@ public class ValidationMessage {
|
||||
result = 31 * result + Arrays.hashCode(parameters);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ValidationMessage{" +
|
||||
"key='" + key + '\'' +
|
||||
", parameters=" + Arrays.toString(parameters) +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,11 +128,11 @@ public class Validator {
|
||||
try {
|
||||
groovyResult = script.run();
|
||||
} catch (CompilationFailedException e) {
|
||||
error.add("groovy script '"+groovyScript+"' throws exception: "+e.getMessage());
|
||||
error.add("validation.message.groovy.exception", groovyScript, e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (groovyResult == null) {
|
||||
error.add("groovy script '"+groovyScript+"' returns null");
|
||||
error.add("validation.message.groovy.return.null", groovyScript);
|
||||
}
|
||||
|
||||
if (!isScriptResultTrue(groovyResult)) {
|
||||
@@ -166,7 +166,7 @@ public class Validator {
|
||||
Integer minLength = getInteger(columnConfig, "minlength");
|
||||
if (minLength != null) {
|
||||
if (!minLength(value, minLength)) {
|
||||
error.add("has not min length of " + minLength);
|
||||
error.add("validation.message.min.length", minLength.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,7 +175,7 @@ public class Validator {
|
||||
Integer maxLength = getInteger(columnConfig, "maxlength");
|
||||
if (maxLength != null) {
|
||||
if (!maxLength(value, maxLength)) {
|
||||
error.add("has not max length of " + maxLength);
|
||||
error.add("validation.message.max.length", maxLength.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -184,7 +184,7 @@ public class Validator {
|
||||
String dateformat = getString(columnConfig, "date");
|
||||
if (dateformat != null && !dateformat.trim().isEmpty()) {
|
||||
if (!isDate(value, dateformat, true)) {
|
||||
error.add("is not a date of format " + dateformat);
|
||||
error.add("validation.message.date.format", dateformat);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -201,7 +201,7 @@ public class Validator {
|
||||
String regexp = getString(columnConfig, "regexp");
|
||||
if (regexp != null && !regexp.trim().isEmpty()) {
|
||||
if (!matchRegexp(value, regexp)) {
|
||||
error.add("does not match " + regexp);
|
||||
error.add("validation.message.regexp", regexp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,10 +237,9 @@ public class Validator {
|
||||
List<String> headerConfig = headerSectionConfig.getStringList("list");
|
||||
if (headerConfig != null) {
|
||||
if (headerNames.length != headerConfig.size()) {
|
||||
result = ValidationError.withoutLineNumber().add("number of headers is not correct! there are " +
|
||||
headerNames.length +
|
||||
" but there should be " +
|
||||
headerConfig.size());
|
||||
result = ValidationError.withoutLineNumber().add("validation.message.header.length",
|
||||
Integer.toString(headerNames.length),
|
||||
Integer.toString(headerConfig.size()));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -249,13 +248,10 @@ public class Validator {
|
||||
for(int i=0; i<headerConfig.size(); i++) {
|
||||
String header = headerConfig.get(i);
|
||||
if (!header.equals(headerNames[i])) {
|
||||
error.add("header number " +
|
||||
i +
|
||||
" does not match \"" +
|
||||
header +
|
||||
"\" should be \"" +
|
||||
headerNames[i] +
|
||||
"\"");
|
||||
error.add("validation.message.header.match",
|
||||
Integer.toString(i),
|
||||
header,
|
||||
headerNames[i]);
|
||||
}
|
||||
}
|
||||
if (!error.isEmpty()) {
|
||||
|
||||
Reference in New Issue
Block a user