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

x/text: messages cannot contain '{' #27849

Open
rothskeller opened this issue Sep 25, 2018 · 2 comments
Open

x/text: messages cannot contain '{' #27849

rothskeller opened this issue Sep 25, 2018 · 2 comments
Labels
NeedsInvestigation
Milestone

Comments

@rothskeller
Copy link

@rothskeller rothskeller commented Sep 25, 2018

What version of Go are you using (go version)?

go version go1.11 darwin/amd64
golang.org/x/text 905a571

What did you do?

Ran gotext update with a message catalog containing the Translation expected '{'.

What did you expect to see?

Correct import of message catalog.

What did you see instead?

gotext: generation failed: error: unmatched '}'

There are really three problems here. First, the substitution mechanism used in translations is too simplistic; it doesn't have any escaping mechanism to allow for the use of a bare '{' in the string. Second, the error message is wrong; it should say unmatched '{', not unmatched '}'. And third, the error message is utterly useless since it doesn't identify which translation has the problem.

@gopherbot gopherbot added this to the Unreleased milestone Sep 25, 2018
@ianlancetaylor ianlancetaylor added the NeedsInvestigation label Sep 25, 2018
@mpvl
Copy link
Contributor

@mpvl mpvl commented Oct 31, 2018

For the first issue, do you have a concrete use case?

The translation strings are indeed quite simplistic, as they are often written by non-engineers unfamiliar with concepts like escaping. One approach is to replace the text to be escaped itself with placeholders, for instance:

printf("Single-number set: { %d }", num)

could rewrite to something like

"Single-number set: {OPEN_BRACE} {Num} {CLOSE_BRACE}"
With the placeholder Num as before and OPEN_BRACE="{" and CLOSE_BRACE="}"

Another approach would be to, like go templates, to allow for user-specifiable open and close markers.

What is best depends a bit on what users wants and needs, so input is welcome.

@rothskeller
Copy link
Author

@rothskeller rothskeller commented Nov 3, 2018

The particular use case I hit was in writing an error message to be generated by a JSON parser when it expected to see an object. The message in English was expected '{' or 'null'.

You are correct that this could be resolved by treating the open brace character as something to be interpolated into the message. That seems cumbersome from a developer perspective, but I take your point that it might be more intuitive for a translator.

@rsc rsc unassigned mpvl Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

4 participants