Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

数据校验组件支持i18n特性,内部使用了goframe框架统一的i18n组件实现。默认使用默认的i18n单例对象,即g.I18n()对象。

在进一步使用之前,关于i18n国际化功能配置及使用请参考章节:I18N国际化

配置示例

错误提示

...

默认i18n错误提示

默认的英文国际化语言配置文件参考:https://github.com/gogf/gf/tree/master/util/gvalid/i18n/en

"gf.gvalid.rule.required"             = "The :attribute field is required"
"gf.gvalid.rule.required-if"          = "The :attribute field is required"
"gf.gvalid.rule.required-unless"      = "The :attribute field is required"
"gf.gvalid.rule.required-with"        = "The :attribute field is required"
"gf.gvalid.rule.required-with-all"    = "The :attribute field is required"
"gf.gvalid.rule.required-without"     = "The :attribute field is required"
"gf.gvalid.rule.required-without-all" = "The :attribute field is required"
"gf.gvalid.rule.date"                 = "The :attribute value is not a valid date"
"gf.gvalid.rule.date-format"          = "The :attribute value does not match the format :format"
"gf.gvalid.rule.email"                = "The :attribute value must be a valid email address"
"gf.gvalid.rule.phone"                = "The :attribute value must be a valid phone number"
"gf.gvalid.rule.telephone"            = "The :attribute value must be a valid telephone number"
"gf.gvalid.rule.passport"             = "The :attribute value is not a valid passport format"
"gf.gvalid.rule.password"             = "The :attribute value is not a valid passport format"
"gf.gvalid.rule.password2"            = "The :attribute value is not a valid passport format"
"gf.gvalid.rule.password3"            = "The :attribute value is not a valid passport format"
"gf.gvalid.rule.postcode"             = "The :attribute value is not a valid passport format"
"gf.gvalid.rule.resident-id"          = "The :attribute value is not a valid resident id number"
"gf.gvalid.rule.bank-card"            = "The :attribute value must be a valid bank card number"
"gf.gvalid.rule.qq"                   = "The :attribute value must be a valid QQ number"
"gf.gvalid.rule.ip"                   = "The :attribute value must be a valid IP address"
"gf.gvalid.rule.ipv4"                 = "The :attribute value must be a valid IPv4 address"
"gf.gvalid.rule.ipv6"                 = "The :attribute value must be a valid IPv6 address"
"gf.gvalid.rule.mac"                  = "The :attribute value must be a valid MAC address"
"gf.gvalid.rule.url"                  = "The :attribute value must be a valid URL address"
"gf.gvalid.rule.domain"               = "The :attribute value must be a valid domain format"
"gf.gvalid.rule.length"               = "The :attribute value length must be between :min and :max"
"gf.gvalid.rule.min-length"           = "The :attribute value length must be equal or greater than :min"
"gf.gvalid.rule.max-length"           = "The :attribute value length must be equal or lesser than :max"
"gf.gvalid.rule.between"              = "The :attribute value must be between :min and :max"
"gf.gvalid.rule.min"                  = "The :attribute value must be equal or greater than :min"
"gf.gvalid.rule.max"                  = "The :attribute value must be equal or lesser than :max"
"gf.gvalid.rule.json"                 = "The :attribute value must be a valid JSON string"
"gf.gvalid.rule.xml"                  = "The :attribute value must be a valid XML string"
"gf.gvalid.rule.array"                = "The :attribute value must be an array"
"gf.gvalid.rule.integer"              = "The :attribute value must be an integer"
"gf.gvalid.rule.float"                = "The :attribute value must be a float"
"gf.gvalid.rule.boolean"              = "The :attribute value field must be true or false"
"gf.gvalid.rule.same"                 = "The :attribute value must be the same as field :field"
"gf.gvalid.rule.different"            = "The :attribute value must be different from field :field"
"gf.gvalid.rule.in"                   = "The :attribute value is not in acceptable range"
"gf.gvalid.rule.not-in"               = "The :attribute value is not in acceptable range"
"gf.gvalid.rule.regex"                = "The :attribute value is invalid"
"gf.gvalid.rule.__default__"          = "The :attribute value is invalid"

中文错误提示

我们提供了建议的中文i18n国际化语言配置文件:https://github.com/gogf/gf/tree/master/util/gvalid/i18n/cn

"gf.gvalid.rule.required"             = ":attribute 字段不能为空"
"gf.gvalid.rule.required-if"          = ":attribute 字段不能为空"
"gf.gvalid.rule.required-unless"      = ":attribute 字段不能为空"
"gf.gvalid.rule.required-with"        = ":attribute 字段不能为空"
"gf.gvalid.rule.required-with-all"    = ":attribute 字段不能为空"
"gf.gvalid.rule.required-without"     = ":attribute 字段不能为空"
"gf.gvalid.rule.required-without-all" = ":attribute 字段不能为空"
"gf.gvalid.rule.date"                 = ":attribute 日期格式不正确"
"gf.gvalid.rule.date-format"          = ":attribute 日期格式不满足:format"
"gf.gvalid.rule.email"                = ":attribute 邮箱地址格式不正确"
"gf.gvalid.rule.phone"                = ":attribute 手机号码格式不正确"
"gf.gvalid.rule.telephone"            = ":attribute 电话号码格式不正确"
"gf.gvalid.rule.passport"             = ":attribute 账号格式不合法,必需以字母开头,只能包含字母、数字和下划线,长度在6~18之间"
"gf.gvalid.rule.password"             = ":attribute 密码格式不合法,密码格式为任意6-18位的可见字符"
"gf.gvalid.rule.password2"            = ":attribute 密码格式不合法,密码格式为任意6-18位的可见字符,必须包含大小写字母和数字"
"gf.gvalid.rule.password3"            = ":attribute 密码格式不合法,密码格式为任意6-18位的可见字符,必须包含大小写字母、数字和特殊字符"
"gf.gvalid.rule.postcode"             = ":attribute 邮政编码不正确"
"gf.gvalid.rule.resident-id"          = ":attribute 身份证号码格式不正确"
"gf.gvalid.rule.bank-card"            = ":attribute 银行卡号格式不正确"
"gf.gvalid.rule.qq"                   = ":attribute QQ号码格式不正确"
"gf.gvalid.rule.ip"                   = ":attribute IP地址格式不正确"
"gf.gvalid.rule.ipv4"                 = ":attribute IPv4地址格式不正确"
"gf.gvalid.rule.ipv6"                 = ":attribute IPv6地址格式不正确"
"gf.gvalid.rule.mac"                  = ":attribute MAC地址格式不正确"
"gf.gvalid.rule.url"                  = ":attribute URL地址格式不正确"
"gf.gvalid.rule.domain"               = ":attribute 域名格式不正确"
"gf.gvalid.rule.length"               = ":attribute 字段长度为:min到:max个字符"
"gf.gvalid.rule.min-length"           = ":attribute 字段最小长度为:min"
"gf.gvalid.rule.max-length"           = ":attribute 字段最大长度为:max"
"gf.gvalid.rule.between"              = ":attribute 字段大小为:min到:max"
"gf.gvalid.rule.min"                  = ":attribute 字段最小值为:min"
"gf.gvalid.rule.max"                  = ":attribute 字段最大值为:max"
"gf.gvalid.rule.json"                 = ":attribute 字段应当为JSON格式"
"gf.gvalid.rule.xml"                  = ":attribute 字段应当为XML格式"
"gf.gvalid.rule.array"                = ":attribute 字段应当为数组"
"gf.gvalid.rule.integer"              = ":attribute 字段应当为整数"
"gf.gvalid.rule.float"                = ":attribute 字段应当为浮点数"
"gf.gvalid.rule.boolean"              = ":attribute 字段应当为布尔值"
"gf.gvalid.rule.same"                 = ":attribute 字段值必须和:field相同"
"gf.gvalid.rule.different"            = ":attribute 字段值不能与:field相同"
"gf.gvalid.rule.in"                   = ":attribute 字段值不合法"
"gf.gvalid.rule.not-in"               = ":attribute 字段值不合法"
"gf.gvalid.rule.regex"                = ":attribute 字段值不合法"
"gf.gvalid.rule.__default__"          = ":attribute 字段值不合法"

__default__错误提示

中文错误提示

我们提供了建议的中文i18n国际化语言配置文件:https://github.com/gogf/gf/tree/master/util/gvalid/i18n/cn 

默认 错误提示

当在i18n中找不到对应规则的错误提示时,将会使用__default__配置的错误提示信息。往往使用在自定义规则中。

开发示例

我们通过中间件统一设置请求的错误提示i18n语言。

目录结构

Note

注意工程目录结构,以便于默认的g.i18n()对象能自动读取配置。有相当一部分同学倒在了这里。


Code Block
languagexml
├── main.go
└── i18n
    ├── en.toml
    └── zh-CN.toml

i18n文件

en.toml

Code Block
languagetoml
"ReuiredUserName" = "Please input user name"
"ReuiredUserType" = "Please select user type"

zh-CN.toml

Code Block
languagetoml
"ReuiredUserName" = "请输入用户名称"
"ReuiredUserType" = "请选择用户类型"

示例代码

Code Block
languagego
package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/i18n/gi18n"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	type User struct {
		Name string `v:"required#ReuiredUserName"`
		Type int    `v:"required#ReuiredUserType"`
	}
	var (
		ctx  = gctx.New()
		data = g.Map{
			"name": "john",
		}
		user  = User{}
		ctxEn = gi18n.WithLanguage(ctx, "en")
		ctxCh = gi18n.WithLanguage(ctx, "zh-CN")
	)

	if err := gconv.Scan(data, &user); err != nil {
		panic(err)
	}
	// 英文 
	if err := g.Validator().Assoc(data).Data(user).Run(ctxEn); err != nil {   
		g.Dump(err.String())
	}
	// 中文 
	if err := g.Validator().Assoc(data).Data(user).Run(ctxCh); err != nil {   
		g.Dump(err.String())
	}
}

执行后,终端输出:

Code Block
languagexml
Please select user type
请选择用户类型





Panel
titleContent Menu

Table of Contents

...