compile

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2025 License: MIT Imports: 16 Imported by: 0

README

Compiler

structure

Documentation

Overview

The compile package contains structures and functions used to compile Mo/Po files with their respective options

Index

Constants

View Source
const (
	LittleEndian = util.LittleEndian
	BigEndian    = util.BigEndian
	NativeEndian = util.NativeEndian
)

Variables

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 HighlightFromReader(cfg *HighlightConfig, name string, input io.Reader) ([]byte, error)

func MoToBytes

func MoToBytes[T po.EntriesOrFile](f T, opts ...MoOption) []byte

func MoToFile

func MoToFile[T po.EntriesOrFile](f T, path string, opts ...MoOption) error

func MoToWriter

func MoToWriter[T po.EntriesOrFile](f T, w io.Writer, opts ...MoOption) error

func PoToBytes

func PoToBytes[T po.EntriesOrFile](f T, opts ...PoOption) []byte

func PoToFile

func PoToFile[T po.EntriesOrFile](f T, path string, opts ...PoOption) error

func PoToString

func PoToString[T po.EntriesOrFile](f T, opts ...PoOption) string

func PoToWriter

func PoToWriter[T po.EntriesOrFile](f T, w io.Writer, opts ...PoOption) error

Types

type Endianness added in v2.3.0

type Endianness = util.Endianness

type HighlightConfig added in v2.4.0

type HighlightConfig struct {
	ID, Str, Comment color.Color
}

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

func DefaultMoConfig(opts ...MoOption) MoConfig

DefaultMoConfig creates a new MoConfig with default values. Applies any provided options during creation.

func (*MoConfig) ApplyOptions

func (mc *MoConfig) ApplyOptions(opts ...MoOption)

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

func MoWithConfig(n MoConfig) MoOption

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 MoWithForce

func MoWithForce(f bool) MoOption

MoWithForce toggles file overwrite behavior

func MoWithHashTable added in v2.3.0

func MoWithHashTable(h bool) MoOption

MoWithHashTable toggles hash table generation

func MoWithIgnoreErrors

func MoWithIgnoreErrors(i bool) MoOption

MoWithIgnoreErrors toggles error suppression

func MoWithLogger

func MoWithLogger(l *log.Logger) MoOption

MoWithLogger sets the output logger

func MoWithVerbose

func MoWithVerbose(v bool) MoOption

MoWithVerbose toggles detailed logging

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 DefaultPoConfig(opts ...PoOption) PoConfig

func NewPoConfigFromOptions

func NewPoConfigFromOptions(opts ...PoOption) PoConfig

func (*PoConfig) ApplyOptions

func (c *PoConfig) ApplyOptions(opts ...PoOption)

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.

func (PoConfig) Validate

func (c PoConfig) Validate() error

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 PoWithCommentFuzzy(c bool) PoOption

func PoWithConfig

func PoWithConfig(cfg PoConfig) PoOption

func PoWithCopyrightHolder

func PoWithCopyrightHolder(holder string) PoOption

func PoWithCustomObsoletePrefixRune

func PoWithCustomObsoletePrefixRune(r rune) PoOption

func PoWithForcePo

func PoWithForcePo(f bool) PoOption

func PoWithForeignUser

func PoWithForeignUser(f bool) PoOption

func PoWithHeaderComments

func PoWithHeaderComments(hc bool) PoOption

func PoWithHeaderFields

func PoWithHeaderFields(w bool) PoOption

func PoWithHighlight added in v2.4.0

func PoWithHighlight(h *HighlightConfig) PoOption

func PoWithIgnoreErrors

func PoWithIgnoreErrors(i bool) PoOption

func PoWithLogger

func PoWithLogger(logger *log.Logger) PoOption

func PoWithMsgstrPrefix

func PoWithMsgstrPrefix(prefix string) PoOption

func PoWithMsgstrSuffix

func PoWithMsgstrSuffix(suffix string) PoOption

func PoWithNoLocation

func PoWithNoLocation(n bool) PoOption

func PoWithOmitHeader

func PoWithOmitHeader(o bool) PoOption

func PoWithPackageName

func PoWithPackageName(name string) PoOption

func PoWithTitle

func PoWithTitle(t string) PoOption

func PoWithUseCustomObsoletePrefix

func PoWithUseCustomObsoletePrefix(u bool) PoOption

func PoWithVerbose

func PoWithVerbose(v bool) PoOption

func PoWithWordWrap

func PoWithWordWrap(w bool) PoOption

Jump to

Keyboard shortcuts

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