//go:build ignore // Gen writes catalog_gen.go, the accessors of MyGo's own strings, with the // generator of apps' catalogs, messages/en.json, the messages they embed, the languages MyGo is // translated to, and the package of each translation in // ../../locales/, and their imports in ../../cmd/mygo for the Windows // installer's own strings: `go generate ./internal/l10n`. package main import ( "bytes" "fmt" "go/format" "log" "os" "path/filepath" "strings" "testing/fstest" "github.com/egoist/mygo/i18n" "github.com/egoist/mygo/internal/i18ngen" ) func main() { log.SetFlags(0) opts := i18ngen.Options{Dir: "internal/l10n", Source: "en", Namespace: "mygo", Own: true} c, diags, err := i18ngen.Load(os.DirFS("."), opts) if err != nil { log.Fatal(err) } check(diags) src, err := i18ngen.Go(c, "l10n") if err != nil { log.Fatal(err) } write(i18ngen.GoFile, src) write(filepath.Join(i18ngen.MessagesDir, "en.json"), i18ngen.MessagesJSON(c.Files["en"])) // The translations, each with the source to check them against. all := fstest.MapFS{} en, err := os.ReadFile("en.json") if err != nil { log.Fatal(err) } all["en.json"] = &fstest.MapFile{Data: en} paths, err := filepath.Glob("../../locales/*/*.json") if err != nil { log.Fatal(err) } var tags []string for _, p := range paths { tag := strings.TrimSuffix(filepath.Base(p), ".json") if filepath.Base(filepath.Dir(p)) != tag { log.Fatalf("%s: a translation is named after its directory", p) } data, err := os.ReadFile(p) if err != nil { log.Fatal(err) } all[tag+".json"] = &fstest.MapFile{Data: data} tags = append(tags, tag) write(filepath.Join(filepath.Dir(p), "locale_gen.go"), localePackage(tag)) } opts.Dir = "locales" _, diags, err = i18ngen.Load(all, opts) if err != nil { log.Fatal(err) } check(diags) var b bytes.Buffer b.WriteString("// Code generated by go generate. DO NOT EDIT.\n\npackage l10n\n\n") b.WriteString("// Languages are those MyGo's own strings are translated to besides\n") b.WriteString("// English, each in a package an app imports, as\n") b.WriteString("// github.com/egoist/mygo/locales/zh-Hans.\n") fmt.Fprintf(&b, "var Languages = %#v\n", tags) write("languages_gen.go", b.Bytes()) b.Reset() b.WriteString("// Code generated by go generate ./internal/l10n. DO NOT EDIT.\n\npackage main\n\n") b.WriteString("// Link every translation of MyGo's strings for the Windows installer.\nimport (\n") for _, tag := range tags { fmt.Fprintf(&b, "\t_ %q\n", "github.com/egoist/mygo/locales/"+tag) } b.WriteString(")\n") imports, err := format.Source(b.Bytes()) if err != nil { log.Fatal(err) } write("../../cmd/mygo/locales_gen.go", imports) } // localePackage returns the source of the package of the translation to // tag. func localePackage(tag string) []byte { name := strings.ToLower(strings.ReplaceAll(tag, "-", "")) src := fmt.Sprintf(`// Code generated by go generate ./internal/l10n. DO NOT EDIT. // Package %[1]s translates MyGo's own strings, those of its controls, // dialogs and menus, to %[2]s (%[3]s), for apps that ship that // language. The code mygo generate writes for the catalogs of an app // imports it for the app's languages; an app without catalogs imports it // itself: // // import _ "github.com/egoist/mygo/locales/%[3]s" package %[1]s import ( "embed" "github.com/egoist/mygo/internal/l10n" ) //go:embed %[3]s.json var files embed.FS func init() { l10n.Add(files) } `, name, i18n.Autonym(tag), tag) out, err := format.Source([]byte(src)) if err != nil { log.Fatal(err) } return out } func check(diags i18ngen.Diagnostics) { for _, d := range diags { log.Print(d) } if diags.Errors() > 0 { os.Exit(1) } } func write(name string, data []byte) { if old, _ := os.ReadFile(name); !bytes.Equal(old, data) { if err := os.WriteFile(name, data, 0o644); err != nil { log.Fatal(err) } } }