You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

57 lines
1.3 KiB

  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "io"
  7. "os"
  8. "golang.org/x/text/message/pipeline"
  9. )
  10. const printerType = "golang.org/x/text/message.Printer"
  11. // TODO:
  12. // - merge information into existing files
  13. // - handle different file formats (PO, XLIFF)
  14. // - handle features (gender, plural)
  15. // - message rewriting
  16. func init() {
  17. overwrite = cmdRewrite.Flag.Bool("w", false, "write files in place")
  18. }
  19. var (
  20. overwrite *bool
  21. )
  22. var cmdRewrite = &Command{
  23. Run: runRewrite,
  24. UsageLine: "rewrite <package>",
  25. Short: "rewrites fmt functions to use a message Printer",
  26. Long: `
  27. rewrite is typically done once for a project. It rewrites all usages of
  28. fmt to use x/text's message package whenever a message.Printer is in scope.
  29. It rewrites Print and Println calls with constant strings to the equivalent
  30. using Printf to allow translators to reorder arguments.
  31. `,
  32. }
  33. func runRewrite(cmd *Command, _ *pipeline.Config, args []string) error {
  34. var w io.Writer
  35. if !*overwrite {
  36. w = os.Stdout
  37. }
  38. pkg := "."
  39. switch len(args) {
  40. case 0:
  41. case 1:
  42. pkg = args[0]
  43. default:
  44. return errorf("can only specify at most one package")
  45. }
  46. return pipeline.Rewrite(w, pkg)
  47. }