本文整理汇总了Golang中golang.org/x/text/language.NewMatcher函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMatcher函数的具体用法?Golang NewMatcher怎么用?Golang NewMatcher使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMatcher函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MobileInit
func MobileInit() {
initChan := make(chan struct{})
initDone = initChan
go func() {
defer close(initChan)
langs := preferredLanguages()
fmt.Printf("PREFERRED LANGUAGES: %v\n", langs)
matcher := language.NewMatcher([]language.Tag{
language.MustParse("en_US"),
language.MustParse("es_MX"),
})
tag, idx, conf := matcher.Match(langs...)
fmt.Printf("I think we'll go with %s (%d th place choice with %s confidence)\n", tag, idx, conf)
if locale := tag.String(); locale != "en-us" {
t, err := loadDictionary(locale)
if err != nil {
panic(fmt.Sprintf("Cannot load '%s': %s", locale, err))
}
translate = t
}
if t, err := loadDictionary("en-us"); err != nil {
panic(fmt.Sprintf("Cannot load 'en-us': %s", err))
} else {
translateFallback = t
}
}()
}
示例2: init
func init() {
tags := []language.Tag{}
for _, s := range strings.Split(supported, " ") {
tags = append(tags, language.MustParse(s))
}
matcher = language.NewMatcher(tags)
Supported = language.NewCoverage(tags)
}
示例3: getLanguage
func getLanguage(w http.ResponseWriter, r *http.Request) string {
var preferred []language.Tag
var err error
save := false
r.ParseForm()
lang := r.Form.Get("lang")
if lang != "" {
preferred, _, err = language.ParseAcceptLanguage(lang)
if err != nil {
// log err
} else {
save = true
}
}
if preferred == nil {
cookie, err := r.Cookie("lang")
if err == nil {
preferred, _, err = language.ParseAcceptLanguage(cookie.Value)
if err != nil {
// log err
}
}
}
if preferred == nil {
preferred, _, err = language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
if err != nil {
// log err
}
}
matcher := language.NewMatcher([]language.Tag{
language.English,
language.Italian,
language.French,
})
code, _, _ := matcher.Match(preferred...)
base, _ := code.Base()
if save {
now := time.Now()
expires := now.AddDate(1, 0, 0)
http.SetCookie(w, &http.Cookie{
Name: "lang",
Value: base.String(),
Path: "/",
Expires: expires,
})
}
return base.String()
}
示例4: LanguageDefault
// LanguageDefault returns the canonical name of the default encoding for a
// given language.
func LanguageDefault(tag language.Tag) string {
matcherOnce.Do(func() {
tags := []language.Tag{}
for _, t := range strings.Split(locales, " ") {
tags = append(tags, language.MustParse(t))
}
matcher = language.NewMatcher(tags)
})
_, i, _ := matcher.Match(tag)
return canonical[localeMap[i]] // Default is Windows-1252.
}
示例5: init
func init() {
tags := make([]language.Tag, numSupported)
s := supported
for i := range tags {
p := strings.IndexByte(s, '|')
tags[i] = language.Raw.Make(s[:p])
s = s[p+1:]
}
matcher = language.NewMatcher(tags)
Supported = language.NewCoverage(tags)
Values = language.NewCoverage(langTagSet.Tags, supportedScripts, supportedRegions)
}
示例6: checkLang
func checkLang(srcStr, targStr string) tree.Bool {
srcLang := language.Make(srcStr)
srcRegion, srcRegionConf := srcLang.Region()
targLang := language.Make(targStr)
targRegion, targRegionConf := targLang.Region()
if srcRegionConf == language.Exact && targRegionConf != language.Exact {
return tree.Bool(false)
}
if srcRegion != targRegion && srcRegionConf == language.Exact && targRegionConf == language.Exact {
return tree.Bool(false)
}
_, _, conf := language.NewMatcher([]language.Tag{srcLang}).Match(targLang)
return tree.Bool(conf >= language.High)
}
示例7: ExampleParseAcceptLanguage
func ExampleParseAcceptLanguage() {
// Tags are reordered based on their q rating. A missing q value means 1.0.
fmt.Println(language.ParseAcceptLanguage(" nn;q=0.3, en-gb;q=0.8, en,"))
m := language.NewMatcher([]language.Tag{language.Norwegian, language.Make("en-AU")})
t, _, _ := language.ParseAcceptLanguage("da, en-gb;q=0.8, en;q=0.7")
fmt.Println(m.Match(t...))
// Danish is pretty close to Norwegian.
t, _, _ = language.ParseAcceptLanguage(" da, nl")
fmt.Println(m.Match(t...))
// Output:
// [en en-GB nn] [1 0.8 0.3] <nil>
// en-AU 1 High
// no 0 High
}
示例8: InitI18n
//Read all .ini files in dir, where the filenames are BCP 47 tags
//Use the language matcher to get the best match for the locale preference
func InitI18n(locale, dir string) {
pref := language.Make(locale) // falls back to en-US on parse error
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
serverLangs := make([]language.Tag, 1)
serverLangs[0] = language.AmericanEnglish // en-US fallback
for _, file := range files {
if filepath.Ext(file.Name()) == ".ini" {
name := strings.TrimSuffix(file.Name(), ".ini")
tag, err := language.Parse(name)
if err == nil {
serverLangs = append(serverLangs, tag)
}
}
}
matcher := language.NewMatcher(serverLangs)
tag, _, _ := matcher.Match(pref)
fname := filepath.Join(dir, tag.String()+".ini")
conf, err := configparser.Read(fname)
if err != nil {
log.Fatal("cannot read translation file for", tag.String(), err)
}
formats, err := conf.Section("formats")
if err != nil {
log.Fatal("Cannot read formats sections in translations for", tag.String(), err)
}
translations, err := conf.Section("strings")
if err != nil {
log.Fatal("Cannot read strings sections in translations for", tag.String(), err)
}
i18nProvider = &i18n{
translation_dir: dir,
formats: formats.Options(),
translations: translations.Options(),
locale: tag,
}
}
示例9: Init
// Init registers locales ll and default locale def for the entire app.
func Init(ll Locales, def language.Tag) {
if locales != nil {
panic(errors.New("i18n: Init called multiple times"))
}
locales = ll
defaultLocale = def
if !locales.Has(def) {
panic(fmt.Errorf("i18n: default locale %q doesn't exist", def))
}
tt := []language.Tag{def}
for t := range ll {
if t != def {
tt = append(tt, t)
}
}
matcher = language.NewMatcher(tt)
}
示例10: ExampleDictionary
// ExampleDictionary shows how to reduce the amount of data linked into your
// binary by only using the predefined Dictionary variables of the languages you
// wish to support.
func ExampleDictionary() {
tags := []language.Tag{
language.English,
language.German,
language.Japanese,
language.Russian,
}
dicts := []*display.Dictionary{
display.English,
display.German,
display.Japanese,
display.Russian,
}
m := language.NewMatcher(tags)
getDict := func(t language.Tag) *display.Dictionary {
_, i, confidence := m.Match(t)
// Skip this check if you want to support a fall-back language, which
// will be the first one passed to NewMatcher.
if confidence == language.No {
return nil
}
return dicts[i]
}
// The matcher will match Swiss German to German.
n := getDict(language.Make("gsw")).Languages()
fmt.Println(n.Name(language.German))
fmt.Println(n.Name(language.Make("de-CH")))
fmt.Println(n.Name(language.Make("gsw")))
// Output:
// Deutsch
// Schweizer Hochdeutsch
// Schweizerdeutsch
}
示例11: Language
package util
import (
"golang.org/x/text/language"
"net/http"
)
var SupportedLanguageMatcher = language.NewMatcher([]language.Tag{
language.SimplifiedChinese,
language.English,
})
func Language(req *http.Request) language.Tag {
tags, _, _ := language.ParseAcceptLanguage(req.Header.Get("Accept-Language"))
tag, _, _ := SupportedLanguageMatcher.Match(tags...)
return tag
}
示例12: handler
// license that can be found in the LICENSE file.
package language_test
import (
"fmt"
"net/http"
"strings"
"golang.org/x/text/language"
)
// matcher is a language.Matcher configured for all supported languages.
var matcher = language.NewMatcher([]language.Tag{
language.BritishEnglish,
language.Norwegian,
language.German,
})
// handler is a http.HandlerFunc.
func handler(w http.ResponseWriter, r *http.Request) {
t, q, err := language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
// We ignore the error: the default language will be selected for t == nil.
tag, _, _ := matcher.Match(t...)
fmt.Printf("%5v (t: %6v; q: %3v; err: %v)\n", tag, t, q, err)
}
func ExampleParseAcceptLanguage() {
for _, al := range []string{
"nn;q=0.3, en-us;q=0.8, en,",
"gsw, en;q=0.7, en-US;q=0.8",
示例13: iter
func (c *Collator) iter(i int) *iter {
// TODO: evaluate performance for making the second iterator optional.
return &c._iter[i]
}
// Supported returns the list of languages for which collating differs from its parent.
func Supported() []language.Tag {
ids := strings.Split(availableLocales, ",")
tags := make([]language.Tag, len(ids))
for i, s := range ids {
tags[i] = language.Make(s)
}
return tags
}
var matcher = language.NewMatcher(Supported())
// New returns a new Collator initialized for the given locale.
func New(t language.Tag, o ...Option) *Collator {
tt, index, _ := matcher.Match(t)
c := newCollator(colltab.Init(locales[index]))
// Set the default options for the retrieved locale.
c.setFromTag(tt)
// Set options from the user-supplied tag.
c.setFromTag(t)
// Set the user-supplied options.
c.setOptions(o)
示例14: preferredLanguageFrom
package main
import (
"C"
"golang.org/x/text/language"
"log"
)
var (
mather = language.NewMatcher([]language.Tag{language.Make("en"), language.Make("ja"), language.Make("zh-TW"), language.Make("zh-CN")})
)
//export preferredLanguageFrom
func preferredLanguageFrom(httpAcceptLanguage *string) *string {
tag, _, _ := language.ParseAcceptLanguage(*httpAcceptLanguage)
t, _, _ := mather.Match(tag...)
l := t.String()
return &l
}
//export preferredLanguageFromUseCString
func preferredLanguageFromUseCString(cHttpAcceptLanguage *C.char) *C.char {
httpAcceptLanguage := C.GoString(cHttpAcceptLanguage)
tag, _, _ := language.ParseAcceptLanguage(httpAcceptLanguage)
t, _, _ := mather.Match(tag...)
return C.CString(t.String())
}
func init() {
示例15: GetLocale
package admin
import (
"github.com/hypertornado/prago"
"golang.org/x/text/language"
)
var (
supportedLocales = []language.Tag{language.Czech, language.English}
languageMatcher = language.NewMatcher(supportedLocales)
localeNames = map[string]string{
"cs": "Čeština",
"en": "English",
}
availableLocales = [][2]string{{"en", "English"}, {"cs", "Čeština"}}
)
//GetLocale from request
func GetLocale(request prago.Request) string {
user, hasUser := request.GetData("currentuser").(*User)
if hasUser {
if validLocale(user.Locale) {
return user.Locale
}
}
return localeFromRequest(request)
}
func validLocale(in string) bool {
_, ok := localeNames[in]
return ok