-
-
Save jney/fab5ee915810014563bea71f75163b29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/text/language" | |
"golang.org/x/text/language/display" | |
) | |
var userPrefs = language.Make("en-CA") | |
var serverLangs = []language.Tag{ | |
language.Make("en"), // Swiss German | |
language.Make("en-US"), | |
} | |
var matcher = language.NewMatcher(serverLangs) | |
func main() { | |
tag, index, confidence := matcher.Match(userPrefs) | |
fmt.Printf("best match: %s (%s) index=%d confidence=%v\n", | |
display.English.Tags().Name(tag), | |
display.Self.Name(tag), | |
index, confidence) | |
// best match: German (Deutsch) index=1 confidence=High | |
if confidence != language.Exact { | |
b, _ := userPrefs.Base() | |
tag, index, confidence := matcher.Match(language.Make(b.String())) | |
fmt.Printf("best match: %s (%s) index=%d confidence=%v\n", | |
display.English.Tags().Name(tag), | |
display.Self.Name(tag), | |
index, confidence) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment