Documentation
¶
Overview ¶
Package pofile provides primitives for gettext text catalogs (.po/.pot).
The package exposes two data layers:
- Catalog for semantic operations (upsert/find/delete, headers, merge)
- Document for lossless workflows (comments, ordering, source positions)
Use ParseFile or ParseDir for semantic flows. Use ParseDocumentFile and ParseDocumentDir when you need source-preserving round-trip behavior. Use ParseCatalogWithDiagnostics for tolerant parse with structured findings.
Scope is intentionally narrow:
- text .po/.pot only
- no binary .mo support
- no runtime gettext lookup engine
Index ¶
- Variables
- func Format(catalog *Catalog) ([]byte, error)
- func FormatDocument(document *Document, options *WriteOptions) ([]byte, error)
- func FormatWithOptions(catalog *Catalog, options *WriteOptions) ([]byte, error)
- func ParseCatalogWithDiagnostics(data []byte, options ParseOptions) (*Catalog, []Diagnostic, error)
- func ParseDir(dir string) (map[string]*Catalog, error)
- func ParseDocumentDir(dir string) (map[string]*Document, error)
- func ParseDocumentReaderWithOptions(reader io.Reader, options ParseOptions) (*Document, []Diagnostic, error)
- func ParseDocumentWithOptions(data []byte, options ParseOptions) (*Document, []Diagnostic, error)
- func ValidateDocument(document *Document) error
- func WriteDocumentFile(path string, document *Document, options *WriteOptions) error
- func WriteFile(path string, catalog *Catalog) error
- func WriteFileWithOptions(path string, catalog *Catalog, options *WriteOptions) error
- type Catalog
- func (c *Catalog) Clone() *Catalog
- func (c *Catalog) ContentHash() string
- func (c *Catalog) DeleteMessage(context, id string) bool
- func (c *Catalog) DeleteMessageInDomain(domain, context, id string) bool
- func (c *Catalog) FindMessage(context, id string) *Message
- func (c *Catalog) FindMessageInDomain(domain, context, id string) *Message
- func (c *Catalog) Header(key string) string
- func (c *Catalog) IsTranslated(context, id string) bool
- func (c *Catalog) IsTranslatedInDomain(domain, context, id string) bool
- func (c *Catalog) MarshalText() ([]byte, error)
- func (c *Catalog) SetHeader(key, value string)
- func (c *Catalog) Translation(context, id string) string
- func (c *Catalog) TranslationInDomain(domain, context, id string) string
- func (c *Catalog) TranslationN(context, id string, index int) string
- func (c *Catalog) TranslationNInDomain(domain, context, id string, index int) string
- func (c *Catalog) UnmarshalText(data []byte) error
- func (c *Catalog) UpsertMessage(context, id, translation string) *Message
- func (c *Catalog) UpsertMessageInDomain(domain, context, id, translation string) *Message
- func (c *Catalog) Validate() error
- type Comment
- type CommentKind
- type Diagnostic
- type Document
- type Entry
- type EntryKey
- type Header
- type Index
- type LintMode
- type LintOptions
- type Message
- type ParseOptions
- type Position
- type Severity
- type Span
- type WriteMode
- type WriteOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilCatalog indicates that a catalog argument is nil. ErrNilCatalog = errors.New("catalog is nil") // ErrNilDocument indicates that a document argument is nil. ErrNilDocument = errors.New("document is nil") // ErrTemplateRequired indicates that template catalog is missing. ErrTemplateRequired = errors.New("template catalog is required") // ErrNilMessage indicates that message list contains a nil item. ErrNilMessage = errors.New("message is nil") // ErrMessageIDRequired indicates message with empty msgid. ErrMessageIDRequired = errors.New("message id is required") // ErrDuplicateMessage indicates duplicate context+id pair. ErrDuplicateMessage = errors.New("duplicate message") // ErrDuplicateEntryKey indicates duplicate domain+context+id in document index. ErrDuplicateEntryKey = errors.New("duplicate entry key") )
Functions ¶
func FormatDocument ¶
func FormatDocument(document *Document, options *WriteOptions) ([]byte, error)
FormatDocument encodes document into PO/POT text.
func FormatWithOptions ¶
func FormatWithOptions(catalog *Catalog, options *WriteOptions) ([]byte, error)
FormatWithOptions encodes semantic catalog using document write options.
func ParseCatalogWithDiagnostics ¶
func ParseCatalogWithDiagnostics( data []byte, options ParseOptions, ) (*Catalog, []Diagnostic, error)
ParseCatalogWithDiagnostics decodes PO/POT bytes and returns diagnostics.
func ParseDocumentDir ¶
ParseDocumentDir loads all *.po files from directory indexed by file base name.
func ParseDocumentReaderWithOptions ¶
func ParseDocumentReaderWithOptions( reader io.Reader, options ParseOptions, ) (*Document, []Diagnostic, error)
ParseDocumentReaderWithOptions parses reader and returns diagnostics.
func ParseDocumentWithOptions ¶
func ParseDocumentWithOptions( data []byte, options ParseOptions, ) (*Document, []Diagnostic, error)
ParseDocumentWithOptions parses PO/POT bytes and returns diagnostics.
func ValidateDocument ¶
ValidateDocument runs strict lint and returns one aggregated error.
func WriteDocumentFile ¶
func WriteDocumentFile(path string, document *Document, options *WriteOptions) error
WriteDocumentFile formats document and writes file to disk.
func WriteFileWithOptions ¶
func WriteFileWithOptions(path string, catalog *Catalog, options *WriteOptions) error
WriteFileWithOptions encodes semantic catalog and writes with options.
Types ¶
type Catalog ¶
type Catalog struct {
// Headers stores header values from the initial msgid/msgstr block.
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
// Language is copied from "Language" header when present.
Language string `json:"language,omitempty" yaml:"language,omitempty"`
// Messages stores translation units.
Messages []*Message `json:"messages,omitempty" yaml:"messages,omitempty"`
}
Catalog is an in-memory PO/POT semantic model.
func MergeTemplate ¶
MergeTemplate applies template messages and keeps existing translations.
func ParseReader ¶
ParseReader decodes PO/POT stream into semantic catalog.
func (*Catalog) ContentHash ¶
ContentHash returns deterministic hash for effective catalog content.
func (*Catalog) DeleteMessage ¶
DeleteMessage removes message by context+id in default domain.
func (*Catalog) DeleteMessageInDomain ¶
DeleteMessageInDomain removes message by domain+context+id.
func (*Catalog) FindMessage ¶
FindMessage returns message by context+id in default domain.
func (*Catalog) FindMessageInDomain ¶
FindMessageInDomain returns message by domain+context+id, or nil when missing.
func (*Catalog) IsTranslated ¶
IsTranslated reports whether singular translation is non-empty in default domain.
func (*Catalog) IsTranslatedInDomain ¶
IsTranslatedInDomain reports whether singular translation is non-empty.
func (*Catalog) MarshalText ¶
MarshalText encodes catalog into PO/POT bytes.
func (*Catalog) Translation ¶
Translation returns singular translation by context+id in default domain.
func (*Catalog) TranslationInDomain ¶
TranslationInDomain returns singular translation by domain+context+id.
func (*Catalog) TranslationN ¶
TranslationN returns plural translation by index in default domain.
func (*Catalog) TranslationNInDomain ¶
TranslationNInDomain returns plural translation by index.
func (*Catalog) UnmarshalText ¶
UnmarshalText decodes semantic catalog from PO/POT text bytes.
func (*Catalog) UpsertMessage ¶
UpsertMessage upserts one singular message in default domain.
func (*Catalog) UpsertMessageInDomain ¶
UpsertMessageInDomain upserts one singular message by domain+context+id.
type Comment ¶
type Comment struct {
Kind CommentKind `json:"kind" yaml:"kind"`
Text string `json:"text" yaml:"text"`
Raw string `json:"raw" yaml:"raw"`
Position Position `json:"position" yaml:"position"`
}
Comment stores typed PO comment line.
type CommentKind ¶
type CommentKind string
CommentKind classifies PO comment prefixes.
const ( // CommentTranslator is a regular translator comment "#". CommentTranslator CommentKind = "translator" // CommentExtracted is an extracted/source comment "#.". CommentExtracted CommentKind = "extracted" // CommentReference is a source reference comment "#:". CommentReference CommentKind = "reference" // CommentFlags is a flags comment "#,". CommentFlags CommentKind = "flags" // CommentPrevious is previous-value comment "#|". CommentPrevious CommentKind = "previous" // CommentOther covers unknown comment styles. CommentOther CommentKind = "other" )
type Diagnostic ¶
type Diagnostic struct {
Severity Severity `json:"severity" yaml:"severity"`
Code string `json:"code" yaml:"code"`
Message string `json:"message" yaml:"message"`
Position Position `json:"position" yaml:"position"`
Span Span `json:"span" yaml:"span"`
}
Diagnostic describes one parser/linter issue.
func LintDocument ¶
func LintDocument(document *Document) []Diagnostic
LintDocument runs basic semantic checks and returns diagnostics.
func LintDocumentWithOptions ¶
func LintDocumentWithOptions( document *Document, options *LintOptions, ) []Diagnostic
LintDocumentWithOptions runs lint with selected checks.
type Document ¶
type Document struct {
HeaderComments []Comment `json:"header_comments,omitempty" yaml:"header_comments,omitempty"`
Headers []Header `json:"headers,omitempty" yaml:"headers,omitempty"`
Entries []*Entry `json:"entries,omitempty" yaml:"entries,omitempty"`
}
Document keeps parsed PO/POT in source order.
func DocumentFromCatalog ¶
DocumentFromCatalog converts semantic catalog to document.
func ParseDocument ¶
ParseDocument parses PO/POT bytes into lossless document.
func ParseDocumentFile ¶
ParseDocumentFile opens and parses one PO/POT file into document.
func ParseDocumentReader ¶
ParseDocumentReader parses PO/POT from reader into lossless document.
func (*Document) HeaderValue ¶
HeaderValue returns header value by key, or empty string.
type Entry ¶
type Entry struct {
// Translations stores msgstr values by plural index (0 for singular).
Translations map[int]string `json:"translations,omitempty" yaml:"translations,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
Context string `json:"context,omitempty" yaml:"context,omitempty"`
ID string `json:"id,omitempty" yaml:"id,omitempty"`
IDPlural string `json:"id_plural,omitempty" yaml:"id_plural,omitempty"`
// Previous values come from "#| msgctxt/msgid/msgid_plural" comments.
PreviousContext string `json:"previous_context,omitempty" yaml:"previous_context,omitempty"`
PreviousID string `json:"previous_id,omitempty" yaml:"previous_id,omitempty"`
PreviousIDPlural string `json:"previous_id_plural,omitempty" yaml:"previous_id_plural,omitempty"`
Comments []Comment `json:"comments,omitempty" yaml:"comments,omitempty"`
Flags []string `json:"flags,omitempty" yaml:"flags,omitempty"`
References []string `json:"references,omitempty" yaml:"references,omitempty"`
Position Position `json:"position" yaml:"position"`
Obsolete bool `json:"obsolete,omitempty" yaml:"obsolete,omitempty"`
}
Entry stores one PO translation unit.
type EntryKey ¶
type EntryKey struct {
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
Context string `json:"context,omitempty" yaml:"context,omitempty"`
ID string `json:"id" yaml:"id"`
}
EntryKey identifies one PO entry by domain, context, and id.
type Header ¶
type Header struct {
Key string `json:"key" yaml:"key"`
Value string `json:"value" yaml:"value"`
Position Position `json:"position" yaml:"position"`
}
Header stores one parsed header key/value with source position.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index provides fast key-based lookup for a Document.
func (*Index) EntryDefaultDomain ¶
EntryDefaultDomain returns entry in default domain.
func (*Index) EntryInDomain ¶
EntryInDomain returns entry by domain, context, and id.
type LintOptions ¶
type LintOptions struct {
// CheckLanguageHeader enables Language header check for translated files.
CheckLanguageHeader *bool `json:"check_language_header,omitempty" yaml:"check_language_header,omitempty"`
// CheckPluralShape enables plural consistency checks.
CheckPluralShape *bool `json:"check_plural_shape,omitempty" yaml:"check_plural_shape,omitempty"`
// CheckPlaceholders enables printf-like placeholder checks.
CheckPlaceholders *bool `json:"check_placeholders,omitempty" yaml:"check_placeholders,omitempty"`
// Mode controls warning/error severity policy.
Mode LintMode `json:"mode,omitempty" yaml:"mode,omitempty"`
}
LintOptions controls lint checks and strictness.
type Message ¶
type Message struct {
// Translations stores msgstr values by plural index.
// Index 0 is the singular translation.
Translations map[int]string `json:"translations,omitempty" yaml:"translations,omitempty"`
// Domain stores optional gettext domain.
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
// Context corresponds to msgctxt.
Context string `json:"context,omitempty" yaml:"context,omitempty"`
// ID corresponds to msgid.
ID string `json:"id,omitempty" yaml:"id,omitempty"`
// IDPlural corresponds to msgid_plural.
IDPlural string `json:"id_plural,omitempty" yaml:"id_plural,omitempty"`
// Previous values come from "#|" comments.
PreviousContext string `json:"previous_context,omitempty" yaml:"previous_context,omitempty"`
// PreviousID is previous msgid from "#| msgid".
PreviousID string `json:"previous_id,omitempty" yaml:"previous_id,omitempty"`
// PreviousIDPlural is previous msgid_plural from "#| msgid_plural".
PreviousIDPlural string `json:"previous_id_plural,omitempty" yaml:"previous_id_plural,omitempty"`
// Comments stores entry comments as raw lines.
Comments []string `json:"comments,omitempty" yaml:"comments,omitempty"`
// Flags stores parsed values from "#," comments.
Flags []string `json:"flags,omitempty" yaml:"flags,omitempty"`
// References stores parsed values from "#:" comments.
References []string `json:"references,omitempty" yaml:"references,omitempty"`
// Obsolete marks an obsolete "#~" entry.
Obsolete bool `json:"obsolete,omitempty" yaml:"obsolete,omitempty"`
}
Message is one translation entry.
func (*Message) IsPlural ¶
IsPlural reports whether message has plural source or plural translations.
func (*Message) SetTranslationAt ¶
SetTranslationAt sets translation value by plural index.
func (*Message) TranslationAt ¶
TranslationAt returns translation value by plural index.
type ParseOptions ¶
type ParseOptions struct {
// AllowInvalid keeps best-effort parse result with diagnostics.
AllowInvalid bool `json:"allow_invalid,omitempty" yaml:"allow_invalid,omitempty"`
}
ParseOptions controls parser behavior.
type Position ¶
type Position struct {
Line int `json:"line" yaml:"line"`
Column int `json:"column" yaml:"column"`
Offset int `json:"offset" yaml:"offset"`
}
Position represents 1-based line/column and byte offset.
type Span ¶
type Span struct {
StartOffset int `json:"start_offset" yaml:"start_offset"`
EndOffset int `json:"end_offset" yaml:"end_offset"`
}
Span represents a byte range in source text.
type WriteOptions ¶
type WriteOptions struct {
// Mode selects preserve or canonical output behavior.
Mode WriteMode `json:"mode,omitempty" yaml:"mode,omitempty"`
// HeaderOrder defines preferred key order before generic keys in canonical mode.
HeaderOrder []string `json:"header_order,omitempty" yaml:"header_order,omitempty"`
// SortEntries sorts entries by domain/context/id before write.
SortEntries bool `json:"sort_entries,omitempty" yaml:"sort_entries,omitempty"`
// SortHeaders sorts headers by key before write.
SortHeaders bool `json:"sort_headers,omitempty" yaml:"sort_headers,omitempty"`
}
WriteOptions controls document formatting.