leonelquinteros / gotext

Go (Golang) GNU gettext utilities package
Other
432 stars 58 forks source link

Get and similar functions don't always process formatting sequences #57

Open bk2204 opened 2 years ago

bk2204 commented 2 years ago

Please describe your issue

Is this a bug, an improvement, a proposal or something else? Describe it.

This is a bug.

The Printf function used to format strings doesn't always invoke fmt.Sprintf. As a result, the expansion of %% sequences differs if there are arguments or not.

What's the expected behaviour, the current behaviour and the steps to reproduce it?

I expect that I can create a locale object, tr, and then do this:

a := tr.Get("My string with an escaped value: %%s")
fmt.Printf(a, "text")

And that it prints My string with an escaped value: text. I happen to need this because I have a map of integers to format strings, where the format strings contain %% sequences.

Unfortunately, the behavior is inconsistent, so things are only passed through fmt.Sprintf if there are arguments. This makes the behavior hard to reason about.

Comments

If there were separate functions to read strings that were formatted and ones that were not, this wouldn't be a problem.

leonelquinteros commented 2 years ago

Hi @bk2204 , totally understand your issue. I think this is because of premature optimization on that method.

Let me give it a thought or two and get back with a proposed solution for it.

In the meanwhile, there may be some workarounds for it. I'm thinking on passing maybe a null or any placeholder value to the translation function, so you force the fmt.Sprintf call, like:

a := tr.Get("My string with an escaped value: %%s", null)
fmt.Printf(a, "text")

Should behave as you expect.