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: package level docs about MatchLanguage are unclear #27641

Open
svip opened this issue Sep 12, 2018 · 4 comments

Comments

@svip
Copy link

commented Sep 12, 2018

Please answer these questions before submitting your issue. Thanks!

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

go1.11 linux/amd64

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?

I tried to use message.MatchLanguage("nl") to obtain language.Dutch, as per the example on its site.

See the following code for an example:

package main

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

func main() {
	nl := message.MatchLanguage("nl")
	fmt.Println(nl) // Prints "und", expected "nl"
	fmt.Println(language.Dutch) // Prints "nl" as expected
	p := message.NewPrinter(message.MatchLanguage("nl"))
	p.Printf("%.2f\n", 5000.00) // Prints "5,000.00", expected "5.000,00"

	p2 := message.NewPrinter(message.MatchLanguage("bn"))
	p2.Println(123456.78) // Prints "5,000.00", expected "১,২৩,৪৫৬.৭৮"
}

What did you expect to see?

I expected to receive language.Dutch, so that when I called p.Printf("%.2f\n", 5000.00), I would get "5.000,00", as Dutch uses "." for thousand separators and "," for decimal separators.

What did you see instead?

Instead message.MatchLanguage("nl") returned the und Tag rather than the Dutch Tag. And therefore when I called p.Printf() as per the example on the package's description, I got "5,000.00", which ironically matches the example in the description, but the example is wrong.

I also tried the third example in opening example, which also did not seem to produce the expected result.

It seems to me that message.MatchLanguage() either does not work or does not work as described.

Note: Making a new Printer with language.Dutch works as expected.

@gopherbot gopherbot added this to the Unreleased milestone Sep 12, 2018

@svip svip changed the title x/text: message.MatchLanguage() does work as expected x/text: message.MatchLanguage() does not work as expected Sep 12, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2018

CC @mpvl

@SamWhited

This comment has been minimized.

Copy link
Member

commented Sep 13, 2018

If I understand your example correctly, you are attempting to match using the language matcher from the default catalog. However, you haven't added any strings (and thus no languages) to the catalog. Something like this works, so I think this is expected behavior (and just an unclear example in the docs):

package main

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

func main() {
	fmt.Println("No strings:")
	nl := message.MatchLanguage("nl")
	fmt.Println(nl) // Prints "und"
	p := message.NewPrinter(nl)
	p.Printf("%.2f\n", 5000.00) // Prints "5,000.00"

	fmt.Println("With Dutch string:")
	message.Set(language.Dutch, "Hoogte", catalog.String("Hoogte"))
	nl = message.MatchLanguage("nl")
	fmt.Println(nl) // Prints "nl"
	p = message.NewPrinter(nl)
	p.Printf("%.2f\n", 5000.00) // Prints "5.000,00"
}

I have relabeled this as a documentation issue (unless of course @mpvl says otherwise).

@svip

This comment has been minimized.

Copy link
Author

commented Sep 14, 2018

Ah, OK, that makes sense. It's just not very well explained in the documentation, hence I got the wrong impression. But that means basically what I want to do in my scenario is call NewPrinter() with NewPrinter(language.Dutch), because I know the language beforehand.

@SamWhited

This comment has been minimized.

Copy link
Member

commented Sep 14, 2018

@svip see also this CL https://golang.org/cl/127598 in which this was brought up which I just remembered. I'm trying to think of a better way to clarify this in the docs than using a language tag directly (which, as Marcel points out, is probably rare).

@SamWhited SamWhited changed the title x/text: message.MatchLanguage() does not work as expected x/text/message: package level docs about MatchLanguage are unclear Sep 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.