2017-12-16 20:25:18 +01:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-07-07 06:18:14 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2017-12-16 20:25:18 +01:00
|
|
|
"golang.org/x/text/message/pipeline"
|
2018-07-07 06:18:14 +02:00
|
|
|
"golang.org/x/tools/go/loader"
|
2017-12-16 20:25:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
out = cmdGenerate.Flag.String("out", "", "output file to write to")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
out *string
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdGenerate = &Command{
|
|
|
|
Run: runGenerate,
|
|
|
|
UsageLine: "generate <package>",
|
|
|
|
Short: "generates code to insert translated messages",
|
|
|
|
}
|
|
|
|
|
2018-07-07 06:18:14 +02:00
|
|
|
var transRe = regexp.MustCompile(`messages\.(.*)\.json`)
|
|
|
|
|
|
|
|
func runGenerate(cmd *Command, args []string) error {
|
|
|
|
|
|
|
|
prog, err := loadPackages(&loader.Config{}, args)
|
|
|
|
if err != nil {
|
|
|
|
return wrap(err, "could not load package")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgs := prog.InitialPackages()
|
|
|
|
if len(pkgs) != 1 {
|
|
|
|
return fmt.Errorf("more than one package selected: %v", pkgs)
|
|
|
|
}
|
|
|
|
pkg := pkgs[0].Pkg.Name()
|
|
|
|
|
|
|
|
// TODO: add in external input. Right now we assume that all files are
|
|
|
|
// manually created and stored in the textdata directory.
|
|
|
|
|
|
|
|
// Build up index of translations and original messages.
|
|
|
|
extracted := pipeline.Locale{}
|
|
|
|
translations := []*pipeline.Locale{}
|
|
|
|
|
|
|
|
err = filepath.Walk(*dir, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return wrap(err, "loading data")
|
|
|
|
}
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if f.Name() == extractFile {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return wrap(err, "read file failed")
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &extracted); err != nil {
|
|
|
|
return wrap(err, "unmarshal source failed")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if f.Name() == outFile {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(path, gotextSuffix) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return wrap(err, "read file failed")
|
|
|
|
}
|
|
|
|
var locale pipeline.Locale
|
|
|
|
if err := json.Unmarshal(b, &locale); err != nil {
|
|
|
|
return wrap(err, "parsing translation file failed")
|
|
|
|
}
|
|
|
|
translations = append(translations, &locale)
|
|
|
|
return nil
|
|
|
|
})
|
2017-12-16 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-07-07 06:18:14 +02:00
|
|
|
return err
|
2017-12-16 20:25:18 +01:00
|
|
|
}
|
2018-07-07 06:18:14 +02:00
|
|
|
|
|
|
|
w := os.Stdout
|
|
|
|
if *out != "" {
|
|
|
|
w, err = os.Create(*out)
|
|
|
|
if err != nil {
|
|
|
|
return wrap(err, "create file failed")
|
|
|
|
}
|
2017-12-16 20:25:18 +01:00
|
|
|
}
|
2018-07-07 06:18:14 +02:00
|
|
|
|
|
|
|
_, err = pipeline.Generate(w, pkg, &extracted, translations...)
|
|
|
|
return err
|
2017-12-16 20:25:18 +01:00
|
|
|
}
|