...

Source file src/github.com/ory/fosite/i18n/default_catalog.go

Documentation: github.com/ory/fosite/i18n

     1  package i18n
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"golang.org/x/text/language"
     7  	"golang.org/x/text/message"
     8  )
     9  
    10  // DefaultMessage is a single message in the locale bundle
    11  // identified by 'ID'.
    12  type DefaultMessage struct {
    13  	ID               string `json:"id"`
    14  	FormattedMessage string `json:"msg"`
    15  }
    16  
    17  // DefaultLocaleBundle is a bundle of messages for the specified
    18  // locale. The language tag can be arbitrary to allow for
    19  // unsupported/unknown languages used by custom clients.
    20  type DefaultLocaleBundle struct {
    21  	LangTag  string            `json:"lang"`
    22  	Messages []*DefaultMessage `json:"messages"`
    23  }
    24  
    25  // defaultMessageCatalog is a catalog of all locale bundles.
    26  type defaultMessageCatalog struct {
    27  	Bundles []*DefaultLocaleBundle
    28  
    29  	matcher language.Matcher
    30  }
    31  
    32  func NewDefaultMessageCatalog(bundles []*DefaultLocaleBundle) MessageCatalog {
    33  	c := &defaultMessageCatalog{
    34  		Bundles: bundles,
    35  	}
    36  
    37  	for _, v := range c.Bundles {
    38  		if err := v.Init(); err != nil {
    39  			continue
    40  		}
    41  	}
    42  
    43  	c.makeMatcher()
    44  	return c
    45  }
    46  
    47  // Init initializes the default catalog with the
    48  // list of messages. The lang tag must parse, otherwise this
    49  // func will panic.
    50  func (l *DefaultLocaleBundle) Init() error {
    51  	tag := language.MustParse(l.LangTag)
    52  	for _, m := range l.Messages {
    53  		if err := message.SetString(tag, m.ID, m.FormattedMessage); err != nil {
    54  			return err
    55  		}
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func (c *defaultMessageCatalog) GetMessage(ID string, tag language.Tag, v ...interface{}) string {
    62  	matchedTag, _, _ := c.matcher.Match(tag)
    63  	p := message.NewPrinter(matchedTag)
    64  
    65  	result := p.Sprintf(ID, v...)
    66  	if result == ID && tag != language.English {
    67  		return c.GetMessage(ID, language.English, v...)
    68  	}
    69  
    70  	return result
    71  }
    72  
    73  func (c *defaultMessageCatalog) GetLangFromRequest(r *http.Request) language.Tag {
    74  	lang, _ := r.Cookie("lang")
    75  	accept := r.Header.Get("Accept-Language")
    76  	tag, _ := language.MatchStrings(c.matcher, lang.String(), accept)
    77  
    78  	return tag
    79  }
    80  
    81  func (c *defaultMessageCatalog) makeMatcher() {
    82  	result := []language.Tag{language.English}
    83  	defLangs := message.DefaultCatalog.Languages()
    84  	// remove "en" if was already in the list of languages
    85  	for i, t := range defLangs {
    86  		if t == language.English {
    87  			result = append(result, defLangs[:i]...)
    88  			result = append(result, defLangs[i+1:]...)
    89  		}
    90  	}
    91  
    92  	c.matcher = language.NewMatcher(defLangs)
    93  }
    94  

View as plain text