go x/text: p.Sprint输出键+备用值

8xiog9wr  于 3个月前  发布在  Go
关注(0)|答案(5)|浏览(43)

请在提交问题之前回答以下问题。谢谢!

您正在使用的Go版本是什么( go version )?

1.11b2

这个问题是否在最新版本中重现?

是的

您正在使用什么操作系统和处理器架构( go env )?

linux/amd64

您做了什么?

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

您期望看到什么?

密码

您实际看到了什么?

{login.form.label.password Password}
T.Sprintf(message.Key("login.form.label.password", "Password")) 返回了预期的输出,但是在许多类似情况下,它并不必要使用,即使在这种情况下,字符串甚至lint工具也显示警告。

q1qsirdb

q1qsirdb1#

@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

kqlmhetl

kqlmhetl2#

是的。我注意到函数签名有所不同,我认为这仍然有点出乎意料。这就是我想保持开放的原因,如果你不介意的话。

zxlwwiss

zxlwwiss3#

这是我想保持开放的原因,如果你不介意的话。
好的,但是你能请你更具体地说明你提议的内容吗?你希望发生什么API变化/添加?或者你想改变或修复哪些文档?或者应该修复哪些错误?
...然而在许多情况下,它的使用并不是必要的,即使在这些情况下,字符串没有参数,甚至lint工具也显示警告。
这对我来说还是有点模糊。你能帮我通过详细阐述来理解吗?

wi3ka0sx

wi3ka0sx4#

好的,但是你能请你具体说明你提议的是什么吗?你会对API做什么样的更改/添加?

API可以根据第一个参数来判断应该使用哪种行为。例如,如果传递了带有fallback的消息,那么它将使用第一个路径,否则没有fallback,它将使用第二个路径。除非这真的会降低速度。

对于第二部分,请忽略它。第二部分的原因是因为我在过去的20年里一直在使用不同的语言,并且认为行为如下:以f结尾的函数只在需要插入/格式化传递的参数时使用,例如fmt.Sprintf("This string has a couple of %s parameters, %d", "string", 10),而以f结尾的函数则在没有这种情况时使用,例如fmt.Sprint("This string has no parameters")

oyt4ldly

oyt4ldly5#

我也对这一切感到困惑,并花了相当多的时间试图理解为什么Sprint无法本地化我的按键,而Sprintf可以。如果Print/Sprint中的每个参数都能"进行翻译和回退",那就太好了。至少这是我的建议。

相关问题