Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intl: format completed reports for pasting and add polish and arabic translations #2742

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
134 changes: 134 additions & 0 deletions client/cmd/translationsreport/main.go
Expand Up @@ -2,14 +2,18 @@ package main

import (
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"

"decred.org/dcrdex/client/core"
"decred.org/dcrdex/client/intl"
"decred.org/dcrdex/client/webserver"
"decred.org/dcrdex/client/webserver/locales"
"decred.org/dcrdex/dex"
)

func main() {
Expand All @@ -21,6 +25,14 @@ func main() {
}

func mainErr() error {
var formatFile string
flag.StringVar(&formatFile, "format", "", "filename. format the completed translation report for Go and print to stdout then quit")
flag.Parse()

if formatFile != "" {
return formatReport(formatFile)
}

core.RegisterTranslations()
webserver.RegisterTranslations()
locales.RegisterTranslations()
Expand Down Expand Up @@ -73,3 +85,125 @@ type WorksheetEntry struct {
English string `json:"english"`
Translation string `json:"translation"`
}

func formatReport(reportFile string) error {
b, err := os.ReadFile(dex.CleanAndExpandPath(reportFile))
if err != nil {
return fmt.Errorf("error reading file at %q: %w", reportFile, err)
}
var entries []*WorksheetEntry
if err := json.Unmarshal(b, &entries); err != nil {
return fmt.Errorf("JSON error for report file at %q: %w", reportFile, err)
}
sortedEntries := make(map[string][]*WorksheetEntry, 3)
entryErrs := make([]string, 0)
for _, e := range entries {
if e.Context == "" {
entryErrs = append(entryErrs, fmt.Sprintf("no context found for report with ID %q", e.ID))
continue
}
sortedEntries[e.Context] = append(sortedEntries[e.Context], e)
}

entryErrs = append(entryErrs, printCoreLines(sortedEntries["notifications"])...)
entryErrs = append(entryErrs, printJSLines(sortedEntries["js"])...)
entryErrs = append(entryErrs, printHTMLLines(sortedEntries["html"])...)

if len(entryErrs) == 0 {
return nil
}

fmt.Printf("\nErrors encountered\n")
for i := range entryErrs {
fmt.Println(entryErrs[i])
}

return errors.New("completed with errors")
}

func printCoreLines(es []*WorksheetEntry) (entryErrs []string) {
fmt.Printf("\nCore notifications. Put in correct locale map in client/core/locale_ntfn.go\n------------\n")
noteEntries := make(map[string][2]*WorksheetEntry)
for _, e := range es {
var i int
var entryID string
switch {
case strings.HasSuffix(e.ID, " subject"):
entryID = e.ID[:len(e.ID)-len(" subject")]
case strings.HasSuffix(e.ID, " template"):
i = 1
entryID = e.ID[:len(e.ID)-len(" template")]
default:
entryErrs = append(entryErrs, fmt.Sprintf("corrupted ID %q for web notification", e.ID))
continue
}
ne := noteEntries[entryID]
ne[i] = e
noteEntries[entryID] = ne
}

var unpaired []*WorksheetEntry
for entryID, st := range noteEntries {
subjectEntry, templateEntry := st[0], st[1]
if subjectEntry == nil {
if templateEntry == nil {
entryErrs = append(entryErrs, fmt.Sprintf("Missing subject or template for translation with ID %s", entryID))
} else {
unpaired = append(unpaired, templateEntry)
}
continue
} else if templateEntry == nil {
unpaired = append(unpaired, subjectEntry)
continue
}
fmt.Printf(webNoteTemplate, entryID, formatReportWithType(subjectEntry), formatReportWithType(templateEntry))
}
for _, e := range unpaired {
fmt.Println("Topic"+e.ID, ": ", formatReportWithType(e)+",")
}

return
}

func printJSLines(es []*WorksheetEntry) (entryErrs []string) {
fmt.Printf("\nJS Notifications. Add these to the appropriate map in client/webserver/jsintl.go, replacing the IDs with the appropriate variables\n------------\n")
for _, e := range es {
fmt.Printf(" %s: %s,\n", e.ID, formatReportEntry(e))
}
return
}

func printHTMLLines(es []*WorksheetEntry) (entryErrs []string) {
fmt.Printf("\nHTML Notifications. Add these to the language file in client/webserver/locales\n------------\n")
for _, e := range es {
fmt.Printf(` "%s": %s,`+"\n", e.ID, formatReportEntry(e))
}
return
}

func escapeTemplate(s string) string {
b, _ := json.Marshal(s)
return string(b)
}

func formatReportEntry(e *WorksheetEntry) string {
var v, n string
if e.Version > 0 {
v = fmt.Sprintf("Version: %d, ", e.Version)
}
t := escapeTemplate(e.Translation)
if e.Notes != "" {
n = fmt.Sprintf(", Notes: %s", escapeTemplate(e.Notes))
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually need these notes in the translations I guess, just in the English. Will probably remove.

return fmt.Sprintf("{%sT: %s%s}", v, t, n)
}

func formatReportWithType(e *WorksheetEntry) string {
return fmt.Sprintf("intl.Translation%s", formatReportEntry(e))
}

var webNoteTemplate = ` Topic%s: {
subject: %s,
template: %s,
},
`