Skip to content

Localizing a validation session with New(...options) function

Valgo has localized error messages. The error messages are currently available in English (default), Spanish and German. However, it is possible to set error messages for any locale by passing the Options parameter to the New() function. Using this parameter, you can also customize the existing Valgo locale messages.

There are two options for localization: localeCode and locale. Below, we list the different ways to customize localization with these two parameters.

  • Changing the validation session’s locale In the following example, we are setting the Spanish locale:

    // Creating the new validation session with other locale
    val := v.New(v.Options{ LocaleCode: "es" })
    // Testing the output
    val := val.Check(v.String(" ", "nombre").Not().Blank())
    out, _ := json.MarshalIndent(val.Error(), "", " ")
    fmt.Println(string(out))

    output:

    {
    "name": [
    "Nombre no puede estar en blanco"
    ]
    }

    If the specified locale does not exist, Valgo’s default English locale will be used. If you wish to change the default locale, you should use a Factory function, which is explained in the Factory section.

  • Changing the locale entries In the example below, we are changing the entry for the “Not Blank” error. Since we are not specifying the localeCode, we are using and replacing the default English locale. However, you can also specify another localeCode if necessary.

    // Creating a new validation session and changing a locale entry
    val := v.New(v.Options{
    Locale: &Locale{
    ErrorKeyNotBlank: "{{title}} should not be blank",
    }
    })
    // Testing the output
    val := val.Check(v.String(" ", "name").Not().Blank())
    out, _ := json.MarshalIndent(val.Error(), "", " ")
    fmt.Println(string(out))

    output:

    {
    "name": [
    "Name should not be blank"
    ]
    }
  • Adding a new locale As mentioned previously, Valgo currently only has the English, Spanish and German locales, but we hope to have more soon. However, you can add your own custom locale. Below is an example using the Estonian language:

    // Creating a new validation session and adding a new locale with two entries
    val := v.New(v.Options{
    LocaleCode: "ee",
    Locale: &Locale{
    ErrorKeyNotBlank: "{{title}} ei tohi olla tühi",
    ErrorKeyNotFalse: "{{title}} ei tohi olla vale",
    }
    })
    // Testing the output
    val := val.Is(
    v.String(" ", "name").Not().Blank(),
    v.Bool(false, "active").Not().False(),
    )
    out, _ := json.MarshalIndent(val.Error(), "", " ")
    fmt.Println(string(out))

    output:

    {
    "name": [
    "Name ei tohi olla tühi"
    ],
    "active": [
    "Active ei tohi olla vale"
    ]
    }

    For entries not specified in the custom locale, the default Valgo locale (English) will be used. If you wish to change the default locale, you can use the Factory function, which is further explained in the Factory section.

We welcome pull requests for adding new locale messages, but please ensure that the translations are of high quality.