diff --git a/cmd/any2anexoj-cli/localizer.go b/cmd/any2anexoj-cli/localizer.go new file mode 100644 index 0000000..18691a2 --- /dev/null +++ b/cmd/any2anexoj-cli/localizer.go @@ -0,0 +1,46 @@ +package main + +import ( + "embed" + "encoding/json" + "fmt" + + "github.com/nicksnyder/go-i18n/v2/i18n" + "golang.org/x/text/language" +) + +//go:embed translations/*.json +var translationsFS embed.FS + +type Localizer struct { + *i18n.Localizer +} + +func NewLocalizer(lang string) (*Localizer, error) { + bundle := i18n.NewBundle(language.English) + bundle.RegisterUnmarshalFunc("json", json.Unmarshal) + + _, err := bundle.LoadMessageFileFS(translationsFS, "translations/en.json") + if err != nil { + return nil, fmt.Errorf("loading english messages: %w", err) + } + + _, err = bundle.LoadMessageFileFS(translationsFS, "translations/pt.json") + if err != nil { + return nil, fmt.Errorf("loading portuguese messages: %w", err) + } + + localizer := i18n.NewLocalizer(bundle, lang) + + return &Localizer{ + Localizer: localizer, + }, nil +} + +func (t Localizer) Translate(key string, count int, values map[string]any) string { + return t.MustLocalize(&i18n.LocalizeConfig{ + MessageID: key, + TemplateData: values, + PluralCount: count, + }) +} diff --git a/cmd/any2anexoj-cli/main.go b/cmd/any2anexoj-cli/main.go index 03e2c6a..d41dcd5 100644 --- a/cmd/any2anexoj-cli/main.go +++ b/cmd/any2anexoj-cli/main.go @@ -13,12 +13,15 @@ import ( "github.com/nmoniz/any2anexoj/internal/trading212" "github.com/spf13/pflag" "golang.org/x/sync/errgroup" + "golang.org/x/text/language" ) // TODO: once we support more brokers or exchanges we should make this parameter required and // remove/change default var platform = pflag.StringP("platform", "p", "trading212", "one of the supported platforms") +var lang = pflag.StringP("language", "l", language.Portuguese.String(), "2 letter language code") + var readerFactories = map[string]func() internal.RecordReader{ "trading212": func() internal.RecordReader { return trading212.NewRecordReader(os.Stdin, internal.NewOpenFIGI(&http.Client{Timeout: 5 * time.Second})) @@ -33,14 +36,19 @@ func main() { os.Exit(1) } - err := run(context.Background(), *platform) + if lang == nil || len(*lang) == 0 { + slog.Error("--language flag is required") + os.Exit(1) + } + + err := run(context.Background(), *platform, *lang) if err != nil { slog.Error("found a fatal issue", slog.Any("err", err)) os.Exit(1) } } -func run(ctx context.Context, platform string) error { +func run(ctx context.Context, platform, lang string) error { ctx, cancel := signal.NotifyContext(ctx, os.Kill, os.Interrupt) defer cancel() @@ -66,7 +74,12 @@ func run(ctx context.Context, platform string) error { return err } - printer := NewPrettyPrinter(os.Stdout) + loc, err := NewLocalizer(lang) + if err != nil { + return fmt.Errorf("create localizer: %w", err) + } + + printer := NewPrettyPrinter(os.Stdout, loc) printer.Render(writer) diff --git a/cmd/any2anexoj-cli/pretty_printer.go b/cmd/any2anexoj-cli/pretty_printer.go index 4ad4f2d..f102573 100644 --- a/cmd/any2anexoj-cli/pretty_printer.go +++ b/cmd/any2anexoj-cli/pretty_printer.go @@ -13,16 +13,21 @@ import ( // PrettyPrinter writes a simple, human readable, table row to the provided io.Writer for each // ReportItem received. type PrettyPrinter struct { - table table.Writer - output io.Writer + table table.Writer + output io.Writer + translator Translator } -func NewPrettyPrinter(w io.Writer) *PrettyPrinter { - t := table.NewWriter() - t.SetOutputMirror(w) - t.SetAutoIndex(true) - t.SetStyle(table.StyleLight) - t.SetColumnConfigs([]table.ColumnConfig{ +type Translator interface { + Translate(key string, count int, values map[string]any) string +} + +func NewPrettyPrinter(w io.Writer, tr Translator) *PrettyPrinter { + tw := table.NewWriter() + tw.SetOutputMirror(w) + tw.SetAutoIndex(true) + tw.SetStyle(table.StyleLight) + tw.SetColumnConfigs([]table.ColumnConfig{ colCountry(1), colOther(2), colOther(3), @@ -39,14 +44,27 @@ func NewPrettyPrinter(w io.Writer) *PrettyPrinter { }) return &PrettyPrinter{ - table: t, - output: w, + table: tw, + output: w, + translator: tr, } } func (pp *PrettyPrinter) Render(aw *internal.AggregatorWriter) { - pp.table.AppendHeader(table.Row{"", "", "Realisation", "Realisation", "Realisation", "Realisation", "Acquisition", "Acquisition", "Acquisition", "Acquisition", "", "", ""}, table.RowConfig{AutoMerge: true}) - pp.table.AppendHeader(table.Row{"Source Country", "Code", "Year", "Month", "Day", "Value", "Year", "Month", "Day", "Value", "Expenses", "Paid Taxes", "Counter Country"}) + realizationTxt := pp.translator.Translate("realization", 1, nil) + acquisitionTxt := pp.translator.Translate("acquisition", 1, nil) + yearTxt := pp.translator.Translate("year", 1, nil) + monthTxt := pp.translator.Translate("month", 1, nil) + dayTxt := pp.translator.Translate("day", 1, nil) + valorTxt := pp.translator.Translate("value", 1, nil) + + pp.table.AppendHeader(table.Row{"", "", realizationTxt, realizationTxt, realizationTxt, realizationTxt, acquisitionTxt, acquisitionTxt, acquisitionTxt, acquisitionTxt, "", "", ""}, table.RowConfig{AutoMerge: true}) + pp.table.AppendHeader(table.Row{ + pp.translator.Translate("source_country", 1, nil), pp.translator.Translate("code", 1, nil), + yearTxt, monthTxt, dayTxt, valorTxt, + yearTxt, monthTxt, dayTxt, valorTxt, + pp.translator.Translate("expenses", 2, nil), pp.translator.Translate("foreign_tax_paid", 1, nil), pp.translator.Translate("counter_country", 1, nil), + }) for ri := range aw.Iter() { pp.table.AppendRow(table.Row{ @@ -68,6 +86,7 @@ func colEuros(n int) table.ColumnConfig { AlignFooter: text.AlignRight, AlignHeader: text.AlignRight, WidthMin: 12, + WidthMax: 15, Transformer: func(val any) string { return fmt.Sprintf("%v €", val) }, @@ -83,6 +102,7 @@ func colOther(n int) table.ColumnConfig { Align: text.AlignLeft, AlignFooter: text.AlignLeft, AlignHeader: text.AlignLeft, + WidthMax: 12, } } diff --git a/cmd/any2anexoj-cli/translations/en.json b/cmd/any2anexoj-cli/translations/en.json new file mode 100644 index 0000000..cf17dbc --- /dev/null +++ b/cmd/any2anexoj-cli/translations/en.json @@ -0,0 +1,46 @@ +{ + "realization": { + "one": "Realization", + "other": "Realizations" + }, + "acquisition": { + "one": "Acquisition", + "other": "Acquisitions" + }, + "source_country": { + "one": "Source country", + "other": "Source countries" + }, + "counter_country": { + "one": "Counter country", + "other": "Counter countries" + }, + "year": { + "one": "Year", + "other": "Years" + }, + "month": { + "one": "Month", + "other": "Months" + }, + "day": { + "one": "Day", + "other": "Days" + }, + "value": { + "one": "Value", + "other": "Values" + }, + "code": { + "one": "Code", + "other": "Codes" + }, + "expenses": { + "one": "Expense and charge", + "other": "Expenses and charges" + }, + "foreign_tax_paid": { + "one": "Tax paid abroad", + "other": "Taxes paid abroad" + } +} diff --git a/cmd/any2anexoj-cli/translations/pt.json b/cmd/any2anexoj-cli/translations/pt.json new file mode 100644 index 0000000..60b6339 --- /dev/null +++ b/cmd/any2anexoj-cli/translations/pt.json @@ -0,0 +1,46 @@ +{ + "realization": { + "one": "Realização", + "other": "Realizações" + }, + "acquisition": { + "one": "Aquisição", + "other": "Aquisições" + }, + "source_country": { + "one": "País da fonte", + "other": "Países da fonte" + }, + "counter_country": { + "one": "País da contraparte", + "other": "Países da contraparte" + }, + "year": { + "one": "Ano", + "other": "Anos" + }, + "month": { + "one": "Mês", + "other": "Meses" + }, + "day": { + "one": "Dia", + "other": "Dias" + }, + "value": { + "one": "Valor", + "other": "Valores" + }, + "code": { + "one": "Código", + "other": "Códigos" + }, + "expenses": { + "one": "Despesa e encargo", + "other": "Despesas e encargos" + }, + "foreign_tax_paid": { + "one": "Imposto pago no estrangeiro", + "other": "Impostos pagos no estrangeiro" + } +} diff --git a/go.mod b/go.mod index 9a68879..6b06e01 100644 --- a/go.mod +++ b/go.mod @@ -14,10 +14,11 @@ require ( require ( github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/nicksnyder/go-i18n/v2 v2.6.0 github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/mod v0.27.0 // indirect golang.org/x/sys v0.35.0 // indirect - golang.org/x/text v0.22.0 // indirect + golang.org/x/text v0.23.0 // indirect golang.org/x/tools v0.36.0 // indirect ) diff --git a/go.sum b/go.sum index 199b11d..9de2131 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/jedib0t/go-pretty/v6 v6.7.2 h1:EYWgQNIH/+JsyHki7ns9OHyBKuHPkzrBo02uYj github.com/jedib0t/go-pretty/v6 v6.7.2/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/nicksnyder/go-i18n/v2 v2.6.0 h1:C/m2NNWNiTB6SK4Ao8df5EWm3JETSTIGNXBpMJTxzxQ= +github.com/nicksnyder/go-i18n/v2 v2.6.0/go.mod h1:88sRqr0C6OPyJn0/KRNaEz1uWorjxIKP7rUUcvycecE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -29,6 +31,8 @@ golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI= golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=