func supportedRegions() []language.Region { reg := make([]language.Region, 0, regionIndex.len()) regionIndex.keys(func(s string) { reg = append(reg, language.MustParseRegion(s)) }) return reg }
func ExampleTag_values() { us := language.MustParseRegion("US") en := language.MustParseBase("en") lang, _, region := language.AmericanEnglish.Raw() fmt.Println(lang == en, region == us) lang, _, region = language.BritishEnglish.Raw() fmt.Println(lang == en, region == us) // Tags can be compared for exact equivalence using '=='. en_us, _ := language.Compose(en, us) fmt.Println(en_us == language.AmericanEnglish) // Output: // true true // true false // true }
func TestRegion(t *testing.T) { tests := []struct { dict string reg string name string }{ {"nl", "NL", "Nederland"}, {"en", "US", "United States"}, {"en", "ZZ", "Unknown Region"}, {"en", "UM", "U.S. Outlying Islands"}, {"en-GB", "UM", "U.S. Minor Outlying Islands"}, {"en-GB", "NL", "Netherlands"}, // Canonical equivalents {"en", "UK", "United Kingdom"}, // No region {"en", "pt", "Unknown Region"}, {"en", "und", "Unknown Region"}, // Don't introduce regions with canonicalization. {"en", "mo", "Unknown Region"}, } for i, tt := range tests { d := Regions(language.MustParse(tt.dict)) var x interface{} if unicode.IsUpper(rune(tt.reg[0])) { // Region x = language.MustParseRegion(tt.reg) tag, _ := language.Raw.Compose(x) if n := d.Name(tag); n != tt.name { t.Errorf("%d:%s:%s: was %q; want %q", i, tt.dict, tt.reg, n, tt.name) } } else { // Tag x = language.Raw.MustParse(tt.reg) } if n := d.Name(x); n != tt.name { t.Errorf("%d:%s:%s: was %q; want %q", i, tt.dict, tt.reg, n, tt.name) } } }
// generate builds and writes all tables. func (b *builder) generate() { fmt.Printf(head, *url, *pkg, cldr.Version) b.filter() b.setData("lang", func(g *group, loc language.Tag, ldn *cldr.LocaleDisplayNames) { if ldn.Languages != nil { for _, v := range ldn.Languages.Language { tag := tagForm.MustParse(v.Type) if tags.contains(tag) { g.set(loc, tag.String(), v.Data()) } } } }) b.setData("script", func(g *group, loc language.Tag, ldn *cldr.LocaleDisplayNames) { if ldn.Scripts != nil { for _, v := range ldn.Scripts.Script { g.set(loc, language.MustParseScript(v.Type).String(), v.Data()) } } }) b.setData("region", func(g *group, loc language.Tag, ldn *cldr.LocaleDisplayNames) { if ldn.Territories != nil { for _, v := range ldn.Territories.Territory { g.set(loc, language.MustParseRegion(v.Type).String(), v.Data()) } } }) b.makeSupported() n := b.writeParents() n += b.writeGroup("lang") n += b.writeGroup("script") n += b.writeGroup("region") b.writeSupported() n += b.writeDictionaries() b.supported = []language.Tag{self} // Compute the names of locales in their own language. Some of these names // may be specified in their parent locales. We iterate the maximum depth // of the parent three times to match successive parents of tags until a // possible match is found. for i := 0; i < 4; i++ { b.setData("self", func(g *group, tag language.Tag, ldn *cldr.LocaleDisplayNames) { parent := tag if b, s, r := tag.Raw(); i > 0 && (s != language.Script{} && r == language.Region{}) { parent, _ = language.Raw.Compose(b) } if ldn.Languages != nil { for _, v := range ldn.Languages.Language { key := tagForm.MustParse(v.Type) saved := key if key == parent { g.set(self, tag.String(), v.Data()) } for k := 0; k < i; k++ { key = key.Parent() } if key == tag { g.set(self, saved.String(), v.Data()) // set does not overwrite a value. } } } }) } n += b.writeGroup("self") fmt.Printf("// TOTAL %d Bytes (%d KB)", n, n/1000) }
var ( langTagSet = tagSet{ single: langIndex, long: langTagsLong, } // selfTagSet is used for indexing the language strings in their own // language. selfTagSet = tagSet{ single: selfIndex, long: selfTagsLong, } zzzz = language.MustParseScript("Zzzz") zz = language.MustParseRegion("ZZ") ) // index returns the index of the tag for the given base, script and region or // its parent if the tag is not available. If the match is for a parent entry, // the excess script and region are returned. func (ts *tagSet) index(base language.Base, scr language.Script, reg language.Region) (int, language.Script, language.Region) { lang := base.String() index := -1 if (scr != language.Script{} || reg != language.Region{}) { if scr == zzzz { scr = language.Script{} } if reg == zz { reg = language.Region{} }