add i18n
This commit is contained in:
@@ -3,6 +3,8 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
@@ -11,27 +13,27 @@ type apiValidationError struct {
|
||||
Err string `json:"error"`
|
||||
}
|
||||
|
||||
func getValidationErrors(ve *validator.ValidationErrors) []apiValidationError {
|
||||
func getValidationErrors(ac appcontext.AppContext, ve *validator.ValidationErrors) []apiValidationError {
|
||||
errors := make([]apiValidationError, len(*ve))
|
||||
for i, fe := range *ve {
|
||||
errors[i] = apiValidationError{
|
||||
Field: fe.Field(),
|
||||
Err: computeValidationMessage(&fe),
|
||||
Err: computeValidationMessage(ac, &fe),
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
func computeValidationMessage(fe *validator.FieldError) string {
|
||||
func computeValidationMessage(ac appcontext.AppContext, fe *validator.FieldError) string {
|
||||
tag := (*fe).Tag()
|
||||
switch tag {
|
||||
case "required":
|
||||
return fmt.Sprintf("%s is required.", (*fe).Field())
|
||||
return i18nresource.GetTranslatedMessage(ac, "ValidationRequired")
|
||||
case "min":
|
||||
return fmt.Sprintf("%s is not long enough. It should be at least %s characters.", (*fe).Field(), (*fe).Param())
|
||||
return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationTooShort"), (*fe).Param())
|
||||
case "max":
|
||||
return fmt.Sprintf("%s is too long. It should be under %s characters.", (*fe).Field(), (*fe).Param())
|
||||
return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationTooLong"), (*fe).Param())
|
||||
default:
|
||||
return fmt.Sprintf("Validation failed for '%s' property.", tag)
|
||||
return fmt.Sprintf(i18nresource.GetTranslatedMessage(ac, "ValidationPropertyFail"), tag)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user