From 08d061ca0eaabc5ee3d6900b688e48ee9f4a47c0 Mon Sep 17 00:00:00 2001 From: Tom5521 Date: Sat, 1 Mar 2025 15:44:05 -0300 Subject: [PATCH 1/2] refactor(compiler): rename Config to PoConfig and update related types - Renamed `Config` to `PoConfig` to better reflect its usage in the PO compiler. - Updated related types and functions to use `PoConfig` and `PoOption` for consistency. - Removed the old `config.go` file and introduced `po_config.go` with the new configuration structure. - Updated all references to `Config` and `Option` in the codebase to use the new `PoConfig` and `PoOption` types. - Adjusted the `LocationMode` to `PoLocationMode` to align with the new naming convention. --- cli/msgomerge/cmd/config.go | 4 +- cli/xgotext/cmd/config.go | 6 +- cli/xgotext/cmd/join.go | 2 +- cli/xgotext/cmd/root.go | 2 +- pkg/po/compiler/compiler.go | 8 +- pkg/po/compiler/config.go | 150 --------------------------------- pkg/po/compiler/mo_compiler.go | 3 +- pkg/po/compiler/po_compiler.go | 16 ++-- pkg/po/compiler/po_config.go | 150 +++++++++++++++++++++++++++++++++ pkg/po/compiler/po_format.go | 8 +- 10 files changed, 174 insertions(+), 175 deletions(-) delete mode 100644 pkg/po/compiler/config.go create mode 100644 pkg/po/compiler/po_config.go diff --git a/cli/msgomerge/cmd/config.go b/cli/msgomerge/cmd/config.go index 756e8b7..297ce39 100644 --- a/cli/msgomerge/cmd/config.go +++ b/cli/msgomerge/cmd/config.go @@ -7,9 +7,9 @@ import ( var ( headerCfg po.HeaderConfig - compilerCfg compiler.Config + compilerCfg compiler.PoConfig ) func initConfig() { - compilerCfg = compiler.Config{} + compilerCfg = compiler.PoConfig{} } diff --git a/cli/xgotext/cmd/config.go b/cli/xgotext/cmd/config.go index 3f0f3f9..1253559 100644 --- a/cli/xgotext/cmd/config.go +++ b/cli/xgotext/cmd/config.go @@ -10,7 +10,7 @@ import ( var ( PoParserCfg poparse.Config GoParserCfg goparse.Config - CompilerCfg compiler.Config + CompilerCfg compiler.PoConfig HeadersCfg po.HeaderConfig ) @@ -28,7 +28,7 @@ func initConfig() { Logger: logger, Verbose: verbose, } - CompilerCfg = compiler.Config{ + CompilerCfg = compiler.PoConfig{ Logger: logger, ForcePo: forcePo, OmitHeader: omitHeader, @@ -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, diff --git a/cli/xgotext/cmd/join.go b/cli/xgotext/cmd/join.go index 34f371d..36437ed 100644 --- a/cli/xgotext/cmd/join.go +++ b/cli/xgotext/cmd/join.go @@ -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()) diff --git a/cli/xgotext/cmd/root.go b/cli/xgotext/cmd/root.go index 02c233c..7857bde 100644 --- a/cli/xgotext/cmd/root.go +++ b/cli/xgotext/cmd/root.go @@ -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 { diff --git a/pkg/po/compiler/compiler.go b/pkg/po/compiler/compiler.go index 74ccd92..2c35a4e 100644 --- a/pkg/po/compiler/compiler.go +++ b/pkg/po/compiler/compiler.go @@ -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 } diff --git a/pkg/po/compiler/config.go b/pkg/po/compiler/config.go deleted file mode 100644 index 5c1ab5c..0000000 --- a/pkg/po/compiler/config.go +++ /dev/null @@ -1,150 +0,0 @@ -package compiler - -import ( - "errors" - "io" - "log" -) - -// Config holds the settings for the compiler, affecting how translations are processed. -type Config struct { - 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 LocationMode // 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 -} - -type LocationMode string - -const ( - LocationModeFull LocationMode = "full" - LocationModeNever LocationMode = "never" - LocationModeFile LocationMode = "file" -) - -func DefaultConfig(opts ...Option) Config { - c := Config{ - Logger: log.New(io.Discard, "", 0), - PackageName: "PACKAGE NAME", - AddLocation: LocationModeFull, - } - - for _, opt := range opts { - opt(&c) - } - - return c -} - -func NewConfigFromOptions(opts ...Option) Config { - var config Config - - for _, opt := range opts { - opt(&config) - } - - return config -} - -type Option func(*Config) - -func WithVerbose(v bool) Option { - return func(c *Config) { - c.Verbose = v - } -} - -func WithLogger(logger *log.Logger) Option { - return func(c *Config) { - c.Logger = logger - } -} - -func WithIgnoreErrors(i bool) Option { - return func(c *Config) { - c.IgnoreErrors = i - } -} - -func WithConfig(cfg Config) Option { - return func(c *Config) { - *c = cfg - } -} - -func WithForcePo(f bool) Option { - return func(c *Config) { - c.ForcePo = f - } -} - -func WithOmitHeader(o bool) Option { - return func(c *Config) { - c.OmitHeader = o - } -} - -func WithPackageName(name string) Option { - return func(c *Config) { - c.PackageName = name - } -} - -func WithCopyrightHolder(holder string) Option { - return func(c *Config) { - c.CopyrightHolder = holder - } -} - -func WithForeignUser(f bool) Option { - return func(c *Config) { - c.ForeignUser = f - } -} - -func WithTitle(t string) Option { - return func(c *Config) { - c.Title = t - } -} - -func WithNoLocation(n bool) Option { - return func(c *Config) { - c.NoLocation = n - } -} - -func WithAddLocation(loc LocationMode) Option { - return func(c *Config) { - c.AddLocation = loc - } -} - -func WithMsgstrPrefix(prefix string) Option { - return func(c *Config) { - c.MsgstrPrefix = prefix - } -} - -func WithMsgstrSuffix(suffix string) Option { - return func(c *Config) { - c.MsgstrSuffix = suffix - } -} - -func (c Config) Validate() error { - if c.NoLocation && c.AddLocation != LocationModeNever { - return errors.New("noLocation and AddLocation are in conflict") - } - - return nil -} diff --git a/pkg/po/compiler/mo_compiler.go b/pkg/po/compiler/mo_compiler.go index 349c161..1828579 100644 --- a/pkg/po/compiler/mo_compiler.go +++ b/pkg/po/compiler/mo_compiler.go @@ -52,8 +52,7 @@ func makeRevVersions() []byte { } type MoCompiler struct { - File *po.File - Config Config + File *po.File } func (mc *MoCompiler) applyOptions(opts ...Option) { diff --git a/pkg/po/compiler/po_compiler.go b/pkg/po/compiler/po_compiler.go index e0d0174..902e0a3 100644 --- a/pkg/po/compiler/po_compiler.go +++ b/pkg/po/compiler/po_compiler.go @@ -16,11 +16,11 @@ 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) } @@ -28,16 +28,16 @@ func (c *PoCompiler) applyOptions(opts ...Option) { // 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 @@ -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 @@ -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. @@ -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. diff --git a/pkg/po/compiler/po_config.go b/pkg/po/compiler/po_config.go new file mode 100644 index 0000000..488fc83 --- /dev/null +++ b/pkg/po/compiler/po_config.go @@ -0,0 +1,150 @@ +package compiler + +import ( + "errors" + "io" + "log" +) + +// PoConfig holds the settings for the compiler, affecting how translations are processed. +type PoConfig struct { + 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 +} + +type PoLocationMode string + +const ( + PoLocationModeFull PoLocationMode = "full" + PoLocationModeNever PoLocationMode = "never" + PoLocationModeFile PoLocationMode = "file" +) + +func DefaultPoConfig(opts ...PoOption) PoConfig { + c := PoConfig{ + Logger: log.New(io.Discard, "", 0), + PackageName: "PACKAGE NAME", + AddLocation: PoLocationModeFull, + } + + for _, opt := range opts { + opt(&c) + } + + return c +} + +func NewPoConfigFromOptions(opts ...PoOption) PoConfig { + var config PoConfig + + for _, opt := range opts { + opt(&config) + } + + return config +} + +type PoOption func(*PoConfig) + +func PoWithVerbose(v bool) PoOption { + return func(c *PoConfig) { + c.Verbose = v + } +} + +func PoWithLogger(logger *log.Logger) PoOption { + return func(c *PoConfig) { + c.Logger = logger + } +} + +func PoWithIgnoreErrors(i bool) PoOption { + return func(c *PoConfig) { + c.IgnoreErrors = i + } +} + +func PoWithConfig(cfg PoConfig) PoOption { + return func(c *PoConfig) { + *c = cfg + } +} + +func PoWithForcePo(f bool) PoOption { + return func(c *PoConfig) { + c.ForcePo = f + } +} + +func PoWithOmitHeader(o bool) PoOption { + return func(c *PoConfig) { + c.OmitHeader = o + } +} + +func PoWithPackageName(name string) PoOption { + return func(c *PoConfig) { + c.PackageName = name + } +} + +func PoWithCopyrightHolder(holder string) PoOption { + return func(c *PoConfig) { + c.CopyrightHolder = holder + } +} + +func PoWithForeignUser(f bool) PoOption { + return func(c *PoConfig) { + c.ForeignUser = f + } +} + +func PoWithTitle(t string) PoOption { + return func(c *PoConfig) { + c.Title = t + } +} + +func PoWithNoLocation(n bool) PoOption { + return func(c *PoConfig) { + c.NoLocation = n + } +} + +func PoWithAddLocation(loc PoLocationMode) PoOption { + return func(c *PoConfig) { + c.AddLocation = loc + } +} + +func PoWithMsgstrPrefix(prefix string) PoOption { + return func(c *PoConfig) { + c.MsgstrPrefix = prefix + } +} + +func PoWithMsgstrSuffix(suffix string) PoOption { + return func(c *PoConfig) { + c.MsgstrSuffix = suffix + } +} + +func (c PoConfig) Validate() error { + if c.NoLocation && c.AddLocation != PoLocationModeNever { + return errors.New("noLocation and AddLocation are in conflict") + } + + return nil +} diff --git a/pkg/po/compiler/po_format.go b/pkg/po/compiler/po_format.go index 1e7d60b..d2b664f 100644 --- a/pkg/po/compiler/po_format.go +++ b/pkg/po/compiler/po_format.go @@ -66,13 +66,13 @@ func (c PoCompiler) formatEntry(t po.Entry) string { fprintfln("#. %s", xcomment) } // Add location comments if not suppressed by the configuration. - if !c.Config.NoLocation && c.Config.AddLocation != LocationModeNever { + if !c.Config.NoLocation && c.Config.AddLocation != PoLocationModeNever { switch c.Config.AddLocation { - case LocationModeFull: + case PoLocationModeFull: for _, location := range t.Locations { fprintfln("#: %s:%d", location.File, location.Line) } - case LocationModeFile: + case PoLocationModeFile: for _, location := range t.Locations { fprintfln("#: %s", location.File) } @@ -114,7 +114,7 @@ func (c PoCompiler) formatEntry(t po.Entry) string { return builder.String() } -func formatPrefixAndSuffix(id string, cfg Config) string { +func formatPrefixAndSuffix(id string, cfg PoConfig) string { text := `""` if cfg.MsgstrPrefix != "" { From 207a5b7a6802313c215bfb4e8e0345f6ec8ba268 Mon Sep 17 00:00:00 2001 From: Tom5521 Date: Sat, 1 Mar 2025 15:44:51 -0300 Subject: [PATCH 2/2] refactor(po): rename header option functions for consistency - Renamed header option functions to follow a consistent naming convention by prefixing them with `HeaderWith`. - Updated all references to these functions in the codebase to use the new naming convention. - This change improves code readability and aligns with the project's naming standards. --- pkg/po/header_options.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/po/header_options.go b/pkg/po/header_options.go index d5f743b..26f82ac 100644 --- a/pkg/po/header_options.go +++ b/pkg/po/header_options.go @@ -2,43 +2,43 @@ package po type HeaderOption func(*HeaderConfig) -func WithHewaderConfig(c HeaderConfig) HeaderOption { +func HeaderWithConfig(c HeaderConfig) HeaderOption { return func(hc *HeaderConfig) { *hc = c } } -func WithNplurals(n uint) HeaderOption { +func HeaderWithNplurals(n uint) HeaderOption { return func(hc *HeaderConfig) { hc.Nplurals = n } } -func WithProjectIDVersion(v string) HeaderOption { +func HeaderWithProjectIDVersion(v string) HeaderOption { return func(hc *HeaderConfig) { hc.ProjectIDVersion = v } } -func WithReportMsgidBugsTo(r string) HeaderOption { +func HeaderWithReportMsgidBugsTo(r string) HeaderOption { return func(hc *HeaderConfig) { hc.ReportMsgidBugsTo = r } } -func WithLanguage(lang string) HeaderOption { +func HeaderWithLanguage(lang string) HeaderOption { return func(hc *HeaderConfig) { hc.Language = lang } } -func WithLanguageTeam(team string) HeaderOption { +func HeaderWithLanguageTeam(team string) HeaderOption { return func(hc *HeaderConfig) { hc.LanguageTeam = team } } -func WithLastTranslator(translator string) HeaderOption { +func HeaderWithLastTranslator(translator string) HeaderOption { return func(hc *HeaderConfig) { hc.LastTranslator = translator }