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 use % within an arg (template data) to the translate func #93

Closed
jhart1685 opened this issue Apr 10, 2018 · 3 comments
Closed

Cannot use % within an arg (template data) to the translate func #93

jhart1685 opened this issue Apr 10, 2018 · 3 comments

Comments

@jhart1685
Copy link

I've spent a few hours trying to figure this out, but it looks like it is not possible for an argument (/insert) on a call to i18n.T to contain a percent character.

anonymized code:

func main() {
	input := "%"
	fmt.Printf(i18n.T("<my translation ID>",
		map[string]interface{}{
			"Insert": input,
		}))
}

Output (with preceding message text removed):

%! (MISSING)

I've tried substituting % with %25 , url escaping , passing in as byte slice rather than string.

But it looks like a percent anywhere within a value (of the args template data) gets garbled.

@jhart1685
Copy link
Author

Note - this is not a generic problem with Go template processing. The following snippet works just fine:

func main() {
	input := "%"
	formatter, _ := template.New("TestTemplate").Parse("{{ .Insert }}")
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	err := formatter.Execute(f, map[string]interface{}{
		"Insert": input,
	})
	if err != nil {
		panic(err)
	}
}

@nicksnyder
Copy link
Owner

What does the translation for <my translation ID> look like?

I am not able to reproduce this with the information you have given me.

For example, this test passes:

func ExamplePercent() {
	i18n.ParseTranslationFileBytes("en-us.all.json", []byte(`[
{
	"id": "hello_person",
	"translation": "hello {{.Person}}"
}
]`))

	T, _ := i18n.Tfunc("en-us")
	fmt.Println(T("hello_person", map[string]interface{}{
		"Person": "%",
	}))
	// Output:
	// hello %
}

If you can write a failing test, the would be the most helpful.

@jhart1685
Copy link
Author

Hi @nicksnyder - thanks for the quick response.
Apologies - this was user error. The difference between your test and mine was that yours wrote the output with Println(), while I used Printf() without a format. The returned string from the translate func was correct - but it got mangled by my misuse of Printf.

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

2 participants