Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot contain "{" and "}" in the parameter text when using func (t *translator) Add(key interface{}, text string, override bool) error #27

Open
pwli0755 opened this issue Oct 20, 2020 · 3 comments

Comments

@pwli0755
Copy link

Cannot contain "{" and "}" in the message when using method func (t *translator) Add(key interface{}, text string, override bool) error

I want to custom a password validitor with tag valid-password, which requires password can contains a-zA-Z0-9 and (~!@#$%^&*()[]{}|/?,.<>) only, and I'm using the universal-translator to custom the error message when validate fails. The point is when the customed error message contains "{" and "}",there will be an error!

@pwli0755
Copy link
Author

func (t *translator) Add(key interface{}, text string, override bool) error {

	if _, ok := t.translations[key]; ok && !override {
		return &ErrConflictingTranslation{locale: t.Locale(), key: key, text: text}
	}

	lb := strings.Count(text, "{")
	rb := strings.Count(text, "}")

	if lb != rb {
		return &ErrMissingBracket{locale: t.Locale(), key: key, text: text}
	}

	trans := &transText{
		text: text,
	}

	var idx int

	for i := 0; i < lb; i++ {
		s := "{" + strconv.Itoa(i) + "}"
		idx = strings.Index(text, s)
		if idx == -1 {
			return &ErrBadParamSyntax{locale: t.Locale(), param: s, key: key, text: text}
		}

		trans.indexes = append(trans.indexes, idx)
		trans.indexes = append(trans.indexes, idx+len(s))
	}
...
}

I think the code upon caused the problem, is there any workaround? I tried \{\} but get error because \{ and \} are not valid escape characters.

@pwli0755 pwli0755 changed the title Cannot contain "{" and "}" in the message when using func (t *translator) Add(key interface{}, text string, override bool) error Cannot contain "{" and "}" in the parameter text when using func (t *translator) Add(key interface{}, text string, override bool) error Oct 20, 2020
@alisakotliarova
Copy link

I am having this problem as well. Additionally, if you have just one of the {} brackets it also fails.
I'm working around it by making the translation something like "{0} must not contain the characters [InvalidPasswordCharacters]" and then doing a find/replace after the translation is done to get "{0} must not contain the characters {}"

@jh125486
Copy link

jh125486 commented Sep 7, 2021

Is there a reason that Go's own template var system wasn't used for interpolation (https://pkg.go.dev/text/template#hdr-Text_and_spaces), e.g. "{{0}} test string here"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants