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: p.Sprint outputs key + fallback #26519

Open
mvrhov opened this issue Jul 21, 2018 · 5 comments
Open

x/text: p.Sprint outputs key + fallback #26519

mvrhov opened this issue Jul 21, 2018 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mvrhov
Copy link

mvrhov commented Jul 21, 2018

Please answer these questions before submitting your issue. Thanks!

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

1.11b2

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

linux/amd64

What did you do?

T.Sprint(message.Key("login.form.label.password", "Password"))

What did you expect to see?

Password

What did you see instead?

{login.form.label.password Password}

T.Sprintf(message.Key("login.form.label.password", "Password")) returns the expected output, however it's use is not necessary in many such cases, where there is no parameters for the string even the lint tool is showing warnings in such cases.

@gopherbot gopherbot added this to the Unreleased milestone Jul 21, 2018
@mvrhov mvrhov changed the title x/text p.Sprint outpust full key x/text p.Sprint outputs key + fallback Jul 21, 2018
@meirf
Copy link
Contributor

meirf commented Jul 22, 2018

@mvrhov, can you please elaborate a bit on this:

... however it's use is not necessary in many such cases, where there is no parameters for the string even the lint tool is showing warnings in such cases.

Is it possible you're not using the Print/Sprint functions as intended?

package main

import (
	"fmt"
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

type foo struct {
	x float64
}

func main() {
	message.SetString(language.German, "archive(noun)", "archive")

	p := message.NewPrinter(language.German)

	p.Print(123456.78)
	fmt.Println()
	p.Print(foo{123456.78})
	fmt.Println()
	p.Print("archive(noun)")
	fmt.Println()
	p.Printf("archive(noun)")
	fmt.Println()

	fmt.Println(p.Sprint(message.Key("login.form.label.password", "Password")))
	fmt.Println(p.Sprintf(message.Key("login.form.label.password", "Password")))
}

Result:

123.456,78
{123.456,78}
archive(noun)
archive
{login.form.label.password Password}
Password

Note the differences: Print/Sprint vs Printf/Sprintf.
Print/Sprint: Accept a ...interface{}. Just do localized formatting.
Printf/Sprintf: Accept key Reference, a ...interface{}. Does translation and fallback.

So for your code:
T.Sprint(message.Key("login.form.label.password", "Password"))
we expect that it will just do localized formatting of each field which in this case is just the contents of the Reference struct. Sprint doesn't view the input as a Reference to translate+fallback - just an arbitrary object to format.

Please let me know if I'm mistaken.

/cc @mpvl

@mvrhov
Copy link
Author

mvrhov commented Jul 22, 2018

Yeah. I've seen that the function signature is different I'd say that it's still a bit unexpected.
And that's the reason I'd like to keep this open if you don't mind.

@meirf
Copy link
Contributor

meirf commented Jul 23, 2018

And that's the reason I'd like to keep this open if you don't mind.

Okay, but can you please be more specific on what you are proposing. What API change/addition would you like to happen or what documentation would you like to change or what bug should be fixed?

... however it's use is not necessary in many such cases, where there is no parameters for the string even the lint tool is showing warnings in such cases.

This is still a bit vague to me. Can you please help me understand by elaborating more?

@mvrhov
Copy link
Author

mvrhov commented Jul 23, 2018

Okay, but can you please be more specific on what you are proposing. What API change/addition would you..

The API could figure out itself if based on the first parameter which behavior it should use. e.g if the message with fallback is passed then it uses one path otherwise there is no fallback it uses the 2nd path. Unless this really slows things down.

For the 2nd part. Please disregard it. The reason for the 2nd part is because I've been using different languages for the past 20years+ and have thought that the behavior is as following.. Functions ending with f are used only when you have to insert/format the passed parameters e.g fmt.Sprintf("This string has a couple of %s parameters, %d", "string", 10) where the functions not ending with f are to be used when you don't have such case e.g fmt.Sprint("This string has no parameters").

@agnivade agnivade changed the title x/text p.Sprint outputs key + fallback x/text: p.Sprint outputs key + fallback Jan 7, 2019
@mikegleasonjr
Copy link

I too was confused by all of this and lost quite a bit of time trying to understand why Sprint would not localize my key while Sprintf would.

It would be great if each argument in Print/Sprint would "Do a translation and fallback.". At least that's what I propose.

@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants