• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang language.MustParse函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中golang.org/x/text/language.MustParse函数的典型用法代码示例。如果您正苦于以下问题:Golang MustParse函数的具体用法?Golang MustParse怎么用?Golang MustParse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了MustParse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestParents

func TestParents(t *testing.T) {
	testCases := []struct {
		tag, parent string
	}{
		{"af", "und"},
		{"en", "und"},
		{"en-001", "en"},
		{"en-AU", "en-001"},
		{"en-US", "en"},
		{"en-US-u-va-posix", "en-US"},
		{"ca-ES-valencia", "ca-ES"},
	}
	for _, tc := range testCases {
		tag, ok := language.CompactIndex(language.MustParse(tc.tag))
		if !ok {
			t.Fatalf("Could not get index of flag %s", tc.tag)
		}
		want, ok := language.CompactIndex(language.MustParse(tc.parent))
		if !ok {
			t.Fatalf("Could not get index of parent %s of tag %s", tc.parent, tc.tag)
		}
		if got := int(Parent[tag]); got != want {
			t.Errorf("Parent[%s] = %d; want %d (%s)", tc.tag, got, want, tc.parent)
		}
	}
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:26,代码来源:gen_test.go


示例2: 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
		}
	}()
}
开发者ID:FlashbackSRS,项目名称:flashback,代码行数:27,代码来源:l10n.go


示例3: TestFormats

func TestFormats(t *testing.T) {
	testCases := []struct {
		lang    string
		pattern string
		index   []byte
	}{
		{"en", "#,##0.###", tagToDecimal},
		{"de", "#,##0.###", tagToDecimal},
		{"de-CH", "#,##0.###", tagToDecimal},
		{"pa", "#,##,##0.###", tagToDecimal},
		{"pa-Arab", "#,##0.###", tagToDecimal}, // Does NOT inherit from pa!
		{"mr", "#,##,##0.###", tagToDecimal},
		{"mr-IN", "#,##,##0.###", tagToDecimal}, // Inherits from mr.
		{"nl", "#E0", tagToScientific},
		{"nl-MX", "#E0", tagToScientific}, // Inherits through Tag.Parent.
		{"zgh", "#,##0 %", tagToPercent},
	}
	for _, tc := range testCases {
		testtext.Run(t, tc.lang, func(t *testing.T) {
			got := formatForLang(language.MustParse(tc.lang), tc.index)
			want, _ := ParsePattern(tc.pattern)
			if *got != *want {
				t.Errorf("\ngot  %#v;\nwant %#v", got, want)
			}
		})
	}
}
开发者ID:msoap,项目名称:html2data,代码行数:27,代码来源:number_test.go


示例4: ExampleNamer

func ExampleNamer() {
	supported := []string{
		"en-US", "en-GB", "ja", "zh", "zh-Hans", "zh-Hant", "pt", "pt-PT", "ko", "ar", "el", "ru", "uk", "pa",
	}

	en := display.English.Languages()

	for _, s := range supported {
		t := language.MustParse(s)
		fmt.Printf("%-20s (%s)\n", en.Name(t), display.Self.Name(t))
	}

	// Output:
	// American English     (American English)
	// British English      (British English)
	// Japanese             (日本語)
	// Chinese              (中文)
	// Simplified Chinese   (简体中文)
	// Traditional Chinese  (繁體中文)
	// Portuguese           (português)
	// European Portuguese  (português europeu)
	// Korean               (한국어)
	// Arabic               (العربية)
	// Greek                (Ελληνικά)
	// Russian              (русский)
	// Ukrainian            (українська)
	// Punjabi              (ਪੰਜਾਬੀ)
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:28,代码来源:examples_test.go


示例5: TestUpdate

// TestUpdate tests whether dictionary entries for certain languages need to be
// updated. For some languages, some of the headers may be empty or they may be
// identical to the parent. This code detects if such entries need to be updated
// after a table update.
func TestUpdate(t *testing.T) {
	tests := []struct {
		d   *Dictionary
		tag string
	}{
		{ModernStandardArabic, "ar-001"},
		{AmericanEnglish, "en-US"},
		{EuropeanSpanish, "es-ES"},
		{BrazilianPortuguese, "pt-BR"},
		{SimplifiedChinese, "zh-Hans"},
	}

	for _, tt := range tests {
		_, i, _ := matcher.Match(language.MustParse(tt.tag))
		if !reflect.DeepEqual(tt.d.lang, langHeaders[i]) {
			t.Errorf("%s: lang table update needed", tt.tag)
		}
		if !reflect.DeepEqual(tt.d.script, scriptHeaders[i]) {
			t.Errorf("%s: script table update needed", tt.tag)
		}
		if !reflect.DeepEqual(tt.d.region, regionHeaders[i]) {
			t.Errorf("%s: region table update needed", tt.tag)
		}
	}
}
开发者ID:davidsoloman,项目名称:beats,代码行数:29,代码来源:display_test.go


示例6: TestBinding

func TestBinding(t *testing.T) {
	testCases := []struct {
		tag   string
		value interface{}
		want  string
	}{
		{"en", 1, "1"},
		{"en", "2", "2"},
		{ // Language is passed.
			"en",
			formatFunc(func(fs fmt.State, v rune) {
				s := fs.(format.State)
				io.WriteString(s, s.Language().String())
			}),
			"en",
		},
	}
	for i, tc := range testCases {
		p := NewPrinter(language.MustParse(tc.tag))
		if got := p.Sprint(tc.value); got != tc.want {
			t.Errorf("%d:%s:Sprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
		}
		var buf bytes.Buffer
		p.Fprint(&buf, tc.value)
		if got := buf.String(); got != tc.want {
			t.Errorf("%d:%s:Fprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
		}
	}
}
开发者ID:davidsoloman,项目名称:beats,代码行数:29,代码来源:message_test.go


示例7: TestTag

func TestTag(t *testing.T) {
	tests := []struct {
		dict string
		tag  string
		name string
	}{
		{"agq", "sr", ""}, // sr is in Value.Languages(), but is not supported by agq.
		{"nl", "nl", "Nederlands"},
		{"nl", "nl-BE", "Vlaams"},
		{"en", "en", "English"},
		{"en", "en-GB", "British English"},
		{"en", "en-US", "American English"}, // American English in CLDR 24+
		{"ru", "ru", "русский"},
		{"ru", "ru-RU", "русский (Россия)"},
		{"ru", "ru-Cyrl", "русский (кириллица)"},
		{"en", lastLang2zu.String(), "Zulu"},
		{"en", firstLang2aa.String(), "Afar"},
		{"en", lastLang3zza.String(), "Zaza"},
		{"en", firstLang3ace.String(), "Achinese"},
		{"en", firstTagAr001.String(), "Modern Standard Arabic"},
		{"en", lastTagZhHant.String(), "Traditional Chinese"},
		{"en", "aaa", ""},
		{"en", "zzj", ""},
		// If full tag doesn't match, try without script or region.
		{"en", "aa-Hans", "Afar (Simplified Han)"},
		{"en", "af-Arab", "Afrikaans (Arabic)"},
		{"en", "zu-Cyrl", "Zulu (Cyrillic)"},
		{"en", "aa-GB", "Afar (United Kingdom)"},
		{"en", "af-NA", "Afrikaans (Namibia)"},
		{"en", "zu-BR", "Zulu (Brazil)"},
		// Correct inheritance and language selection.
		{"zh", "zh-TW", "中文 (台湾)"},
		{"zh", "zh-Hant-TW", "繁体中文 (台湾)"},
		{"zh-Hant", "zh-TW", "中文 (台灣)"},
		{"zh-Hant", "zh-Hant-TW", "繁體中文 (台灣)"},
		// Some rather arbitrary interpretations for Serbian. This is arguably
		// correct and consistent with the way zh-[Hant-]TW is handled. It will
		// also give results more in line with the expectations if users
		// explicitly use "sh".
		{"sr-Latn", "sr-ME", "srpski (Crna Gora)"},
		{"sr-Latn", "sr-Latn-ME", "Srpskohrvatski (Crna Gora)"},
		// Double script and region
		{"nl", "en-Cyrl-BE", "Engels (Cyrillisch, België)"},
		// Canonical equivalents.
		{"ro", "ro-MD", "moldovenească"},
		{"ro", "mo", "moldovenească"},
	}
	for i, tt := range tests {
		d := Tags(language.MustParse(tt.dict))
		if n := d.Name(language.Raw.MustParse(tt.tag)); n != tt.name {
			// There are inconsistencies w.r.t. capitalization in the tests
			// due to CLDR's update procedure which treats modern and other
			// languages differently.
			// See http://unicode.org/cldr/trac/ticket/8051.
			// TODO: use language capitalization to sanitize the strings.
			t.Errorf("%d:%s:%s: was %q; want %q", i, tt.dict, tt.tag, n, tt.name)
		}
	}
}
开发者ID:davidsoloman,项目名称:beats,代码行数:59,代码来源:display_test.go


示例8: 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)
}
开发者ID:TriangleGo,项目名称:golang.org,代码行数:8,代码来源:map.go


示例9: TestTables

func TestTables(t *testing.T) {
	if !*long {
		return
	}

	gen.Init()

	// Read the CLDR zip file.
	r := gen.OpenCLDRCoreZip()
	defer r.Close()

	d := &cldr.Decoder{}
	d.SetDirFilter("supplemental", "main")
	d.SetSectionFilter("numbers")
	data, err := d.DecodeZip(r)
	if err != nil {
		t.Fatalf("DecodeZip: %v", err)
	}

	dr, err := cldr.ParseDraft(*draft)
	if err != nil {
		t.Fatalf("filter: %v", err)
	}

	for _, lang := range data.Locales() {
		p := message.NewPrinter(language.MustParse(lang))

		ldml := data.RawLDML(lang)
		if ldml.Numbers == nil || ldml.Numbers.Currencies == nil {
			continue
		}
		for _, c := range ldml.Numbers.Currencies.Currency {
			syms := cldr.MakeSlice(&c.Symbol)
			syms.SelectDraft(dr)

			for _, sym := range c.Symbol {
				cur, err := ParseISO(c.Type)
				if err != nil {
					continue
				}
				formatter := Symbol
				switch sym.Alt {
				case "":
				case "narrow":
					formatter = NarrowSymbol
				default:
					continue
				}
				want := sym.Data()
				if got := p.Sprint(formatter(cur)); got != want {
					t.Errorf("%s:%sSymbol(%s) = %s; want %s", lang, strings.Title(sym.Alt), c.Type, got, want)
				}
			}
		}
	}
}
开发者ID:bigwhite,项目名称:gocmpp,代码行数:56,代码来源:tables_test.go


示例10: initCat

func initCat(entries []entry) (*Catalog, []language.Tag) {
	tags := []language.Tag{}
	cat := newCatalog()
	for _, e := range entries {
		tag := language.MustParse(e.tag)
		tags = append(tags, tag)
		cat.SetString(tag, e.key, e.msg)
	}
	return cat, internal.UniqueTags(tags)
}
开发者ID:CyCoreSystems,项目名称:coreos-kubernetes,代码行数:10,代码来源:catalog_test.go


示例11: getCacheEntry

func (env *CollationEnvironment) getCacheEntry(locale string) collationEnvironmentCacheEntry {
	entry, ok := env.cache[locale]
	if !ok {
		if env.cache == nil {
			env.cache = make(map[string]collationEnvironmentCacheEntry)
		}
		entry = collationEnvironmentCacheEntry{locale, collate.New(language.MustParse(locale))}
		env.cache[locale] = entry
	}
	return entry
}
开发者ID:nvanbenschoten,项目名称:cockroach,代码行数:11,代码来源:datum.go


示例12: 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.
}
开发者ID:yansal,项目名称:pollbc,代码行数:13,代码来源:htmlindex.go


示例13: parseTranslationFile

func (i18n JsonI18n) parseTranslationFile(fullpath string, f os.FileInfo, err error) error {
	if !f.IsDir() {
		rawTag := filepath.Base(filepath.Dir(fullpath))
		tag := language.MustParse(rawTag)
		file, e := ioutil.ReadFile(fullpath)
		if e != nil {
			return e
		}
		return i18n.parseTranslations(file, tag)
	}
	return nil
}
开发者ID:seccijr,项目名称:quintoweb,代码行数:12,代码来源:i18n.go


示例14: dictTags

func dictTags() (tag []language.Tag) {
	// TODO: replace with language.Common.Tags() once supported.
	const str = "af am ar ar-001 az bg bn ca cs da de el en en-US en-GB " +
		"es es-ES es-419 et fa fi fil fr fr-CA gu he hi hr hu hy id is it ja " +
		"ka kk km kn ko ky lo lt lv mk ml mn mr ms my ne nl no pa pl pt pt-BR " +
		"pt-PT ro ru si sk sl sq sr sr-Latn sv sw ta te th tr uk ur uz vi " +
		"zh zh-Hans zh-Hant zu"

	for _, s := range strings.Split(str, " ") {
		tag = append(tag, language.MustParse(s))
	}
	return tag
}
开发者ID:npchp110,项目名称:text,代码行数:13,代码来源:maketables.go


示例15: TestInfo

func TestInfo(t *testing.T) {
	testCases := []struct {
		lang     string
		sym      SymbolType
		wantSym  string
		wantNine rune
	}{
		{"und", SymDecimal, ".", '9'},
		{"de", SymGroup, ".", '9'},
		{"de-BE", SymGroup, ".", '9'},          // inherits from de (no number data in CLDR)
		{"de-BE-oxendict", SymGroup, ".", '9'}, // inherits from de (no compact index)

		// U+096F DEVANAGARI DIGIT NINE ('९')
		{"de-BE-u-nu-deva", SymGroup, ".", '\u096f'}, // miss -> latn -> de
		{"de-Cyrl-BE", SymGroup, ",", '9'},           // inherits from root
		{"de-CH", SymGroup, "'", '9'},                // overrides values in de
		{"de-CH-oxendict", SymGroup, "'", '9'},       // inherits from de-CH (no compact index)
		{"de-CH-u-nu-deva", SymGroup, "'", '\u096f'}, // miss -> latn -> de-CH

		{"pa", SymExponential, "E", '9'},

		// "×۱۰^" -> U+00d7 U+06f1 U+06f0^"
		// U+06F0 EXTENDED ARABIC-INDIC DIGIT ZERO
		// U+06F1 EXTENDED ARABIC-INDIC DIGIT ONE
		// U+06F9 EXTENDED ARABIC-INDIC DIGIT NINE
		{"pa-u-nu-arabext", SymExponential, "\u00d7\u06f1\u06f0^", '\u06f9'},

		//  "གྲངས་མེད" - > U+0f42 U+0fb2 U+0f44 U+0f66 U+0f0b U+0f58 U+0f7a U+0f51
		// Examples:
		// U+0F29 TIBETAN DIGIT NINE (༩)
		{"dz", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'}, // defaults to tibt
		{"dz-u-nu-latn", SymInfinity, "∞", '9'},                                           // select alternative
		{"dz-u-nu-tibt", SymInfinity, "\u0f42\u0fb2\u0f44\u0f66\u0f0b\u0f58\u0f7a\u0f51", '\u0f29'},
		{"en-u-nu-tibt", SymInfinity, "∞", '\u0f29'},

		// algorithmic number systems fall back to ASCII if Digits is used.
		{"en-u-nu-hanidec", SymPlusSign, "+", '9'},
		{"en-u-nu-roman", SymPlusSign, "+", '9'},
	}
	for _, tc := range testCases {
		info := InfoFromTag(language.MustParse(tc.lang))
		if got := info.Symbol(tc.sym); got != tc.wantSym {
			t.Errorf("%s:%v:sym: got %q; want %q", tc.lang, tc.sym, got, tc.wantSym)
		}
		if got := info.Digit('9'); got != tc.wantNine {
			t.Errorf("%s:%v:nine: got %q; want %q", tc.lang, tc.sym, got, tc.wantNine)
		}
	}
}
开发者ID:vmware,项目名称:vic,代码行数:49,代码来源:number_test.go


示例16: doGo

func doGo(tag, caser, input string) string {
	var c Caser
	t := language.MustParse(tag)
	switch caser {
	case "lower":
		c = Lower(t)
	case "upper":
		c = Upper(t)
	case "title":
		c = Title(t)
	case "fold":
		c = Fold()
	}
	return c.String(input)
}
开发者ID:rawlingsj,项目名称:gofabric8,代码行数:15,代码来源:icu_test.go


示例17: Init

// Init 初始化 locale 包并返回当前系统默认的本地化语言信息。
func Init() (language.Tag, error) {
	for id, messages := range locales {
		tag := language.MustParse(id)
		for key, val := range messages {
			message.SetString(tag, key, val)
		}
	}

	localeName, err := getLocaleName()
	if err != nil {
		return language.Und, err
	}

	return language.Parse(localeName)
}
开发者ID:caixw,项目名称:apidoc,代码行数:16,代码来源:locale.go


示例18: TestLanguageDefault

func TestLanguageDefault(t *testing.T) {
	for _, tc := range []struct{ tag, want string }{
		{"und", "windows-1252"}, // The default value.
		{"ar", "windows-1256"},
		{"ba", "windows-1251"},
		{"be", "windows-1251"},
		{"bg", "windows-1251"},
		{"cs", "windows-1250"},
		{"el", "iso-8859-7"},
		{"et", "windows-1257"},
		{"fa", "windows-1256"},
		{"he", "windows-1255"},
		{"hr", "windows-1250"},
		{"hu", "iso-8859-2"},
		{"ja", "shift_jis"},
		{"kk", "windows-1251"},
		{"ko", "euc-kr"},
		{"ku", "windows-1254"},
		{"ky", "windows-1251"},
		{"lt", "windows-1257"},
		{"lv", "windows-1257"},
		{"mk", "windows-1251"},
		{"pl", "iso-8859-2"},
		{"ru", "windows-1251"},
		{"sah", "windows-1251"},
		{"sk", "windows-1250"},
		{"sl", "iso-8859-2"},
		{"sr", "windows-1251"},
		{"tg", "windows-1251"},
		{"th", "windows-874"},
		{"tr", "windows-1254"},
		{"tt", "windows-1251"},
		{"uk", "windows-1251"},
		{"vi", "windows-1258"},
		{"zh-hans", "gb18030"},
		{"zh-hant", "big5"},
		// Variants and close approximates of the above.
		{"ar_EG", "windows-1256"},
		{"bs", "windows-1250"}, // Bosnian Latin maps to Croatian.
		// Use default fallback in case of miss.
		{"nl", "windows-1252"},
	} {
		if got := LanguageDefault(language.MustParse(tc.tag)); got != tc.want {
			t.Errorf("LanguageDefault(%s) = %s; want %s", tc.tag, got, tc.want)
		}
	}
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:47,代码来源:htmlindex_test.go


示例19: TestCatalog

func TestCatalog(t *testing.T) {
	for _, tc := range testCases {
		cat, wantTags := initCat(tc.cat)

		// languages
		if got := cat.Languages(); !reflect.DeepEqual(got, wantTags) {
			t.Errorf("%s:Languages: got %v; want %v", tc.desc, got, wantTags)
		}

		// Lookup
		for _, e := range tc.lookup {
			tag := language.MustParse(e.tag)
			msg, ok := cat.get(tag, e.key)
			if okWant := e.msg != ""; ok != okWant || msg != e.msg {
				t.Errorf("%s:Lookup(%s, %s) = %s, %v; want %s, %v", tc.desc, tag, e.key, msg, ok, e.msg, okWant)
			}
		}
	}
}
开发者ID:CyCoreSystems,项目名称:coreos-kubernetes,代码行数:19,代码来源:catalog_test.go


示例20: testPlurals

func testPlurals(t *testing.T, p *pluralRules, testCases []pluralTest) {
	for _, tc := range testCases {
		for _, loc := range strings.Split(tc.locales, " ") {
			langIndex, _ := language.CompactIndex(language.MustParse(loc))
			// Test integers
			for _, s := range tc.integer {
				a := strings.Split(s, "~")
				from := parseUint(t, a[0])
				to := from
				if len(a) > 1 {
					to = parseUint(t, a[1])
				}
				for n := from; n <= to; n++ {
					if f := matchPlural(p, langIndex, n, 0, 0); f != tc.form {
						t.Errorf("%s:int(%d) = %v; want %v", loc, n, f, tc.form)
					}
				}
			}
			// Test decimals
			for _, s := range tc.decimal {
				a := strings.Split(s, "~")
				from, scale := parseFixedPoint(t, a[0])
				to := from
				if len(a) > 1 {
					var toScale int
					if to, toScale = parseFixedPoint(t, a[1]); toScale != scale {
						t.Fatalf("%s:%s: non-matching scales %d versus %d", loc, s, scale, toScale)
					}
				}
				m := 1
				for i := 0; i < scale; i++ {
					m *= 10
				}
				for n := from; n <= to; n++ {
					if f := matchPlural(p, langIndex, n/m, n%m, scale); f != tc.form {
						t.Errorf("%[1]s:dec(%[2]d.%0[4]*[3]d) = %[5]v; want %[6]v", loc, n/m, n%m, scale, f, tc.form)
					}
				}
			}
		}
	}
}
开发者ID:msoap,项目名称:html2data,代码行数:42,代码来源:plural_test.go



注:本文中的golang.org/x/text/language.MustParse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang language.MustParseRegion函数代码示例发布时间:2022-05-28
下一篇:
Golang language.Make函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap