lotus/cmd/curio/internal/translations/knowns/main.go
Andrew Jackson (Ajax) 81ba6ab6f0
feat: Curio - Easy Migration (#11617)
* feat: lp mig - first few steps

* lp mig: default tasks

* code comments

* docs

* lp-mig-progress

* shared

* comments and todos

* fix: curio: rename lotus-provider to curio (#11645)

* rename provider to curio

* install gotext

* fix lint errors, mod tidy

* fix typo

* fix API_INFO and add gotext to circleCI

* add back gotext

* add gotext after remerge

* lp: channels doc

* finish easy-migration TODOs

* out generate

* merging and more renames

* avoid make-all

* minor doc stuff

* cu: make gen

* make gen fix

* make gen

* tryfix

* go mod tidy

* minor ez migration fixes

* ez setup - ui cleanups

* better error message

* guided setup colors

* better path to saveconfigtolayer

* loadconfigwithupgrades fix

* readMiner oops

* guided - homedir

* err if miner is running

* prompt error should exit

* process already running, miner_id sectors in migration

* dont prompt for language a second time

* check miner stopped

* unlock repo

* render and sql oops

* curio easyMig - some fixes

* easyMigration runs successfully

* lint

* review fixes

* fix backup path

* fixes1

* fixes2

* fixes 3

---------

Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com>
Co-authored-by: LexLuthr <lexluthr@protocol.ai>
2024-03-15 16:38:13 -05:00

83 lines
1.8 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path"
"github.com/samber/lo"
)
func main() {
for _, arg := range os.Args {
handleKnowns(arg)
}
}
func handleKnowns(pathStart string) {
outpath := path.Join(pathStart, "out.gotext.json")
b, err := os.ReadFile(outpath)
if err != nil {
fmt.Println("cannot open "+outpath+":", err)
return
}
type TMsg struct {
ID string `json:"id"`
Translation string `json:"translation"`
Message string `json:"message"`
Placeholder json.RawMessage `json:"placeholder"`
}
type Dataformat struct {
Language string `json:"language"`
Messages []TMsg `json:"messages"`
}
var outData Dataformat
err = json.NewDecoder(bytes.NewBuffer(b)).Decode(&outData)
if err != nil {
fmt.Println("cannot decode "+outpath+":", err)
return
}
f, err := os.Open(path.Join(pathStart, "messages.gotext.json"))
if err != nil {
fmt.Println("cannot open "+path.Join(pathStart, "messages.gotext.json")+":", err)
return
}
defer func() { _ = f.Close() }()
var msgData Dataformat
err = json.NewDecoder(f).Decode(&msgData)
if err != nil {
fmt.Println("cannot decode "+path.Join(pathStart, "messages.gotext.json")+":", err)
return
}
knowns := map[string]string{}
for _, msg := range msgData.Messages {
knowns[msg.ID] = msg.Translation
}
toTranslate := lo.Filter(outData.Messages, func(msg TMsg, _ int) bool {
_, ok := knowns[msg.ID]
return !ok
})
outData.Messages = toTranslate // drop the "done" messages
var outJSON bytes.Buffer
enc := json.NewEncoder(&outJSON)
enc.SetIndent(" ", " ")
err = enc.Encode(outData)
if err != nil {
fmt.Println("cannot encode "+outpath+":", err)
return
}
err = os.WriteFile(outpath, outJSON.Bytes(), 0644)
if err != nil {
fmt.Println("cannot write "+outpath+":", err)
return
}
fmt.Println("rearranged successfully")
}