Skip to content

Commit

Permalink
add out-of-the-box translations
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Nov 21, 2024
1 parent 8037463 commit b4ad65d
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A Go package for validating JSON that can generate JSON Schema.

Features:

* Mechanism to translate error messages.
* Out-of-the-box translations for some languages.
* Supports the latest JSON Schema specification (2020-12).
* Pure Go.
* No code generation, no reflection, no unsafe code.
Expand Down
134 changes: 134 additions & 0 deletions valdo/locales.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package valdo

// Locales registry with all locales supported out-of-the-box.
var DefaultLocales = Locales{
"nl": Dutch,
"ru": Russian,
"de": German,
"fr": French,
}

// The original English error mesages, for reference.
var English = Locale{
ErrNoInput{}: "the input is empty",
ErrProperty{}: "{name}: {error}",
ErrIndex{}: "at {index}: {error}",
ErrType{}: "invalid type: got {got}, expected {expected}",
ErrRequired{}: "{name} is required but not found",
ErrUnexpected{}: "unexpected property: {name}",
ErrMultipleOf{}: "must be a multiple of {value}",
ErrNot{}: "must not match the schema",
ErrMin{}: "must be greater than or equal to {value}",
ErrExclMin{}: "must be greater than {value}",
ErrMax{}: "must be less than or equal to {value}",
ErrExclMax{}: "must be less than {value}",
ErrMinLen{}: "must be at least {value} characters long",
ErrMaxLen{}: "must be at most {value} characters long",
ErrPattern{}: "must match the pattern",
ErrContains{}: "at least one item {error}",
ErrMinItems{}: "must contain at least {value} items",
ErrMaxItems{}: "must contain at most {value} items",
ErrPropertyNames{}: "property name {name} {error}",
ErrMinProperties{}: "must contain at least {value} properties",
ErrMaxProperties{}: "must contain at most {value} properties",
}

// Dutch translation of all error messages.
var Dutch = Locale{
ErrNoInput{}: "de invoer is leeg",
ErrProperty{}: "{name}: {error}",
ErrIndex{}: "bij {index}: {error}",
ErrType{}: "ongeldig type: kreeg {got}, verwachtte {expected}",
ErrRequired{}: "{name} is vereist maar niet gevonden",
ErrUnexpected{}: "onverwachte eigenschap: {name}",
ErrMultipleOf{}: "moet een veelvoud van {value} zijn",
ErrNot{}: "mag niet overeenkomen met het schema",
ErrMin{}: "moet groter zijn dan of gelijk aan {value}",
ErrExclMin{}: "moet groter zijn dan {value}",
ErrMax{}: "moet kleiner zijn dan of gelijk aan {value}",
ErrExclMax{}: "moet kleiner zijn dan {value}",
ErrMinLen{}: "moet minstens {value} tekens lang zijn",
ErrMaxLen{}: "mag maximaal {value} tekens lang zijn",
ErrPattern{}: "moet overeenkomen met het patroon",
ErrContains{}: "ten minste één item {error}",
ErrMinItems{}: "moet minstens {value} items bevatten",
ErrMaxItems{}: "mag maximaal {value} items bevatten",
ErrPropertyNames{}: "eigenschapsnaam {name} {error}",
ErrMinProperties{}: "moet minstens {value} eigenschappen bevatten",
ErrMaxProperties{}: "mag maximaal {value} eigenschappen bevatten",
}

// Russian translation of all error messages.
var Russian = Locale{
ErrNoInput{}: "ввод пуст",
ErrProperty{}: "{name}: {error}",
ErrIndex{}: "на {index}: {error}",
ErrType{}: "неверный тип: получено {got}, ожидалось {expected}",
ErrRequired{}: "{name} обязателен, но не найден",
ErrUnexpected{}: "неожиданное свойство: {name}",
ErrMultipleOf{}: "должно быть кратным {value}",
ErrNot{}: "не должно соответствовать схеме",
ErrMin{}: "должно быть больше или равно {value}",
ErrExclMin{}: "должно быть больше {value}",
ErrMax{}: "должно быть меньше или равно {value}",
ErrExclMax{}: "должно быть меньше {value}",
ErrMinLen{}: "должно содержать как минимум {value} символов",
ErrMaxLen{}: "должно содержать не более {value} символов",
ErrPattern{}: "должно соответствовать шаблону",
ErrContains{}: "как минимум один элемент {error}",
ErrMinItems{}: "должно содержать как минимум {value} элементов",
ErrMaxItems{}: "должно содержать не более {value} элементов",
ErrPropertyNames{}: "имя свойства {name} {error}",
ErrMinProperties{}: "должно содержать как минимум {value} свойств",
ErrMaxProperties{}: "должно содержать не более {value} свойств",
}

// German translation of all error messages.
var German = Locale{
ErrNoInput{}: "Die Eingabe ist leer",
ErrProperty{}: "{name}: {error}",
ErrIndex{}: "bei {index}: {error}",
ErrType{}: "Ungültiger Typ: erhalten {got}, erwartet {expected}",
ErrRequired{}: "{name} ist erforderlich, wurde aber nicht gefunden",
ErrUnexpected{}: "Unerwartete Eigenschaft: {name}",
ErrMultipleOf{}: "Muss ein Vielfaches von {value} sein",
ErrNot{}: "Darf nicht dem Schema entsprechen",
ErrMin{}: "Muss größer oder gleich {value} sein",
ErrExclMin{}: "Muss größer als {value} sein",
ErrMax{}: "Muss kleiner oder gleich {value} sein",
ErrExclMax{}: "Muss kleiner als {value} sein",
ErrMinLen{}: "Muss mindestens {value} Zeichen lang sein",
ErrMaxLen{}: "Darf höchstens {value} Zeichen lang sein",
ErrPattern{}: "Muss dem Muster entsprechen",
ErrContains{}: "Mindestens ein Element {error}",
ErrMinItems{}: "Muss mindestens {value} Elemente enthalten",
ErrMaxItems{}: "Darf höchstens {value} Elemente enthalten",
ErrPropertyNames{}: "Eigenschaftsname {name} {error}",
ErrMinProperties{}: "Muss mindestens {value} Eigenschaften enthalten",
ErrMaxProperties{}: "Darf höchstens {value} Eigenschaften enthalten",
}

// French translation of all error messages.
var French = Locale{
ErrNoInput{}: "l'entrée est vide",
ErrProperty{}: "{name}: {error}",
ErrIndex{}: "à {index}: {error}",
ErrType{}: "type invalide : reçu {got}, attendu {expected}",
ErrRequired{}: "{name} est requis mais non trouvé",
ErrUnexpected{}: "propriété inattendue : {name}",
ErrMultipleOf{}: "doit être un multiple de {value}",
ErrNot{}: "ne doit pas correspondre au schéma",
ErrMin{}: "doit être supérieur ou égal à {value}",
ErrExclMin{}: "doit être supérieur à {value}",
ErrMax{}: "doit être inférieur ou égal à {value}",
ErrExclMax{}: "doit être inférieur à {value}",
ErrMinLen{}: "doit contenir au moins {value} caractères",
ErrMaxLen{}: "doit contenir au maximum {value} caractères",
ErrPattern{}: "doit correspondre au modèle",
ErrContains{}: "au moins un élément {error}",
ErrMinItems{}: "doit contenir au moins {value} éléments",
ErrMaxItems{}: "doit contenir au maximum {value} éléments",
ErrPropertyNames{}: "nom de la propriété {name} {error}",
ErrMinProperties{}: "doit contenir au moins {value} propriétés",
ErrMaxProperties{}: "doit contenir au maximum {value} propriétés",
}
8 changes: 7 additions & 1 deletion valdo/localization.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ type Locale map[Error]string
func (ls Locales) Wrap(lang string, v Validator) Validator {
locale, hasLang := ls[lang]
if !hasLang {
return v
if len(lang) != 5 {
return v
}
locale, hasLang = ls[lang[:2]]
if !hasLang {
return v
}
}
return locale.Wrap(v)
}
Expand Down

0 comments on commit b4ad65d

Please sign in to comment.