func supportedScripts() []language.Script { scr := make([]language.Script, 0, scriptIndex.len()) scriptIndex.keys(func(s string) { scr = append(scr, language.MustParseScript(s)) }) return scr }
func TestDictionaryScript(t *testing.T) { tests := []struct { d *Dictionary script string name string }{ {English, "Cyrl", "Cyrillic"}, {Portuguese, "Gujr", "gujerati"}, {EuropeanPortuguese, "Gujr", "guzerate"}, } for i, test := range tests { tag := language.MustParseScript(test.script) if got := test.d.Scripts().Name(tag); got != test.name { t.Errorf("%d:%v: got %s; want %s", i, tag, got, test.name) } } }
func TestScript(t *testing.T) { tests := []struct { dict string scr string name string }{ {"nl", "Arab", "Arabisch"}, {"en", "Arab", "Arabic"}, {"en", "Zzzz", "Unknown Script"}, {"zh-Hant", "Hang", "韓文字"}, {"zh-Hant-HK", "Hang", "韓文字"}, {"zh", "Arab", "阿拉伯文"}, {"zh-Hans-HK", "Arab", "阿拉伯文"}, // same as zh {"zh-Hant", "Arab", "阿拉伯文"}, {"zh-Hant-HK", "Arab", "阿拉伯文"}, // same as zh // Canonicalized form {"en", "Qaai", "Inherited"}, // deprecated script, now is Zinh {"en", "sh", "Unknown Script"}, // sh canonicalizes to sr-Latn {"en", "en", "Unknown Script"}, // Don't introduce scripts with canonicalization. {"en", "sh", "Unknown Script"}, // sh canonicalizes to sr-Latn } for i, tt := range tests { d := Scripts(language.MustParse(tt.dict)) var x interface{} if unicode.IsUpper(rune(tt.scr[0])) { x = language.MustParseScript(tt.scr) 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.scr, n, tt.name) } } else { x = language.Raw.MustParse(tt.scr) } if n := d.Name(x); n != tt.name { t.Errorf("%d:%s:%s: was %q; want %q", i, tt.dict, tt.scr, n, tt.name) } } }
} 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{}
// generate builds and writes all tables. func (b *builder) generate() { fmt.Fprintf(b.w, versionInfo, 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.Fprintf(b.w, "// TOTAL %d Bytes (%d KB)", n, n/1000) }
// generate builds and writes all tables. func (b *builder) generate() { fmt.Fprintf(b.w, versionInfo, 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 { code := language.MustParseScript(v.Type) if code.IsPrivateUse() { // Qaaa..Qabx // TODO: data currently appears to be very meager. // Reconsider if we have data for English. if loc == language.English { log.Fatal("Consider including data for private use scripts.") } continue } g.set(loc, code.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() b.writeParents() b.writeGroup("lang") b.writeGroup("script") b.writeGroup("region") b.w.WriteConst("numSupported", len(b.supported)) buf := bytes.Buffer{} for _, tag := range b.supported { fmt.Fprint(&buf, tag.String(), "|") } b.w.WriteConst("supported", buf.String()) 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. } } } }) } b.writeGroup("self") }