po

package
v2.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2025 License: MIT Imports: 11 Imported by: 0

README

Po

structure

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareEntriesFunc

func CompareEntriesFunc(a, b Entries, cmp Cmp[Entry]) int

func CompareEntry

func CompareEntry(a, b Entry) int

func CompareEntryByFile

func CompareEntryByFile(a, b Entry) int

func CompareEntryByFuzzy

func CompareEntryByFuzzy(a, b Entry) int

func CompareEntryByID

func CompareEntryByID(a, b Entry) int

func CompareEntryByLine

func CompareEntryByLine(a, b Entry) int

func CompareEntryByLocation

func CompareEntryByLocation(a, b Entry) int

func CompareEntryByObsolete

func CompareEntryByObsolete(a, b Entry) int

func CompareEntryByStr

func CompareEntryByStr(a, b Entry) int

func CompareLocation

func CompareLocation(a, b Location) int

func CompareLocationByFile

func CompareLocationByFile(a, b Location) int

func CompareLocationByLine

func CompareLocationByLine(a, b Location) int

func CompareLocationsFunc

func CompareLocationsFunc(a, b Locations, cmp Cmp[Location]) int

func ComparePluralEntriesFunc

func ComparePluralEntriesFunc(a, b PluralEntries, cmp Cmp[PluralEntry]) int

func ComparePluralEntry

func ComparePluralEntry(a, b PluralEntry) int

func ComparePluralEntryByID

func ComparePluralEntryByID(a, b PluralEntry) int

func ComparePluralEntryByStr

func ComparePluralEntryByStr(a, b PluralEntry) int

func EntryIDMatchRatio

func EntryIDMatchRatio(e1, e2 Entry) int

func EntryStrMatchRatio

func EntryStrMatchRatio(e1, e2 Entry) int

Types

type Cmp

type Cmp[X any] func(a, b X) int

type Compiler

type Compiler interface {
	SetFile(*File)
	ToWriter(io.Writer) error
	ToBytes() []byte
}

type Entries

type Entries []Entry

Entries represents a collection of Entry objects.

func Merge

func Merge(def, ref Entries, options ...MergeOption) Entries

func MergeWithConfig

func MergeWithConfig(config MergeConfig, def, ref Entries) Entries

func (Entries) BestIDRatio

func (e Entries) BestIDRatio(e1 Entry) (best, highestRatio int)

func (Entries) BestStrRatio

func (e Entries) BestStrRatio(e1 Entry) (best, highestRatio int)

func (Entries) CleanDuplicates

func (e Entries) CleanDuplicates() Entries

CleanDuplicates removes duplicate entries with the same ID and context, merging their locations.

func (Entries) CleanEmpties added in v2.3.0

func (e Entries) CleanEmpties() Entries

func (Entries) CleanFuzzy

func (e Entries) CleanFuzzy() Entries

func (Entries) CleanObsoletes

func (e Entries) CleanObsoletes() Entries

func (Entries) ContainsFunc added in v2.3.0

func (e Entries) ContainsFunc(f func(e Entry) bool) bool

func (Entries) ContainsUnifiedID

func (e Entries) ContainsUnifiedID(uid string) bool

func (Entries) CutHeader

func (e Entries) CutHeader() Entries

func (Entries) Equal

func (e Entries) Equal(e2 Entries) bool

func (Entries) FuzzyFind

func (e Entries) FuzzyFind(id, context string) int

func (Entries) HasDuplicates

func (e Entries) HasDuplicates() bool

func (Entries) Header

func (e Entries) Header() (h Header)

func (Entries) Index

func (e Entries) Index(id, context string) int

func (Entries) IndexByUnifiedID

func (e Entries) IndexByUnifiedID(uid string) int

func (Entries) IndexFunc added in v2.3.0

func (e Entries) IndexFunc(f func(Entry) bool) int

func (Entries) IsSorted

func (e Entries) IsSorted() bool

func (Entries) IsSortedFunc

func (e Entries) IsSortedFunc(cmp Cmp[Entry]) bool

func (Entries) PrepareSorter

func (e Entries) PrepareSorter(cmp Cmp[Entry]) func() Entries

func (Entries) Solve

func (e Entries) Solve() Entries

Solve is a convenience method that uses the default SolveMerge function to resolve duplicates in Entries. It calls SolveFunc with SolveMerge as the merger function.

func (Entries) SolveFunc

func (e Entries) SolveFunc(merger MergeFunc) Entries

SolveFunc processes a slice of Entries using a provided merging function (merger). It groups entries by a unified identifier (UnifiedID) and merges duplicates using the merger. The result is a cleaned list of Entries with duplicates resolved.

func (Entries) Sort

func (e Entries) Sort() Entries

Sort organizes the entries by grouping them by file and sorting them by line.

func (Entries) SortFunc

func (e Entries) SortFunc(cmp Cmp[Entry]) Entries

func (Entries) Validate added in v2.3.0

func (e Entries) Validate() error

type EntriesOrFile

type EntriesOrFile interface{ Entries | *File | File }

type Entry

type Entry struct {
	Flags             []string
	Comments          []string
	ExtractedComments []string
	Previous          []string

	Obsolete  bool
	ID        string // The original string to be translated.
	Context   string // The context in which the string is used (optional).
	Plural    string // The plural form of the string (optional).
	Plurals   PluralEntries
	Str       string
	Locations Locations // A list of source code locations for the string.
}

Entry represents a translatable string, including its context, plural forms, and source code locations.

func SolveMerge

func SolveMerge(a, b Entry) *Entry

SolveMerge merges two Entry objects based on certain preference criteria. It prefers the Entry with a higher priority according to CompareEntry. It combines the Locations from both entries. Then, it chooses the Str and Plurals fields based on CompareEntryByStr.

func (Entry) Equal

func (e Entry) Equal(x Entry) bool

func (Entry) FullHash added in v2.5.0

func (e Entry) FullHash() uint32

Returns the complete hash of the input ID's, including the msgctxt, msgid and msgid_plural. WARNING: This is NOT compatible with the original gettext.

func (Entry) HasContext

func (e Entry) HasContext() bool

func (Entry) Hash added in v2.3.0

func (e Entry) Hash() uint32

func (Entry) IsFuzzy

func (e Entry) IsFuzzy() bool

func (Entry) IsHeader

func (e Entry) IsHeader() bool

func (Entry) IsPlural

func (e Entry) IsPlural() bool

func (Entry) String

func (e Entry) String() string

func (Entry) UnifiedID

func (e Entry) UnifiedID() string

Returns the unified msgid, msgid_plural and context according to MO format (CTXT \x04 ID (\x00 PLURAL)...).

func (Entry) UnifiedStr

func (e Entry) UnifiedStr() string

Returns the msgstr (STR) or plurals (PluralStr1 \x00 PluralStr2 \x00 PluralStr3...) unified according to the MO format.

func (Entry) Validate

func (e Entry) Validate() error

Check for possible errors and inconsistencies in the entry.

type File

type File struct {
	Name string
	Entries
}

func NewFile

func NewFile(name string, entries ...Entry) *File

func (File) Equal

func (f File) Equal(f2 File) bool

func (File) Load

func (f File) Load(id string, context string) string

func (File) LoadByUnifiedID

func (f File) LoadByUnifiedID(uid string) string

func (*File) Set

func (f *File) Set(id, context string, e Entry)
type Header struct {
	Fields []HeaderField // A slice storing all registered header fields.
}

Header represents a collection of header fields.

func DefaultTemplateHeader

func DefaultTemplateHeader() (h Header)

DefaultTemplateHeader initializes a Header object with commonly used default fields. These fields are typically found in .po files for localization.

func NewHeader

func NewHeader(options ...HeaderOption) Header

func (*Header) Load

func (h *Header) Load(key string) string

Load retrieves the value associated with a given key from the Header object. Parameters: - key: The name of the header field to retrieve. Returns: - The value associated with the key if found; otherwise, an empty string ("").

func (Header) Nplurals

func (h Header) Nplurals() (nplurals uint)

func (*Header) Register

func (h *Header) Register(key string, d ...string)

Register adds a new header field to the Header object if the key does not already exist. Parameters:

  • key: The name of the header field to register.
  • d: Optional variadic arguments representing the value(s) to associate with the key. If provided, they are concatenated into a single string using fmt.Sprint.

func (*Header) Set

func (h *Header) Set(key, value string)

Set updates the value of an existing header field or adds a new field if the key does not exist. Parameters: - key: The name of the header field to update or add. - value: The new value to associate with the key.

func (Header) ToEntry

func (h Header) ToEntry() (e Entry)

type HeaderConfig

type HeaderConfig struct {
	Nplurals          uint
	ProjectIDVersion  string
	ReportMsgidBugsTo string
	Language          string
	LanguageTeam      string
	LastTranslator    string
}

func DefaultHeaderConfig

func DefaultHeaderConfig() HeaderConfig

func HeaderConfigFromOptions

func HeaderConfigFromOptions(options ...HeaderOption) HeaderConfig

func (HeaderConfig) ToHeader

func (cfg HeaderConfig) ToHeader() (h Header)

func (HeaderConfig) ToHeaderWithDefaults

func (cfg HeaderConfig) ToHeaderWithDefaults() (h Header)

type HeaderField

type HeaderField struct {
	Key   string // The name of the header field.
	Value string // The value associated with the header field.
}

HeaderField represents a single key-value pair in a header.

type HeaderOption

type HeaderOption func(*HeaderConfig)

func HeaderWithConfig

func HeaderWithConfig(c HeaderConfig) HeaderOption

func HeaderWithLanguage

func HeaderWithLanguage(lang string) HeaderOption

func HeaderWithLanguageTeam

func HeaderWithLanguageTeam(team string) HeaderOption

func HeaderWithLastTranslator

func HeaderWithLastTranslator(translator string) HeaderOption

func HeaderWithNplurals

func HeaderWithNplurals(n uint) HeaderOption

func HeaderWithProjectIDVersion

func HeaderWithProjectIDVersion(v string) HeaderOption

func HeaderWithReportMsgidBugsTo

func HeaderWithReportMsgidBugsTo(r string) HeaderOption

type Location

type Location struct {
	Line int
	File string
}

Location represents the location of a translation string in the source code.

func (Location) Equal

func (l Location) Equal(l2 Location) bool

type Locations

type Locations []Location

func (Locations) Equal

func (l Locations) Equal(l2 Locations) bool

func (Locations) IsSorted

func (l Locations) IsSorted() bool

func (Locations) IsSortedFunc

func (l Locations) IsSortedFunc(cmp Cmp[Location]) bool

func (Locations) Sort

func (l Locations) Sort() Locations

func (Locations) SortFunc

func (l Locations) SortFunc(cmp Cmp[Location]) Locations

type MergeConfig

type MergeConfig struct {
	FuzzyMatch      bool
	KeepPreviousIDs bool
	Sort            bool
	SortMode        SortMode
}

func DefaultMergeConfig

func DefaultMergeConfig(opts ...MergeOption) MergeConfig

func (*MergeConfig) ApplyOption

func (m *MergeConfig) ApplyOption(opts ...MergeOption)

type MergeFunc

type MergeFunc func(a, b Entry) *Entry

MergeFunc defines a function type that takes two Entry objects and returns a merged Entry pointer.

func MergeAndMarkObsoleteIfNotPrioritized

func MergeAndMarkObsoleteIfNotPrioritized(priorList []string) MergeFunc

MergeAndMarkObsoleteIfNotPrioritized returns a MergeFunc that merges two entries using SolveMerge. If the UnifiedID of the entry is not found in the priorList, the resulting merged entry is marked as obsolete. This is useful for keeping non-prioritized entries while flagging them as deprecated or no longer in use.

func MergeIfInPriorityList

func MergeIfInPriorityList(priorList []string) MergeFunc

MergeIfInPriorityList returns a MergeFunc that applies SolveMerge only if the entry's UnifiedID is present in the provided list of priority IDs (priorList). If the UnifiedID is not found in the list, the merge is skipped and nil is returned.

func MergeUsingPriorAsBase

func MergeUsingPriorAsBase(priorList Entries) MergeFunc

MergeUsingPriorAsBase returns a MergeFunc that prefers using the version of the entry from priorList as the base. If a matching UnifiedID is found in priorList, it merges that entry with 'b'. Otherwise, it merges 'a' and 'b' as usual using SolveMerge.

func MergeUsingPriorityOrFallback

func MergeUsingPriorityOrFallback(priorList Entries) MergeFunc

MergeUsingPriorityOrFallback returns a MergeFunc that prioritizes the version from priorList if found, otherwise it falls back to SolveMerge.

type MergeOption

type MergeOption func(mc *MergeConfig)

func MergeWithFuzzyMatch

func MergeWithFuzzyMatch(f bool) MergeOption

func MergeWithKeepPreviousIDs

func MergeWithKeepPreviousIDs(k bool) MergeOption

func MergeWithMergeConfig

func MergeWithMergeConfig(n MergeConfig) MergeOption

func MergeWithSort

func MergeWithSort(s bool) MergeOption

func MergeWithSortMode

func MergeWithSortMode(sm SortMode) MergeOption

type Parser

type Parser interface {
	Parse() *File
	Error() error
	Errors() []error
}

type PluralEntries

type PluralEntries []PluralEntry

func (PluralEntries) Equal

func (p PluralEntries) Equal(p2 PluralEntries) bool

func (PluralEntries) IsSorted

func (p PluralEntries) IsSorted() bool

func (PluralEntries) IsSortedFunc

func (p PluralEntries) IsSortedFunc(cmp Cmp[PluralEntry]) bool

func (PluralEntries) Solve

func (p PluralEntries) Solve() PluralEntries

func (PluralEntries) Sort

func (p PluralEntries) Sort() PluralEntries

func (PluralEntries) SortFunc

func (p PluralEntries) SortFunc(cmp Cmp[PluralEntry]) PluralEntries

type PluralEntry

type PluralEntry struct {
	ID  int
	Str string
}

func (PluralEntry) Equal

func (p PluralEntry) Equal(p2 PluralEntry) bool

type SortMode

type SortMode int
const (
	SortByAll SortMode = iota
	SortByID
	SortByFile
	SortByLine
	SortByFuzzy
	SortByObsolete
)

func (SortMode) SortMethod

func (mode SortMode) SortMethod(entries Entries) func() Entries

Directories

Path Synopsis
The compile package contains structures and functions used to compile Mo/Po files with their respective options
The compile package contains structures and functions used to compile Mo/Po files with their respective options
The parse package contains structures and functions for parsing Mo/Po files with the respective options.
The parse package contains structures and functions for parsing Mo/Po files with the respective options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL