support translations
This commit is contained in:
46
cmd/any2anexoj-cli/localizer.go
Normal file
46
cmd/any2anexoj-cli/localizer.go
Normal file
@@ -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,
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
46
cmd/any2anexoj-cli/translations/en.json
Normal file
46
cmd/any2anexoj-cli/translations/en.json
Normal file
@@ -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"
|
||||
}
|
||||
}
|
||||
46
cmd/any2anexoj-cli/translations/pt.json
Normal file
46
cmd/any2anexoj-cli/translations/pt.json
Normal file
@@ -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"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user