Documentation
¶
Overview ¶
The compile package contains structures and functions used to compile Mo/Po files with their respective options
Index ¶
- Constants
- Variables
- func Highlight(cfg *HighlightConfig, name, input string) ([]byte, error)
- func HighlightFromBytes(cfg *HighlightConfig, name string, input []byte) ([]byte, error)
- func HighlightFromReader(cfg *HighlightConfig, name string, input io.Reader) ([]byte, error)
- func MoToBytes[T po.EntriesOrFile](f T, opts ...MoOption) []byte
- func MoToFile[T po.EntriesOrFile](f T, path string, opts ...MoOption) error
- func MoToWriter[T po.EntriesOrFile](f T, w io.Writer, opts ...MoOption) error
- func PoToBytes[T po.EntriesOrFile](f T, opts ...PoOption) []byte
- func PoToFile[T po.EntriesOrFile](f T, path string, opts ...PoOption) error
- func PoToString[T po.EntriesOrFile](f T, opts ...PoOption) string
- func PoToWriter[T po.EntriesOrFile](f T, w io.Writer, opts ...PoOption) error
- type Endianness
- type HighlightConfig
- type MoCompiler
- func (mc *MoCompiler) SetFile(f *po.File)
- func (mc MoCompiler) ToBytes() []byte
- func (mc *MoCompiler) ToBytesWithOptions(options ...MoOption) []byte
- func (mc MoCompiler) ToFile(f string) error
- func (mc *MoCompiler) ToFileWithOptions(f string, options ...MoOption) error
- func (mc MoCompiler) ToWriter(w io.Writer) error
- func (mc *MoCompiler) ToWriterWithOptions(w io.Writer, opts ...MoOption) error
- type MoConfig
- type MoOption
- type PoCompiler
- func (c *PoCompiler) SetFile(f *po.File)
- func (c PoCompiler) ToBytes() []byte
- func (c *PoCompiler) ToBytesWithOptions(opts ...PoOption) []byte
- func (c PoCompiler) ToFile(f string) error
- func (c *PoCompiler) ToFileWithOptions(f string, opts ...PoOption) error
- func (c PoCompiler) ToString() string
- func (c *PoCompiler) ToStringWithOptions(opts ...PoOption) string
- func (c PoCompiler) ToWriter(w io.Writer) error
- func (c *PoCompiler) ToWriterWithOptions(w io.Writer, opts ...PoOption) error
- type PoConfig
- type PoLocationMode
- type PoOption
- func PoWithAddLocation(loc PoLocationMode) PoOption
- func PoWithCommentFuzzy(c bool) PoOption
- func PoWithConfig(cfg PoConfig) PoOption
- func PoWithCopyrightHolder(holder string) PoOption
- func PoWithCustomObsoletePrefixRune(r rune) PoOption
- func PoWithForcePo(f bool) PoOption
- func PoWithForeignUser(f bool) PoOption
- func PoWithHeaderComments(hc bool) PoOption
- func PoWithHeaderFields(w bool) PoOption
- func PoWithHighlight(h *HighlightConfig) PoOption
- func PoWithIgnoreErrors(i bool) PoOption
- func PoWithLogger(logger *log.Logger) PoOption
- func PoWithMsgstrPrefix(prefix string) PoOption
- func PoWithMsgstrSuffix(suffix string) PoOption
- func PoWithNoLocation(n bool) PoOption
- func PoWithOmitHeader(o bool) PoOption
- func PoWithPackageName(name string) PoOption
- func PoWithTitle(t string) PoOption
- func PoWithUseCustomObsoletePrefix(u bool) PoOption
- func PoWithVerbose(v bool) PoOption
- func PoWithWordWrap(w bool) PoOption
Constants ¶
const ( LittleEndian = util.LittleEndian BigEndian = util.BigEndian NativeEndian = util.NativeEndian )
Variables ¶
var DefaultHighlight = &HighlightConfig{color.Magenta, color.Blue, color.Green}
Functions ¶
func Highlight ¶ added in v2.5.0
func Highlight(cfg *HighlightConfig, name, input string) ([]byte, error)
func HighlightFromBytes ¶ added in v2.5.0
func HighlightFromBytes(cfg *HighlightConfig, name string, input []byte) ([]byte, error)
func HighlightFromReader ¶ added in v2.5.0
func MoToWriter ¶
func PoToString ¶
func PoToString[T po.EntriesOrFile](f T, opts ...PoOption) string
func PoToWriter ¶
Types ¶
type Endianness ¶ added in v2.3.0
type Endianness = util.Endianness
type HighlightConfig ¶ added in v2.4.0
type MoCompiler ¶
type MoCompiler struct {
// File is the PO file to be compiled
File *po.File
// Config contains the compilation configuration
Config MoConfig
}
MoCompiler implements the po.Compiler interface for compiling PO files to MO format. It holds a PO file reference and configuration settings for the compilation process.
func NewMo ¶
func NewMo(file *po.File, opts ...MoOption) MoCompiler
NewMo creates a new MoCompiler instance with the given PO file and optional configuration. It applies any provided MoOption functions to configure the compiler.
func (*MoCompiler) SetFile ¶
func (mc *MoCompiler) SetFile(f *po.File)
SetFile updates the PO file reference in the compiler.
func (MoCompiler) ToBytes ¶
func (mc MoCompiler) ToBytes() []byte
ToBytes returns the compiled MO data as a byte slice. This is useful for in-memory operations or when you need the raw binary data.
func (*MoCompiler) ToBytesWithOptions ¶
func (mc *MoCompiler) ToBytesWithOptions(options ...MoOption) []byte
ToBytesWithOptions returns the compiled MO data as a byte slice with temporary options. The options are only applied for this operation and then reverted.
func (MoCompiler) ToFile ¶
func (mc MoCompiler) ToFile(f string) error
ToFile writes the compiled MO output to the specified file path. By default, it fails if the file already exists (unless Force is enabled). Returns an error if file operations or compilation fails.
func (*MoCompiler) ToFileWithOptions ¶
func (mc *MoCompiler) ToFileWithOptions(f string, options ...MoOption) error
ToFileWithOptions writes the compiled MO output to a file with temporary options. The options are only applied for this operation and then reverted. Returns an error if file operations or compilation fails.
func (MoCompiler) ToWriter ¶
func (mc MoCompiler) ToWriter(w io.Writer) error
ToWriter writes the compiled MO output to an io.Writer. It uses buffered writing for better performance with large files. Returns an error if compilation or writing fails.
func (*MoCompiler) ToWriterWithOptions ¶
func (mc *MoCompiler) ToWriterWithOptions(w io.Writer, opts ...MoOption) error
ToWriterWithOptions writes the compiled MO output to an io.Writer with temporary options. The options are only applied for this operation and then reverted. Returns an error if compilation or writing fails.
type MoConfig ¶
type MoConfig struct {
// The logger can be nil, otherwise this logger will be used to print all errors by default.
Logger *log.Logger
// If true, it still writes to the file if it already exists, in the method [MoCompiler.ToFile].
Force bool
// If true, process information and warnings are also printed.
Verbose bool
IgnoreErrors bool
Endianness Endianness
// If true, compiles the hash table.
HashTable bool
// contains filtered or unexported fields
}
func DefaultMoConfig ¶
DefaultMoConfig creates a new MoConfig with default values. Applies any provided options during creation.
func (*MoConfig) ApplyOptions ¶
Overwrite the configuration with the options provided, saving the previous state so that it can be restored later with MoConfig.RestoreLastCfg if desired.
func (*MoConfig) RestoreLastCfg ¶
func (mc *MoConfig) RestoreLastCfg()
Restores the configuration state prior to the last MoConfig.ApplyOptions if it exists, otherwise it does nothing.
type MoOption ¶
type MoOption func(c *MoConfig)
MoOption defines functions that modify MoConfig
func MoWithConfig ¶
MoWithConfig replaces the entire configuration
func MoWithEndianness ¶ added in v2.3.0
func MoWithEndianness(e Endianness) MoOption
MoWithEndianness sets the byte order for MO file output
func MoWithHashTable ¶ added in v2.3.0
MoWithHashTable toggles hash table generation
func MoWithIgnoreErrors ¶
MoWithIgnoreErrors toggles error suppression
type PoCompiler ¶
type PoCompiler struct {
// File is the source PO file containing translation entries
File *po.File
// Config contains compilation settings and options
Config PoConfig
// contains filtered or unexported fields
}
func NewPo ¶
func NewPo(file *po.File, options ...PoOption) PoCompiler
NewPo creates a new PoCompiler instance with the given PO file and options. The options are applied to configure the compiler behavior.
func (*PoCompiler) SetFile ¶
func (c *PoCompiler) SetFile(f *po.File)
SetFile updates the PO file reference in the compiler.
func (PoCompiler) ToBytes ¶
func (c PoCompiler) ToBytes() []byte
ToBytes returns the compiled output as a byte slice.
func (*PoCompiler) ToBytesWithOptions ¶
func (c *PoCompiler) ToBytesWithOptions(opts ...PoOption) []byte
ToBytesWithOptions returns the compiled output as bytes with temporary options. The options are only applied for this operation and then reverted.
func (PoCompiler) ToFile ¶
func (c PoCompiler) ToFile(f string) error
ToFile writes compiled output to the specified file path. By default fails if file exists (unless ForcePo is enabled).
func (*PoCompiler) ToFileWithOptions ¶
func (c *PoCompiler) ToFileWithOptions(f string, opts ...PoOption) error
ToFileWithOptions writes compiled output to a file with temporary options. The options are only applied for this operation and then reverted.
func (PoCompiler) ToString ¶
func (c PoCompiler) ToString() string
ToString returns the compiled output as a string.
func (*PoCompiler) ToStringWithOptions ¶
func (c *PoCompiler) ToStringWithOptions(opts ...PoOption) string
ToStringWithOptions returns the compiled output as a string with temporary options. The options are only applied for this operation and then reverted.
func (PoCompiler) ToWriter ¶
func (c PoCompiler) ToWriter(w io.Writer) error
ToWriter writes compiled PO content to an io.Writer. Handles header writing, duplicate cleaning, and optional syntax highlighting.
func (*PoCompiler) ToWriterWithOptions ¶
func (c *PoCompiler) ToWriterWithOptions(w io.Writer, opts ...PoOption) error
ToWriterWithOptions writes compiled output to an io.Writer with temporary options. The options are only applied for this operation and then reverted.
type PoConfig ¶
type PoConfig struct {
// The logger can be nil, otherwise this logger will be used to print all errors by default.
Logger *log.Logger
ForcePo bool // If true, forces the creation of a `.po` file, even if not strictly needed.
OmitHeader bool // If true, omits the header section in the generated `.po` file.
PackageName string // Name of the package associated with the translation.
CopyrightHolder string // Name of the entity holding copyright over the translation.
ForeignUser bool // If true, marks the translation as public domain.
Title string // Title to be included in the `.po` file header.
NoLocation bool // If true, suppresses location comments in the `.po` file.
AddLocation PoLocationMode // Specifies how location comments should be included ("never", "file", "full").
MsgstrPrefix string // Prefix added to all translation strings.
MsgstrSuffix string // Suffix added to all translation strings.
IgnoreErrors bool // If true, allows compilation to proceed despite non-critical errors.
Verbose bool
CommentFuzzy bool
HeaderComments bool
HeaderFields bool
WordWrap bool
HeaderConfig *po.HeaderConfig
UseCustomObsoletePrefix bool
CustomObsoletePrefixRune rune
Highlight *HighlightConfig
// contains filtered or unexported fields
}
PoConfig holds the settings for the compiler, affecting how translations are processed.
func DefaultPoConfig ¶
func NewPoConfigFromOptions ¶
func (*PoConfig) ApplyOptions ¶
Overwrite the configuration with the options provided, saving the previous state so that it can be restored later with PoConfig.RestoreLastCfg if desired.
func (*PoConfig) RestoreLastCfg ¶
func (c *PoConfig) RestoreLastCfg()
Restores the configuration state prior to the last PoConfig.ApplyOptions if it exists, otherwise it does nothing.
type PoLocationMode ¶
type PoLocationMode string
const ( PoLocationModeFull PoLocationMode = "full" PoLocationModeNever PoLocationMode = "never" PoLocationModeFile PoLocationMode = "file" )
type PoOption ¶
type PoOption func(*PoConfig)
func PoWithAddLocation ¶
func PoWithAddLocation(loc PoLocationMode) PoOption
func PoWithCommentFuzzy ¶
func PoWithConfig ¶
func PoWithCopyrightHolder ¶
func PoWithForcePo ¶
func PoWithForeignUser ¶
func PoWithHeaderComments ¶
func PoWithHeaderFields ¶
func PoWithHighlight ¶ added in v2.4.0
func PoWithHighlight(h *HighlightConfig) PoOption
