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