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: message.MatchLanguage does not return desired language #26766

Closed
kaskavalci opened this issue Aug 2, 2018 · 6 comments
Closed

x/text: message.MatchLanguage does not return desired language #26766

kaskavalci opened this issue Aug 2, 2018 · 6 comments

Comments

@kaskavalci
Copy link

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

go version go1.10.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

GOOS="darwin"
GOARCH="amd64"

What did you do?

Run the example on https://godoc.org/golang.org/x/text/message

Note: Slightly changed because example is not correct. Behavior is the same.

p := message.NewPrinter(message.MatchLanguage("nl"))
p.Printf("Hoogte: %1.f meter\n", 1244.9) // Prints Hoogte: 1.244,9 meter

What did you expect to see?

It should print the desired output, comma as decimal deliminator.
Output is the following which is English formatting.

Hoogte: 1.244,9 meter

What did you see instead?

Hoogte: 1,244.9 meter

MatchLanguage fallsback to English as can be seen from here:

base, _ := message.MatchLanguage("nl").Base()
fmt.Println(base.String()) // Prints en
@belimawr
Copy link

belimawr commented Aug 2, 2018

I'll start looking into this

@gopherbot
Copy link

Change https://golang.org/cl/127551 mentions this issue: message: fixing documentation

@belimawr
Copy link

belimawr commented Aug 2, 2018

I found a issue with your example, the correct fmt string is: Hoogte: %.1f meter\n.

I fixed the documentation.

@kaskavalci
Copy link
Author

kaskavalci commented Aug 2, 2018

@belimawr it's not only that unfortunately. What is the output? Comma should be used as decimal separator but dot is used instead. MatchLanguage is falling back to English. If I use language.Dutch it prints correctly but not with message.MatchLanguage("nl").

p := message.NewPrinter(message.MatchLanguage("nl"))
p.Printf("Hoogte: %.2f meter\n", 1244.9134) // Prints Hoogte: 1,244.91 meter

p = message.NewPrinter(language.Dutch)
p.Printf("Hoogte: %.2f meter\n", 1244.9134) // Prints Hoogte: 1.244,91 meter

@kaskavalci
Copy link
Author

kaskavalci commented Aug 2, 2018

OK, after reading the code and its tests it was more clear. We need to setup catalog otherwise it is always the default fallback language English. I think documentation should reflect this behavior. Following code works for English and Dutch with Dutch as the fallback language. It would be exteremly nice to have this in the sample code in documentation.

c := catalog.NewBuilder(catalog.Fallback(language.Dutch))
c.SetString(language.Dutch, "", "")
c.SetString(language.English, "", "")
message.DefaultCatalog = c

Edit: I think message.SetString(language.Dutch, "nl", "") also does the same thing without default fallback.

@belimawr
Copy link

belimawr commented Aug 2, 2018

@kaskavalci I see your point. The thing is that the default catalog is empty and the fallback is English. I looked more at the code and documentation for catalog, and I'm not sure that the best method to get the language is message.MatchLanguage because it uses message.DefaultCatalog (that is empty by default) under the hood.

If the objective of that piece of documentation is to show how to use message.NewPrinter to get a printer that can print localised numbers, you could just use:

p := message.NewPrinter(language.Make("nl"))
p.Printf("Hoogte: %.1f meter\n", 1244.9) // Prints Hoogte: 1.244,9 meter

I created another issue #26767 before this investigation to improve the documentation, however now I'm not sure if it's a documentation issue, or a design issue.

Edit: let's keep this discussion on #26767 as it does not seem to be an implementation issue anymore.

@golang golang locked and limited conversation to collaborators Aug 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants