Documentation
¶
Index ¶
- func WithLanguage(ctx context.Context, raw string) context.Context
- type Container
- func (c *Container) Message(ctx context.Context, key Key, replacements map[string]any) string
- func (c *Container) Messages(lang LanguageID) map[Key]*parser.Message
- func (c *Container) Raw() map[LanguageID]map[string]string
- func (c *Container) Scope(ctx context.Context) *ScopedContainer
- func (c *Container) ScopedLanguage(ctx context.Context) LanguageID
- type ContainerOpt
- type FileMatcher
- type Key
- type LanguageID
- type MergeStrategy
- type RegexMatcher
- type ScopedContainer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithLanguage ¶
WithLanguage parses the given raw language and adds it to the ctx. If the language can not be parsed no language is added to the context. This will make Container.Message fallback to the default container language if set.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
func ContainerFromFs ¶
func ContainerFromFs(fs afero.Fs, opts ...ContainerOpt) (*Container, error)
ContainerFromFs reads all translation files that match the default FileMatcher from the fs.
func ContainerFromFsAndMatcher ¶
func ContainerFromFsAndMatcher(fs afero.Fs, matcher FileMatcher, opts ...ContainerOpt) (*Container, error)
func Merge ¶
func Merge(from *Container, to *Container, strategy MergeStrategy) *Container
Merge merges the messages from the from container to the to container based on the strategy.
func (*Container) Raw ¶
func (c *Container) Raw() map[LanguageID]map[string]string
Raw returns the raw messages from the container.
func (*Container) Scope ¶
func (c *Container) Scope(ctx context.Context) *ScopedContainer
Scope returns a container type with the ctx embedded.
func (*Container) ScopedLanguage ¶
func (c *Container) ScopedLanguage(ctx context.Context) LanguageID
ScopedLanguage returns the language in the context or falls back to no strict matches or the default lang. Returns and empty LanguageID{} if no language can be detected.
type ContainerOpt ¶
type ContainerOpt func(c *Container)
func WithDefaultLanguage ¶
func WithDefaultLanguage(lang LanguageID) ContainerOpt
type FileMatcher ¶
type FileMatcher interface { IsMatch(name string) bool LanguageID(name string) (LanguageID, error) }
FileMatcher is an interface that is used to check if a given file in a directory structure is a match, and should be parsed. It also provides a way to get the language ID from the file name.
type LanguageID ¶
LanguageID holds the language and an optional region.
func FromCtx ¶
func FromCtx(ctx context.Context) LanguageID
FromCtx returns the language from the ctx or an empty language if no language is set.
func MustParseLanguage ¶
func MustParseLanguage(lang string) LanguageID
MustParseLanguage parses the language string into a LanguageID. If the language can not be parsed it will panic.
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) Match ¶
func (l LanguageID) Match(cmp LanguageID) (match bool, strongMatch bool)
func (LanguageID) String ¶
func (l LanguageID) String() string
type MergeStrategy ¶
type MergeStrategy int
const ( // SkipAndClean will skip existing messages and remove the messages from to that are not in from. SkipAndClean MergeStrategy = iota // Skip will skip the merge for messages that already exist in to. Skip // Overwrite will overwrite the messages in to with the messages from from. Overwrite // OverWriteAndClean will overwrite the messages in to with the messages from from and remove the messages from to that are not in from. OverWriteAndClean )
type RegexMatcher ¶
type RegexMatcher struct {
// contains filtered or unexported fields
}
RegexMatcher matches all files in the directory that match the regex. The first capture group is used to extract the language ID.
func NewRegexMatcher ¶
func NewRegexMatcher(re *regexp.Regexp) *RegexMatcher
NewRegexMatcher creates a new RegexMatcher with the given regex.
func (*RegexMatcher) IsMatch ¶
func (m *RegexMatcher) IsMatch(name string) bool
func (*RegexMatcher) LanguageID ¶
func (m *RegexMatcher) LanguageID(name string) (LanguageID, error)
type ScopedContainer ¶
type ScopedContainer struct {
// contains filtered or unexported fields
}