Documentation
¶
Index ¶
- Constants
- Variables
- func ToCtx(ctx context.Context, lang string) context.Context
- func TranslationKeysFromSourceCode(dir string) ([]string, error)
- func WithLanguage(ctx context.Context, lang string) (context.Context, error)
- type Key
- type LanguageID
- type Opt
- type Parser
- type RawMessages
- type Translator
Constants ¶
const (
// AttributeKey is the key that is used for the :attribute replacement.
AttributeKey = "attribute"
)
Variables ¶
var (
ErrDuplicateReplacementWithDifferentCase = fmt.Errorf("duplicate replacement with different case")
)
var (
ErrInvalidTranslationKey = fmt.Errorf("restricted translation key: attributes")
)
Functions ¶
func ToCtx ¶
ToCtx is comparable to WithLanguage, but does not return an error when parsing the language fails. On failure it will return the context as is.
func TranslationKeysFromSourceCode ¶
TranslationKeysFromSourceCode finds all translation key's used in go source files. It will parse dir and every subdirectory recursively for go files and search for instances of messages.Key.
func WithLanguage ¶
WithLanguage sets the language in the ctx. Language is parsed to retrieve the language and region. If the region can not reliably be parsed, it is set to an empty string. An error is returned if the language can not be parsed.
Types ¶
type Key ¶
type Key string
Key is a type that represents a translation key. Msgextractor will look for this type in the source code to extract all keys.
type LanguageID ¶
LanguageID holds the language and an optional region.
func FromCtx ¶
func FromCtx(ctx context.Context) LanguageID
LanguageFromCtx returns the language from the ctx.
func ParseLanguage ¶
func ParseLanguage(lang string) (LanguageID, error)
ParseLanguage parses the language string into a LanguageID.
func (LanguageID) Empty ¶
func (l LanguageID) Empty() bool
func (LanguageID) String ¶
func (l LanguageID) String() string
type Opt ¶
type Opt func(*Translator)
Opt is a functional option for the Translator.
func WithDefaultLanguage ¶
func WithDefaultLanguage(lang LanguageID) Opt
Use the given default language when the ctx has no language set or the language has no translations.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func (*Parser) MessagesFromFile ¶
func (p *Parser) MessagesFromFile(filename string) (*RawMessages, error)
RawTranslationsFromFile reads the translations from the given file and returns them as a map.
type RawMessages ¶
func (*RawMessages) MarshalJSON ¶
func (r *RawMessages) MarshalJSON() ([]byte, error)
func (*RawMessages) UnmarshalJSON ¶
func (r *RawMessages) UnmarshalJSON(data []byte) error
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator holds translations for all Languages. Use the Translate message to look up translations.
func NewTranslator ¶
NewTranslator reads all translations from the given directory and returns a new Translator. The directory should contain simple json files with the translations. The filename should be the language code, e.g. en.json.
Translations should be in the format:
{ "validation.required": ":Attribute is required.", "attributes": { "addr_street" : "street" } }
A translation file can have attributes. An attribute changes the replacement value before it is inserted into the translation. This can be useful for validation rules. Field names often have name like first_name or last_name. When using the translation example above:
translator.Translate("validation.required", map[string]any{"attribute": "addr_street"}) Output: Street is required.