Skip to content

Commit 5ee57e3

Browse files
committed
update: new option --no-file-location and --no-location
To generate a po file for Chinese without location: git-po-helper update --no-location zh_CN To generate a po file for Chinese without both file and location: git-po-helper update --no-file-location zh_CN Signed-off-by: Jiang Xin <[email protected]>
1 parent d46815e commit 5ee57e3

File tree

4 files changed

+201
-39
lines changed

4 files changed

+201
-39
lines changed

cmd/update.go

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,69 @@ import (
44
"github.com/git-l10n/git-po-helper/repository"
55
"github.com/git-l10n/git-po-helper/util"
66
"github.com/spf13/cobra"
7+
"github.com/spf13/viper"
78
)
89

9-
var updateCmd = &cobra.Command{
10-
Use: "update <XX.po>...",
11-
Short: "Update XX.po file",
12-
SilenceErrors: true,
13-
RunE: func(cmd *cobra.Command, args []string) error {
14-
var err error
10+
type updateCommand struct {
11+
cmd *cobra.Command
12+
}
1513

16-
// Execute in root of worktree.
17-
repository.ChdirProjectRoot()
14+
func (v *updateCommand) Command() *cobra.Command {
15+
if v.cmd != nil {
16+
return v.cmd
17+
}
1818

19-
if len(args) == 0 {
20-
return newUserError("no argument for update command")
21-
}
22-
for _, locale := range args {
23-
if !util.CmdUpdate(locale) {
24-
err = errExecute
19+
v.cmd = &cobra.Command{
20+
Use: "update <XX.po>...",
21+
Short: "Update XX.po file",
22+
SilenceErrors: true,
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
var err error
25+
26+
// Execute in root of worktree.
27+
repository.ChdirProjectRoot()
28+
29+
if len(args) == 0 {
30+
return newUserError("no argument for update command")
31+
}
32+
for _, locale := range args {
33+
if !util.CmdUpdate(locale) {
34+
err = errExecute
35+
}
2536
}
37+
return err
38+
},
39+
}
40+
v.cmd.Flags().Bool("no-file-location",
41+
false,
42+
"no filename and location in comment for entry")
43+
v.cmd.Flags().Bool("no-location",
44+
false,
45+
"no location in comment for entry")
46+
_ = viper.BindPFlag("no-file-location", v.cmd.Flags().Lookup("no-file-location"))
47+
_ = viper.BindPFlag("no-location", v.cmd.Flags().Lookup("no-location"))
48+
return v.cmd
49+
}
50+
51+
func (v updateCommand) Execute(args []string) error {
52+
var err error
53+
54+
// Execute in root of worktree.
55+
repository.ChdirProjectRoot()
56+
57+
if len(args) == 0 {
58+
return newUserError("no argument for update command")
59+
}
60+
for _, locale := range args {
61+
if !util.CmdUpdate(locale) {
62+
err = errExecute
2663
}
27-
return err
28-
},
64+
}
65+
return err
2966
}
3067

68+
var updateCmd = updateCommand{}
69+
3170
func init() {
32-
rootCmd.AddCommand(updateCmd)
71+
rootCmd.AddCommand(updateCmd.Command())
3372
}

test/t0020-update.sh

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ test_expect_success "fail to update zh_CN: bad syntax of zh_CN.po" '
6161
6262
cat >expect <<-\EOF &&
6363
po/zh_CN.po:25: end-of-line within string
64-
level=error msg="fail to update \"po/zh_CN.po\": exit status 1"
64+
level=error msg="fail to read output for \"po/zh_CN.po\": exit status 1"
6565
EOF
6666
6767
test_cmp expect actual
@@ -124,4 +124,82 @@ test_expect_success "check update of zh_CN.po" '
124124
test_cmp expect actual
125125
'
126126

127+
test_expect_success "update zh_CN with file and location" '
128+
cat >workdir/po/zh_CN.po <<-\EOF &&
129+
msgid ""
130+
msgstr ""
131+
"Project-Id-Version: Git\n"
132+
"Report-Msgid-Bugs-To: Git Mailing List <[email protected]>\n"
133+
"POT-Creation-Date: 2021-03-04 22:41+0800\n"
134+
"PO-Revision-Date: 2021-03-04 22:41+0800\n"
135+
"Last-Translator: Automatically generated\n"
136+
"Language-Team: none\n"
137+
"Language: zh_CN\n"
138+
"MIME-Version: 1.0\n"
139+
"Content-Type: text/plain; charset=UTF-8\n"
140+
"Content-Transfer-Encoding: 8bit\n"
141+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
142+
EOF
143+
144+
git -C workdir $HELPER --pot-file=po/git.pot \
145+
update zh_CN &&
146+
147+
grep "^#: builtin/clean.c" workdir/po/zh_CN.po >output &&
148+
sort output | head -1 >actual &&
149+
cat >expect <<-EOF &&
150+
#: builtin/clean.c:29
151+
EOF
152+
test_cmp expect actual
153+
'
154+
155+
test_expect_success "update zh_CN --no-location" '
156+
cat >workdir/po/zh_CN.po <<-\EOF &&
157+
msgid ""
158+
msgstr ""
159+
"Project-Id-Version: Git\n"
160+
"Report-Msgid-Bugs-To: Git Mailing List <[email protected]>\n"
161+
"POT-Creation-Date: 2021-03-04 22:41+0800\n"
162+
"PO-Revision-Date: 2021-03-04 22:41+0800\n"
163+
"Last-Translator: Automatically generated\n"
164+
"Language-Team: none\n"
165+
"Language: zh_CN\n"
166+
"MIME-Version: 1.0\n"
167+
"Content-Type: text/plain; charset=UTF-8\n"
168+
"Content-Transfer-Encoding: 8bit\n"
169+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
170+
EOF
171+
172+
git -C workdir $HELPER --pot-file=po/git.pot \
173+
update --no-location zh_CN &&
174+
175+
grep "^#: builtin/clean.c" workdir/po/zh_CN.po >output &&
176+
sort output | head -1 >actual &&
177+
cat >expect <<-EOF &&
178+
#: builtin/clean.c
179+
EOF
180+
test_cmp expect actual
181+
'
182+
183+
test_expect_success "update zh_CN --no-file-location" '
184+
cat >workdir/po/zh_CN.po <<-\EOF &&
185+
msgid ""
186+
msgstr ""
187+
"Project-Id-Version: Git\n"
188+
"Report-Msgid-Bugs-To: Git Mailing List <[email protected]>\n"
189+
"POT-Creation-Date: 2021-03-04 22:41+0800\n"
190+
"PO-Revision-Date: 2021-03-04 22:41+0800\n"
191+
"Last-Translator: Automatically generated\n"
192+
"Language-Team: none\n"
193+
"Language: zh_CN\n"
194+
"MIME-Version: 1.0\n"
195+
"Content-Type: text/plain; charset=UTF-8\n"
196+
"Content-Transfer-Encoding: 8bit\n"
197+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
198+
EOF
199+
200+
git -C workdir $HELPER --pot-file=po/git.pot \
201+
update --no-file-location zh_CN &&
202+
test_must_fail grep "^#: builtin/clean.c" workdir/po/zh_CN.po
203+
'
204+
127205
test_done

test/t0060-github-action.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ test_expect_success "bad syntax of zh_CN.po" '
7373
'
7474

7575
cat >expect <<-EOF
76-
INFO updating po file for "Chinese - China": msgmerge --add-location --backup=off -U po/zh_CN.po po/git.pot
76+
INFO run msgmerge for "Chinese - China": msgmerge --add-location -o - po/zh_CN.po po/git.pot
7777
------------------------------------------------------------------------------
7878
INFO [po/zh_CN.po] 2 translated messages, 5102 untranslated messages.
7979
------------------------------------------------------------------------------

util/update.go

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,31 @@ func UpdatePotFile() (string, bool) {
107107
// CmdUpdate implements update sub command.
108108
func CmdUpdate(fileName string) bool {
109109
var (
110-
cmd *exec.Cmd
111-
locale string
112-
localeFullName string
113-
err error
114-
poFile string
115-
cmdArgs []string
116-
poTemplate string
117-
ok bool
110+
cmd *exec.Cmd
111+
msgCatCmd *exec.Cmd
112+
locale string
113+
localeFullName string
114+
err error
115+
poFile string
116+
tmpFile string
117+
cmdArgs []string
118+
poTemplate string
119+
ok bool
120+
optNoLocation = viper.GetBool("no-location")
121+
optNoFileLocation = viper.GetBool("no-file-location")
122+
output []byte
118123
)
119124

120125
locale = strings.TrimSuffix(filepath.Base(fileName), ".po")
121126
if localeFullName, err = GetPrettyLocaleName(locale); err != nil {
122-
log.Errorf("fail to update: %s", err)
127+
log.Errorf("fail to get locale name: %s", err)
123128
return false
124129
}
125130
poFile = filepath.Join(PoDir, locale+".po")
131+
tmpFile = poFile + ".tmp"
132+
defer func() {
133+
os.Remove(tmpFile)
134+
}()
126135

127136
// Update pot file.
128137
if poTemplate, ok = UpdatePotFile(); !ok {
@@ -143,25 +152,61 @@ func CmdUpdate(fileName string) bool {
143152
return false
144153
}
145154

146-
cmdArgs = []string{"msgmerge",
147-
"--add-location",
148-
"--backup=off",
149-
"-U",
155+
cmdArgs = []string{"msgmerge"}
156+
if optNoFileLocation {
157+
cmdArgs = append(cmdArgs, "--no-location")
158+
} else {
159+
cmdArgs = append(cmdArgs, "--add-location")
160+
}
161+
cmdArgs = append(cmdArgs,
162+
"-o", "-", // Save output to stdout
150163
poFile,
151164
poTemplate,
152-
}
153-
log.Infof(`updating po file for "%s": %s`, localeFullName, strings.Join(cmdArgs, " "))
165+
)
166+
log.Infof(`run msgmerge for "%s": %s`, localeFullName, strings.Join(cmdArgs, " "))
154167
cmd = exec.Command(cmdArgs[0], cmdArgs[1:]...)
155168
cmd.Dir = repository.WorkDir()
156169
cmd.Stderr = os.Stderr
157-
if err := cmd.Start(); err != nil {
158-
log.Errorf(`fail to update "%s": %s`, poFile, err)
159-
return false
170+
171+
if optNoLocation {
172+
msgCatCmdArgs := []string{"msgcat", "--add-location=file", "-"}
173+
log.Infof(`run msgcat for "%s": %s`, localeFullName, strings.Join(msgCatCmdArgs, " "))
174+
msgCatCmd = exec.Command(msgCatCmdArgs[0], msgCatCmdArgs[1:]...)
175+
msgCatCmd.Stdin, err = cmd.StdoutPipe()
176+
if err != nil {
177+
log.Errorf("fail to create pipe: %v\n", err)
178+
return false
179+
}
180+
if err := cmd.Start(); err != nil {
181+
log.Errorf(`fail to start msgmerge: %s`, err)
182+
return false
183+
}
184+
output, err = msgCatCmd.Output()
185+
if err != nil {
186+
log.Errorf(`fail to read output for "%s": %s`, poFile, err)
187+
return false
188+
}
189+
} else {
190+
output, err = cmd.Output()
191+
if err != nil {
192+
log.Errorf(`fail to read output for "%s": %s`, poFile, err)
193+
return false
194+
}
160195
}
161-
if err := cmd.Wait(); err != nil {
162-
log.Errorf(`fail to update "%s": %s`, poFile, err)
196+
197+
if err := os.WriteFile(tmpFile, output, 0644); err != nil {
198+
log.Errorf(`fail to write to "%s": %s`, tmpFile, err)
163199
return false
164200
}
201+
os.Rename(tmpFile, poFile)
202+
203+
if optNoLocation {
204+
if err := cmd.Wait(); err != nil {
205+
log.Errorf(`wait failed: %s`, err)
206+
return false
207+
}
208+
}
209+
165210
viper.Set("check--report-file-locations", "none")
166211
return CheckPoFile(locale, poFile)
167212
}

0 commit comments

Comments
 (0)