...

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

Documentation: github.com/ory/fosite/i18n

     1  package i18n
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"golang.org/x/text/language"
     7  )
     8  
     9  // MessageCatalog declares the interface to get globalized messages
    10  type MessageCatalog interface {
    11  	GetMessage(ID string, tag language.Tag, v ...interface{}) string
    12  	GetLangFromRequest(r *http.Request) language.Tag
    13  }
    14  
    15  // GetMessage is a helper func to get the translated message based on
    16  // the message ID and lang. If no matching message is found, it uses
    17  // ID as the message itself.
    18  func GetMessage(c MessageCatalog, ID string, tag language.Tag, v ...interface{}) string {
    19  	return GetMessageOrDefault(c, ID, tag, ID, v...)
    20  }
    21  
    22  // GetMessageOrDefault is a helper func to get the translated message based on
    23  // the message ID and lang. If no matching message is found, it returns the
    24  // 'def' message.
    25  func GetMessageOrDefault(c MessageCatalog, ID string, tag language.Tag, def string, v ...interface{}) string {
    26  	if c != nil {
    27  		if s := c.GetMessage(ID, tag, v...); s != ID {
    28  			return s
    29  		}
    30  	}
    31  
    32  	return def
    33  }
    34  
    35  // GetLangFromRequest is a helper func to get the language tag based on the
    36  // HTTP request and the constructed message catalog.
    37  func GetLangFromRequest(c MessageCatalog, r *http.Request) language.Tag {
    38  	if c != nil {
    39  		return c.GetLangFromRequest(r)
    40  	}
    41  
    42  	return language.English
    43  }
    44  

View as plain text