Get and similar functions don't always process formatting sequences · Issue #57 · leonelquinteros/gotext · GitHub
Skip to content

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

Open

Description

@bk2204

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.

Activity

  1. leonelquinteros commented on Dec 14, 2021

    @leonelquinteros
    Owner

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions