Skip to content

Commit

Permalink
Merge pull request #14 from Tom5521/13-add-mo-file-support
Browse files Browse the repository at this point in the history
Quick Update: Refactoring for Better Code Consistency
  • Loading branch information
Tom5521 authored Mar 1, 2025
2 parents 5430431 + 207a5b7 commit e1ba19c
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 182 deletions.
4 changes: 2 additions & 2 deletions cli/msgomerge/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

var (
headerCfg po.HeaderConfig
compilerCfg compiler.Config
compilerCfg compiler.PoConfig
)

func initConfig() {
compilerCfg = compiler.Config{}
compilerCfg = compiler.PoConfig{}
}
6 changes: 3 additions & 3 deletions cli/xgotext/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
PoParserCfg poparse.Config
GoParserCfg goparse.Config
CompilerCfg compiler.Config
CompilerCfg compiler.PoConfig
HeadersCfg po.HeaderConfig
)

Expand All @@ -28,7 +28,7 @@ func initConfig() {
Logger: logger,
Verbose: verbose,
}
CompilerCfg = compiler.Config{
CompilerCfg = compiler.PoConfig{
Logger: logger,
ForcePo: forcePo,
OmitHeader: omitHeader,
Expand All @@ -37,7 +37,7 @@ func initConfig() {
ForeignUser: foreignUser,
Title: title,
NoLocation: noLocation,
AddLocation: compiler.LocationMode(addLocation),
AddLocation: compiler.PoLocationMode(addLocation),
MsgstrPrefix: msgstrPrefix,
MsgstrSuffix: msgstrSuffix,
Verbose: verbose,
Expand Down
2 changes: 1 addition & 1 deletion cli/xgotext/cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func join(newParse *goparse.Parser, rawfile *os.File) error {

po.MergeFiles(false, base, parsed)

compiler := compiler.NewPo(base, compiler.WithConfig(CompilerCfg))
compiler := compiler.NewPo(base, compiler.PoWithConfig(CompilerCfg))

// Truncate file.
rawfile, err = os.Create(rawfile.Name())
Expand Down
2 changes: 1 addition & 1 deletion cli/xgotext/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Similarly for optional arguments.`,
return join(parser, out)
}

compiler := compiler.NewPo(parsedFile, compiler.WithConfig(CompilerCfg))
compiler := compiler.NewPo(parsedFile, compiler.PoWithConfig(CompilerCfg))

err = compiler.ToWriter(out)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/po/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package compiler
import "io"

type Compiler interface {
ToWriter(io.Writer, ...Option) error
ToFile(string, ...Option) error
ToString(...Option) string
ToBytes(...Option) []byte
ToWriter(io.Writer, ...PoOption) error
ToFile(string, ...PoOption) error
ToString(...PoOption) string
ToBytes(...PoOption) []byte
}
150 changes: 0 additions & 150 deletions pkg/po/compiler/config.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/po/compiler/mo_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func makeRevVersions() []byte {
}

type MoCompiler struct {
File *po.File
Config Config
File *po.File
}

func (mc *MoCompiler) applyOptions(opts ...Option) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/po/compiler/po_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ var _ Compiler = (*PoCompiler)(nil)
// into different output formats, such as strings, byte slices, or files.
type PoCompiler struct {
File *po.File // The source file containing translation entries.
Config Config // Configuration settings for compilation.
Config PoConfig // Configuration settings for compilation.
}

// applyOptions applies a set of options to modify the compiler's configuration.
func (c *PoCompiler) applyOptions(opts ...Option) {
func (c *PoCompiler) applyOptions(opts ...PoOption) {
for _, opt := range opts {
opt(&c.Config)
}
}

// NewPo creates a new Compiler instance with the given translation file and options.
// The provided options override the default configuration.
func NewPo(file *po.File, options ...Option) PoCompiler {
func NewPo(file *po.File, options ...PoOption) PoCompiler {
return PoCompiler{
File: file,
Config: DefaultConfig(options...),
Config: DefaultPoConfig(options...),
}
}

// ToWriter writes the compiled translations to an `io.Writer` in the PO file format.
// The provided options override the instance's configuration.
func (c PoCompiler) ToWriter(w io.Writer, options ...Option) error {
func (c PoCompiler) ToWriter(w io.Writer, options ...PoOption) error {
// Apply the provided options, which take precedence over the instance's configuration.
c.applyOptions(options...)
var err error
Expand Down Expand Up @@ -78,7 +78,7 @@ func (c PoCompiler) ToWriter(w io.Writer, options ...Option) error {
// ToFile writes the compiled translations to a specified file.
// If `ForcePo` is enabled, the file is created or truncated before writing.
// The provided options override the instance's configuration.
func (c PoCompiler) ToFile(f string, options ...Option) error {
func (c PoCompiler) ToFile(f string, options ...PoOption) error {
flags := os.O_RDWR
if c.Config.ForcePo {
flags |= os.O_CREATE
Expand Down Expand Up @@ -123,7 +123,7 @@ func (c PoCompiler) ToFile(f string, options ...Option) error {

// ToString compiles the translations and returns the result as a string.
// The provided options override the instance's configuration.
func (c PoCompiler) ToString(options ...Option) string {
func (c PoCompiler) ToString(options ...PoOption) string {
var b strings.Builder

// Write the compiled content to the string builder.
Expand All @@ -134,7 +134,7 @@ func (c PoCompiler) ToString(options ...Option) string {

// ToBytes compiles the translations and returns the result as a byte slice.
// The provided options override the instance's configuration.
func (c PoCompiler) ToBytes(options ...Option) []byte {
func (c PoCompiler) ToBytes(options ...PoOption) []byte {
var b bytes.Buffer

// Write the compiled content to the byte buffer.
Expand Down
Loading

0 comments on commit e1ba19c

Please sign in to comment.