yaml

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package yamlfile implements reading and writing of YAML translation files.

The expected file format is a nested YAML map with string leaf values:

greeting: Hello
nav:
  home: Home
  about: About

Rails i18n style (locale as the top-level key) is also supported:

en:
  greeting: Hello
  nav:
    home: Home

Leaf values with empty strings are treated as untranslated. Non-string leaves (numbers, booleans, arrays) are passed through unchanged. The package preserves the source file structure and key order on round-trip.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SyncKeys

func SyncKeys(src, target *File)

SyncKeys ensures the target file has the same keys as the source file. New keys from source are added with empty values; keys missing in source are removed from the target.

Types

type Entry

type Entry struct {
	// Path is the dot-joined key path (e.g. "nav.home").
	Path string
	// Value is the current translation (empty = untranslated).
	Value string
	// Style is the original yaml scalar style for round-trip fidelity.
	Style yaml.Style
}

Entry represents a single translatable leaf value.

type File

type File struct {
	// contains filtered or unexported fields
}

File represents a parsed YAML translation file.

func NewTranslationFile

func NewTranslationFile(srcFile *File, targetLocale string) *File

NewTranslationFile creates an empty target File with the same structure as srcFile but with all values cleared (ready for translation). The targetLocale is used when the source file uses Rails i18n style.

func Parse

func Parse(data []byte) (*File, error)

Parse parses YAML data into a File.

func ParseFile

func ParseFile(path string) (*File, error)

ParseFile reads and parses a YAML translation file.

func (*File) Get

func (f *File) Get(path string) (string, bool)

Get returns the current value for the given path.

func (*File) Keys

func (f *File) Keys() []string

Keys returns all entry paths in document order.

func (*File) Marshal

func (f *File) Marshal() ([]byte, error)

Marshal serialises the file back to YAML, preserving the original structure and scalar styles.

func (*File) Set

func (f *File) Set(path, value string) bool

Set updates the value for the given path. Returns false if the path is not in the file.

func (*File) SourceValues

func (f *File) SourceValues() map[string]string

SourceValues returns a map of path → value (for use as translation source).

func (*File) Stats

func (f *File) Stats() (int, int, float64)

Stats returns (total, translated, percent).

func (*File) UntranslatedKeys

func (f *File) UntranslatedKeys() []string

UntranslatedKeys returns paths whose value is empty.

func (*File) WriteFile

func (f *File) WriteFile(path string) error

WriteFile serialises the file and writes it to the given path.

Jump to

Keyboard shortcuts

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