2021-01-12 10:46:39 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-11-13 00:12:35 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-11-13 00:12:35 +08:00
|
|
|
|
|
|
|
|
package gvalid
|
|
|
|
|
|
2021-10-11 21:34:19 +08:00
|
|
|
import "context"
|
|
|
|
|
|
2020-05-10 22:32:10 +08:00
|
|
|
// getErrorMessageByRule retrieves and returns the error message for specified rule.
|
|
|
|
|
// It firstly retrieves the message from custom message map, and then checks i18n manager,
|
2021-11-14 17:47:21 +08:00
|
|
|
// it returns the default error message if it's not found in neither custom message map nor i18n manager.
|
2021-10-11 21:34:19 +08:00
|
|
|
func (v *Validator) getErrorMessageByRule(ctx context.Context, ruleKey string, customMsgMap map[string]string) string {
|
2020-05-10 22:32:10 +08:00
|
|
|
content := customMsgMap[ruleKey]
|
|
|
|
|
if content != "" {
|
2021-05-13 00:16:45 +08:00
|
|
|
// I18n translation.
|
2021-10-11 21:34:19 +08:00
|
|
|
i18nContent := v.i18nManager.GetContent(ctx, content)
|
2021-05-13 00:16:45 +08:00
|
|
|
if i18nContent != "" {
|
|
|
|
|
return i18nContent
|
|
|
|
|
}
|
2020-05-10 22:32:10 +08:00
|
|
|
return content
|
|
|
|
|
}
|
2021-05-13 00:16:45 +08:00
|
|
|
// Retrieve default message according to certain rule.
|
2021-10-11 21:34:19 +08:00
|
|
|
content = v.i18nManager.GetContent(ctx, ruleMessagePrefixForI18n+ruleKey)
|
2020-05-10 10:56:11 +08:00
|
|
|
if content == "" {
|
2020-05-10 22:32:10 +08:00
|
|
|
content = defaultMessages[ruleKey]
|
2020-05-10 10:56:11 +08:00
|
|
|
}
|
2020-09-21 23:51:30 +08:00
|
|
|
// If there's no configured rule message, it uses default one.
|
|
|
|
|
if content == "" {
|
2021-10-11 21:34:19 +08:00
|
|
|
content = v.i18nManager.GetContent(ctx, ruleMessagePrefixForI18n+internalDefaultRuleName)
|
2020-09-21 23:51:30 +08:00
|
|
|
if content == "" {
|
2021-05-19 13:29:40 +08:00
|
|
|
content = defaultMessages[internalDefaultRuleName]
|
2020-09-21 23:51:30 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-10 10:56:11 +08:00
|
|
|
return content
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|