stringtable

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 13 Imported by: 0

README

stringtable

stringtable is a Go module for DayZ stringtable.csv files.

It provides:

  • CSV parse/read/write (ParseCSV*, FormatCSV*, WriteCSVFile*)
  • table model with language columns (Table, Row)
  • language helpers (DefaultLanguages, ParseLanguages)
  • language selection helper (SelectLanguages)
  • pofile integration for .po/.pot workflows
  • merge/update helpers between CSV and PO catalogs
  • PO directory helpers (ReadCatalogSet, WriteCatalogSet)
  • optional build header management (UpdateBuildHeaders)

Install

go get github.com/woozymasta/stringtable

Quick Example

package main

import (
    "log"

    "github.com/woozymasta/stringtable"
)

func main() {
    table, err := stringtable.ParseCSVFile("stringtable.csv")
    if err != nil {
        log.Fatal(err)
    }

    catalog, err := stringtable.CatalogFromTableLanguage(table, "russian")
    if err != nil {
        log.Fatal(err)
    }

    if err := stringtable.MergeCatalogLanguage(
        table,
        "russian",
        catalog,
        stringtable.MergeOptions{},
    ); err != nil {
        log.Fatal(err)
    }

    if err := stringtable.WriteCSVFile("stringtable_out.csv", table); err != nil {
        log.Fatal(err)
    }
}

By default, merge uses the original text as fallback for empty, missing, and notranslate entries. Use MergeOptions.Disable* flags to turn off specific fallback rules.

By default, CSV formatting writes all default DayZ language columns. Use WriteOptions.UseTableLanguagesOnly to keep only current table columns.

UpdateBuildHeaders can write common headers, content hash, and update PO-Revision-Date or POT-Creation-Date only when content actually changed.

Documentation

Overview

Package stringtable provides DayZ stringtable.csv primitives and pofile integration.

Use ParseCSVFile and WriteCSVFile for CSV workflows, ParseLanguages and DefaultLanguages for language handling, and the pofile integration helpers to convert between CSV rows and gettext PO catalogs. Use UpdateBuildHeaders to optionally manage standard PO/POT build headers and content hash.

Index

Constants

View Source
const (
	// DefaultContentHashHeader is the default PO header for content hash.
	DefaultContentHashHeader = "X-Content-Hash"

	// DefaultSourceHashHeader is the default PO header for source hash.
	DefaultSourceHashHeader = "X-CSV-Hash"
)

Variables

View Source
var (
	// ErrNilTable indicates nil table argument.
	ErrNilTable = errors.New("table is nil")

	// ErrNilCatalog indicates nil PO catalog argument.
	ErrNilCatalog = errors.New("catalog is nil")

	// ErrInvalidHeader indicates invalid CSV header shape.
	ErrInvalidHeader = errors.New("invalid stringtable csv header")

	// ErrDuplicateKey indicates duplicate key in CSV rows.
	ErrDuplicateKey = errors.New("duplicate key")
)
View Source
var DefaultLanguages = []string{
	"english",
	"czech",
	"german",
	"russian",
	"polish",
	"hungarian",
	"italian",
	"spanish",
	"french",
	"chinese",
	"japanese",
	"portuguese",
	"chinesesimp",
}

DefaultLanguages is the default DayZ language order.

Functions

func CatalogFromTableLanguage

func CatalogFromTableLanguage(table *Table, language string) (*pofile.Catalog, error)

CatalogFromTableLanguage builds language-specific PO catalog from CSV table.

func CatalogSetFromTable

func CatalogSetFromTable(
	table *Table,
	languages []string,
) (map[string]*pofile.Catalog, error)

CatalogSetFromTable builds PO catalogs for requested languages.

func ContainsLanguage

func ContainsLanguage(list []string, language string) bool

ContainsLanguage reports whether language exists in list.

func ExtractLanguageName

func ExtractLanguageName(path string) string

ExtractLanguageName extracts language from "/path/lang.po" path.

func FormatCSV

func FormatCSV(table *Table) ([]byte, error)

FormatCSV serializes table into stringtable CSV bytes.

func FormatCSVWithOptions

func FormatCSVWithOptions(table *Table, options *WriteOptions) ([]byte, error)

FormatCSVWithOptions serializes table into stringtable CSV bytes.

func LanguageCode added in v0.1.1

func LanguageCode(language string) (string, bool)

LanguageCode returns language code for DayZ language name.

func LanguageNameFromCode added in v0.1.1

func LanguageNameFromCode(code string) (string, bool)

LanguageNameFromCode returns DayZ language name for language code.

func MergeCatalogLanguage

func MergeCatalogLanguage(
	table *Table,
	language string,
	catalog *pofile.Catalog,
	options MergeOptions,
) error

MergeCatalogLanguage applies PO translations into one CSV table language.

func MergeCatalogSet

func MergeCatalogSet(
	table *Table,
	catalogs map[string]*pofile.Catalog,
	options MergeOptions,
) error

MergeCatalogSet applies multiple PO catalogs into table language columns.

func ParseCatalogDir

func ParseCatalogDir(dir string) (map[string]*pofile.Catalog, error)

ParseCatalogDir loads all *.po files from directory by language filename.

func ParseLanguages

func ParseLanguages(value string) []string

ParseLanguages parses comma-separated language list.

func ReadCatalogSet

func ReadCatalogSet(dir string) (map[string]*pofile.Catalog, error)

ReadCatalogSet loads all *.po files from directory by language filename.

func SelectLanguages

func SelectLanguages(available, include, exclude []string) []string

SelectLanguages returns deterministic language list using include/exclude.

func TemplateCatalogFromTable

func TemplateCatalogFromTable(table *Table) (*pofile.Catalog, error)

TemplateCatalogFromTable builds POT-like catalog from CSV table.

func UpdateBuildHeaders

func UpdateBuildHeaders(catalog *pofile.Catalog, options HeaderOptions) bool

UpdateBuildHeaders updates optional build headers and returns whether effective catalog content changed (by content hash).

func UpdateCatalogFromTable

func UpdateCatalogFromTable(
	table *Table,
	language string,
	existing *pofile.Catalog,
) (*pofile.Catalog, error)

UpdateCatalogFromTable updates catalog entries using table rows.

func WriteCSVFile

func WriteCSVFile(path string, table *Table) error

WriteCSVFile writes table as stringtable CSV file.

func WriteCSVFileWithOptions

func WriteCSVFileWithOptions(path string, table *Table, options *WriteOptions) error

WriteCSVFileWithOptions writes table as stringtable CSV file.

func WriteCatalogSet

func WriteCatalogSet(dir string, catalogs map[string]*pofile.Catalog) error

WriteCatalogSet writes PO catalogs into "<language>.po" files in directory.

Types

type HeaderOptions

type HeaderOptions struct {
	// ProjectVersion sets Project-Id-Version when non-empty.
	ProjectVersion string `json:"project_version,omitempty" yaml:"project_version,omitempty"`

	// Generator sets X-Generator when non-empty.
	Generator string `json:"generator,omitempty" yaml:"generator,omitempty"`

	// SourceHash writes source hash to source hash header when non-empty.
	SourceHash string `json:"source_hash,omitempty" yaml:"source_hash,omitempty"`

	// ContentHashHeader overrides content hash header name.
	ContentHashHeader string `json:"content_hash_header,omitempty" yaml:"content_hash_header,omitempty"`

	// SourceHashHeader overrides source hash header name.
	SourceHashHeader string `json:"source_hash_header,omitempty" yaml:"source_hash_header,omitempty"`

	// WriteStandardHeaders writes MIME and language headers when missing.
	WriteStandardHeaders bool `json:"write_standard_headers,omitempty" yaml:"write_standard_headers,omitempty"`

	// WriteHash writes content hash into hash header.
	WriteHash bool `json:"write_hash,omitempty" yaml:"write_hash,omitempty"`

	// WriteDateOnChange updates creation/revision date only when content changed.
	WriteDateOnChange bool `json:"write_date_on_change,omitempty" yaml:"write_date_on_change,omitempty"`

	// Template switches date header to POT-Creation-Date (otherwise PO-Revision-Date).
	Template bool `json:"template,omitempty" yaml:"template,omitempty"`
}

HeaderOptions controls optional build/header updates for PO/POT catalogs.

type MergeOptions

type MergeOptions struct {
	// DisableOriginalOnEmpty disables original fallback when msgstr is empty.
	DisableOriginalOnEmpty bool `json:"no_fallback_empty,omitempty" yaml:"no_fallback_empty,omitempty"`

	// DisableOriginalOnNoTranslate disables original fallback for "notranslate".
	DisableOriginalOnNoTranslate bool `json:"no_fallback_notranslate,omitempty" yaml:"no_fallback_notranslate,omitempty"`

	// DisableOriginalOnMissing disables original fallback when entry is missing.
	DisableOriginalOnMissing bool `json:"no_fallback_missing,omitempty" yaml:"no_fallback_missing,omitempty"`
}

MergeOptions controls PO-to-CSV translation merge behavior.

type Row

type Row struct {
	// Translations stores values per language column.
	Translations map[string]string `json:"translations,omitempty" yaml:"translations,omitempty"`

	// Key is value from "Language" column.
	Key string `json:"key" yaml:"key"`

	// Original is value from "original" column.
	Original string `json:"original" yaml:"original"`
}

Row is one stringtable.csv entry.

type Table

type Table struct {
	// Languages defines translation column order after key+original columns.
	Languages []string `json:"languages,omitempty" yaml:"languages,omitempty"`

	// Rows stores key/original and per-language values.
	Rows []Row `json:"rows,omitempty" yaml:"rows,omitempty"`
}

Table is a DayZ stringtable CSV model.

func NewTable

func NewTable(languages []string) *Table

NewTable creates an empty table with language order.

func ParseCSV

func ParseCSV(data []byte) (*Table, error)

ParseCSV parses stringtable CSV bytes.

func ParseCSVFile

func ParseCSVFile(path string) (*Table, error)

ParseCSVFile parses stringtable CSV file from disk.

func ParseCSVReader

func ParseCSVReader(reader io.Reader) (*Table, error)

ParseCSVReader parses stringtable CSV from reader.

func (*Table) Clone

func (t *Table) Clone() *Table

Clone deep-copies table.

func (*Table) EnsureLanguage

func (t *Table) EnsureLanguage(language string)

EnsureLanguage appends missing language to column order.

func (*Table) Validate

func (t *Table) Validate() error

Validate checks structural consistency.

type WriteOptions

type WriteOptions struct {
	// UseTableLanguagesOnly writes only table header languages as-is.
	// When false, writer emits all default DayZ languages plus any extra
	// languages found in table data.
	UseTableLanguagesOnly bool `json:"use_table_languages_only,omitempty" yaml:"use_table_languages_only,omitempty"`
}

WriteOptions controls CSV serialization behavior.

Jump to

Keyboard shortcuts

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