-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: i18n package with spanish & english translations (#28)
* feat: added fallback error message to map * feat: i18n package * refactor: move default map to i18n/en package * feat: Add new es package to i18n (#29) credit @cachesdev * Add new zes package which contains spanish translations for validation errors * Remove articles and change `Coleccion` to `Lista` * Fix typo * Change package name zes -> es * fix: import * docs: i18n package * feat: option to choose lang key * docs: added i18n docs --------- Co-authored-by: Gustavo Dominguez <59543366+cachesdev@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
227 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package en | ||
|
||
import ( | ||
"github.com/Oudwins/zog/zconst" | ||
) | ||
|
||
var Map zconst.LangMap = map[zconst.ZogType]map[zconst.ZogErrCode]string{ | ||
zconst.TypeString: { | ||
zconst.ErrCodeRequired: "is required", | ||
zconst.ErrCodeMin: "string must contain at least {{min}} character(s)", | ||
zconst.ErrCodeMax: "string must contain at most {{max}} character(s)", | ||
zconst.ErrCodeLen: "string must be exactly {{len}} character(s)", | ||
zconst.ErrCodeEmail: "must be a valid email", | ||
zconst.ErrCodeURL: "must be a valid URL", | ||
zconst.ErrCodeHasPrefix: "string must start with {{prefix}}", | ||
zconst.ErrCodeHasSuffix: "string must end with {{suffix}}", | ||
zconst.ErrCodeContains: "string must contain {{contained}}", | ||
zconst.ErrCodeContainsDigit: "string must contain at least one digit", | ||
zconst.ErrCodeContainsUpper: "string must contain at least one uppercase letter", | ||
zconst.ErrCodeContainsLower: "string must contain at least one lowercase letter", | ||
zconst.ErrCodeContainsSpecial: "string must contain at least one special character", | ||
zconst.ErrCodeOneOf: "string must be one of {{one_of_options}}", | ||
zconst.ErrCodeFallback: "string is invalid", | ||
}, | ||
zconst.TypeBool: { | ||
zconst.ErrCodeRequired: "is required", | ||
zconst.ErrCodeTrue: "must be true", | ||
zconst.ErrCodeFalse: "must be false", | ||
zconst.ErrCodeFallback: "value is invalid", | ||
}, | ||
zconst.TypeNumber: { | ||
zconst.ErrCodeRequired: "is required", | ||
zconst.ErrCodeLTE: "number must be less than or equal to {{lte}}", | ||
zconst.ErrCodeLT: "number must be less than {{lt}}", | ||
zconst.ErrCodeGTE: "number must be greater than or equal to {{gte}}", | ||
zconst.ErrCodeGT: "number must be greater than {{gt}}", | ||
zconst.ErrCodeEQ: "number must be equal to {{eq}}", | ||
zconst.ErrCodeOneOf: "number must be one of {{options}}", | ||
zconst.ErrCodeFallback: "number is invalid", | ||
}, | ||
zconst.TypeTime: { | ||
zconst.ErrCodeRequired: "is required", | ||
zconst.ErrCodeAfter: "time must be after {{after}}", | ||
zconst.ErrCodeBefore: "time must be before {{before}}", | ||
zconst.ErrCodeEQ: "time must be equal to {{eq}}", | ||
zconst.ErrCodeFallback: "time is invalid", | ||
}, | ||
zconst.TypeSlice: { | ||
zconst.ErrCodeRequired: "is required", | ||
zconst.ErrCodeMin: "slice must contain at least {{min}} items", | ||
zconst.ErrCodeMax: "slice must contain at most {{max}} items", | ||
zconst.ErrCodeLen: "slice must contain exactly {{len}} items", | ||
zconst.ErrCodeContains: "slice must contain {{contained}}", | ||
zconst.ErrCodeFallback: "slice is invalid", | ||
}, | ||
zconst.TypeStruct: { | ||
zconst.ErrCodeRequired: "is required", | ||
zconst.ErrCodeFallback: "struct is invalid", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package es | ||
|
||
import ( | ||
"github.com/Oudwins/zog/zconst" | ||
) | ||
|
||
var Map zconst.LangMap = map[zconst.ZogType]map[zconst.ZogErrCode]string{ | ||
zconst.TypeString: { | ||
zconst.ErrCodeRequired: "Es obligatorio", | ||
zconst.ErrCodeMin: "Cadena debe contener al menos {{min}} caracter(es)", | ||
zconst.ErrCodeMax: "Cadena debe contener como máximo {{max}} caracter(es)", | ||
zconst.ErrCodeLen: "Cadena debe tener exactamente {{len}} caracter(es)", | ||
zconst.ErrCodeEmail: "Debe ser un correo electrónico válido", | ||
zconst.ErrCodeURL: "Debe ser una URL válida", | ||
zconst.ErrCodeHasPrefix: "Cadena debe comenzar con {{prefix}}", | ||
zconst.ErrCodeHasSuffix: "Cadena debe terminar con {{suffix}}", | ||
zconst.ErrCodeContains: "Cadena debe contener {{contained}}", | ||
zconst.ErrCodeContainsDigit: "Cadena debe contener al menos un dígito", | ||
zconst.ErrCodeContainsUpper: "Cadena debe contener al menos una letra mayúscula", | ||
zconst.ErrCodeContainsLower: "Cadena debe contener al menos una letra minúscula", | ||
zconst.ErrCodeContainsSpecial: "Cadena debe contener al menos un carácter especial", | ||
zconst.ErrCodeOneOf: "Cadena debe ser una de las siguientes: {{one_of_options}}", | ||
zconst.ErrCodeFallback: "Cadena no es válida", | ||
}, | ||
zconst.TypeBool: { | ||
zconst.ErrCodeRequired: "Es obligatorio", | ||
zconst.ErrCodeTrue: "Debe ser verdadero", | ||
zconst.ErrCodeFalse: "Debe ser falso", | ||
zconst.ErrCodeFallback: "Valor no es válido", | ||
}, | ||
zconst.TypeNumber: { | ||
zconst.ErrCodeRequired: "Es obligatorio", | ||
zconst.ErrCodeLTE: "Número debe ser menor o igual a {{lte}}", | ||
zconst.ErrCodeLT: "Número debe ser menor que {{lt}}", | ||
zconst.ErrCodeGTE: "Número debe ser mayor o igual a {{gte}}", | ||
zconst.ErrCodeGT: "Número debe ser mayor que {{gt}}", | ||
zconst.ErrCodeEQ: "Número debe ser igual a {{eq}}", | ||
zconst.ErrCodeOneOf: "Número debe ser uno de los siguientes: {{options}}", | ||
zconst.ErrCodeFallback: "Número no es válido", | ||
}, | ||
zconst.TypeTime: { | ||
zconst.ErrCodeRequired: "Es obligatorio", | ||
zconst.ErrCodeAfter: "Fecha debe ser posterior a {{after}}", | ||
zconst.ErrCodeBefore: "Fecha debe ser anterior a {{before}}", | ||
zconst.ErrCodeEQ: "Fecha debe ser igual a {{eq}}", | ||
zconst.ErrCodeFallback: "Fecha no es válida", | ||
}, | ||
zconst.TypeSlice: { | ||
zconst.ErrCodeRequired: "Es obligatorio", | ||
zconst.ErrCodeMin: "Lista debe contener al menos {{min}} elementos", | ||
zconst.ErrCodeMax: "Lista debe contener como máximo {{max}} elementos", | ||
zconst.ErrCodeLen: "Lista debe contener exactamente {{len}} elementos", | ||
zconst.ErrCodeContains: "Lista debe contener {{contained}}", | ||
zconst.ErrCodeFallback: "Lista no es válida", | ||
}, | ||
zconst.TypeStruct: { | ||
zconst.ErrCodeRequired: "Es obligatorio", | ||
zconst.ErrCodeFallback: "Estructura no es válida", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package i18n | ||
|
||
import ( | ||
"github.com/Oudwins/zog/conf" | ||
"github.com/Oudwins/zog/internals" | ||
"github.com/Oudwins/zog/zconst" | ||
) | ||
|
||
// Takes a map[langKey]conf.LangMap | ||
// usage is i18n.SetLanguagesErrsMap(map[string]zconst.LangMap{ | ||
// "es": es.Map, "en": en.Map, | ||
// }, "en", i18n.WithLangKey("langKey")) | ||
// schema.Parse(data, &dest, z.WithCtxValue("langKey", "es")) | ||
func SetLanguagesErrsMap(m map[string]zconst.LangMap, defaultLang string, opts ...setLanguageOption) { | ||
langKey := "lang" | ||
|
||
for _, op := range opts { | ||
op(&langKey) | ||
} | ||
|
||
conf.ErrorFormatter = func(e internals.ZogError, ctx internals.ParseCtx) { | ||
lang := ctx.Get(langKey) | ||
if lang != nil { | ||
langM, ok := m[lang.(string)] | ||
if ok { | ||
conf.NewDefaultFormatter(langM)(e, ctx) | ||
return | ||
} | ||
} | ||
// use default lang if failed to get correct language map | ||
conf.NewDefaultFormatter(m[defaultLang])(e, ctx) | ||
} | ||
} | ||
|
||
// Override the default lang key used to get the language from the ParseContext | ||
func WithLangKey(key string) setLanguageOption { | ||
return func(lk *string) { | ||
*lk = key | ||
} | ||
} | ||
|
||
// Please use the helper function this type may very well change in the future but the helper function's API will stay the same | ||
type setLanguageOption = func(langKey *string) | ||
|
||
// Proxy the type for easy use | ||
type LangMap = zconst.LangMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters