本文整理汇总了Golang中golang.org/x/text/language.Tag类的典型用法代码示例。如果您正苦于以下问题:Golang Tag类的具体用法?Golang Tag怎么用?Golang Tag使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tag类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: formatForLang
func formatForLang(t language.Tag, index []byte) *Format {
for ; ; t = t.Parent() {
if x, ok := language.CompactIndex(t); ok {
return &formats[index[x]]
}
}
}
示例2: set
func (p *I18n) set(lng *language.Tag, code, message string) {
lang := lng.String()
if _, ok := p.Locales[lang]; !ok {
p.Locales[lang] = make(map[string]string)
}
p.Locales[lang][code] = message
}
示例3: Translate
// Translate one or more strings of text from a source language to a target
// language. All inputs must be in the same language.
//
// The target parameter supplies the language to translate to. The supported
// languages are listed at
// https://cloud.google.com/translate/v2/translate-reference#supported_languages.
// You can also call the SupportedLanguages method.
//
// The returned Translations appear in the same order as the inputs.
func (c *Client) Translate(ctx context.Context, inputs []string, target language.Tag, opts *Options) ([]Translation, error) {
call := c.raw.Translations.List(inputs, target.String()).Context(ctx)
if opts != nil {
if s := opts.Source; s != language.Und {
call.Source(s.String())
}
if f := opts.Format; f != "" {
call.Format(string(f))
}
if m := opts.Model; m != "" {
call.Model(m)
}
}
res, err := call.Do()
if err != nil {
return nil, err
}
var ts []Translation
for _, t := range res.Translations {
var source language.Tag
if t.DetectedSourceLanguage != "" {
source, err = language.Parse(t.DetectedSourceLanguage)
if err != nil {
return nil, err
}
}
ts = append(ts, Translation{
Text: t.TranslatedText,
Source: source,
Model: t.Model,
})
}
return ts, nil
}
示例4: InfoFromTag
// InfoFromTag returns a Info for the given language tag.
func InfoFromTag(t language.Tag) Info {
for {
if index, ok := language.CompactIndex(t); ok {
return InfoFromLangID(index, t.TypeForKey("nu"))
}
t = t.Parent()
}
}
示例5: Get
//Get get locale
func (p *DatabaseProvider) Get(lng *language.Tag, code string) string {
var l Locale
if err := p.Db.Where("lang = ? AND code = ?", lng.String(), code).First(&l).Error; err != nil {
p.Logger.Error(err)
}
return l.Message
}
示例6: T
//T translate by lang tag
func (p *I18n) T(lng *language.Tag, code string, args ...interface{}) string {
lang := lng.String()
msg := p.Provider.Get(lng, code)
if len(msg) == 0 {
if items, ok := p.Locales[lang]; ok {
msg = items[code]
}
}
return fmt.Sprintf(msg, args...)
}
示例7: Tailoring
// Tailoring returns a Tailoring for the given locale. One should
// have completed all calls to Add before calling Tailoring.
func (b *Builder) Tailoring(loc language.Tag) *Tailoring {
t := &Tailoring{
id: loc.String(),
builder: b,
index: b.root.clone(),
}
t.index.id = t.id
b.locale = append(b.locale, t)
return t
}
示例8: ldmlBool
func ldmlBool(t language.Tag, old bool, key string) bool {
switch t.TypeForKey(key) {
case "true":
return true
case "false":
return false
default:
return old
}
}
示例9: FromTag
// FromTag reports the most likely currency for the given tag. It considers the
// currency defined in the -u extension and infers the region if necessary.
func FromTag(t language.Tag) (Unit, language.Confidence) {
if cur := t.TypeForKey("cu"); len(cur) == 3 {
c, _ := ParseISO(cur)
return c, language.Exact
}
r, conf := t.Region()
if cur, ok := FromRegion(r); ok {
return cur, conf
}
return Unit{}, language.No
}
示例10: GetLanguages
// GetLanguages returns a list of languages as a key/value slice. Odd index/key = locale,
// even index/value = Humanized readable string. The humanized strings contains the language
// name in its language and language name in requested tag
func GetLanguages(t language.Tag) []string {
var ret = make([]string, len(tags)*2)
n := getDict(t)
i := 0
for _, t := range tags {
b, _ := t.Base()
r, _ := t.Region()
ret[i] = GetLocale(b, r)
ret[i+1] = fmt.Sprintf("%-20s (%s)", display.Self.Name(t), n.Languages().Name(t))
i = i + 2
}
return ret
}
示例11: FromTag
// FromTag reports the most likely currency for the given tag. It considers the
// currency defined in the -u extension and infers the region if necessary.
func FromTag(t language.Tag) (Currency, language.Confidence) {
if cur := t.TypeForKey("cu"); len(cur) == 3 {
var buf [3]byte
copy(buf[:], cur)
tag.FixCase("XXX", buf[:])
if x := currency.Index(buf[:]); x > 0 {
return Currency{uint16(x)}, language.Exact
}
}
r, conf := t.Region()
if cur, ok := FromRegion(r); ok {
return cur, conf
}
return Currency{}, language.No
}
示例12: Set
//Set set locale
func (p *DatabaseProvider) Set(lng *language.Tag, code, message string) {
var l Locale
var err error
if p.Db.Where("lang = ? AND code = ?", lng.String(), code).First(&l).RecordNotFound() {
l.Lang = lng.String()
l.Code = code
l.Message = message
err = p.Db.Create(&l).Error
} else {
l.Message = message
err = p.Db.Save(&l).Error
}
if err != nil {
p.Logger.Error(err)
}
}
示例13: get
func (c *Catalog) get(tag language.Tag, key string) (msg string, ok bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
for ; ; tag = tag.Parent() {
if msgs, ok := c.index[tag]; ok {
if statement, ok := msgs[key]; ok {
// TODO: use type switches when we implement selecting.
msg := string(statement.(format.String))
return msg, true
}
}
if tag == language.Und {
break
}
}
return "", false
}
示例14: SupportedLanguages
// SupportedLanguages returns a list of supported languages for translation.
// The target parameter is the language to use to return localized, human
// readable names of supported languages.
func (c *Client) SupportedLanguages(ctx context.Context, target language.Tag) ([]Language, error) {
call := c.raw.Languages.List().Context(ctx).Target(target.String())
res, err := call.Do()
if err != nil {
return nil, err
}
var ls []Language
for _, l := range res.Languages {
tag, err := language.Parse(l.Language)
if err != nil {
return nil, err
}
ls = append(ls, Language{
Name: l.Name,
Tag: tag,
})
}
return ls, nil
}
示例15: setFromTag
func (o *options) setFromTag(t language.Tag) {
o.caseLevel = ldmlBool(t, o.caseLevel, "kc")
o.backwards = ldmlBool(t, o.backwards, "kb")
o.numeric = ldmlBool(t, o.numeric, "kn")
// Extract settings from the BCP47 u extension.
switch t.TypeForKey("ks") { // strength
case "level1":
o.ignore[colltab.Secondary] = true
o.ignore[colltab.Tertiary] = true
case "level2":
o.ignore[colltab.Tertiary] = true
case "level3", "":
// The default.
case "level4":
o.ignore[colltab.Quaternary] = false
case "identic":
o.ignore[colltab.Quaternary] = false
o.ignore[colltab.Identity] = false
}
switch t.TypeForKey("ka") {
case "shifted":
o.alternate = altShifted
// The following two types are not official BCP47, but we support them to
// give access to this otherwise hidden functionality. The name blanked is
// derived from the LDML name blanked and posix reflects the main use of
// the shift-trimmed option.
case "blanked":
o.alternate = altBlanked
case "posix":
o.alternate = altShiftTrimmed
}
// TODO: caseFirst ("kf"), reorder ("kr"), and maybe variableTop ("vt").
// Not used:
// - normalization ("kk", not necessary for this implementation)
// - hiraganaQuatenary ("kh", obsolete)
}