Merge pull request #16852 from aiyijing/fix/subcmd-unable-translate

fix subcommand cannot be translated
pull/16878/head
Steven Powell 2023-07-13 14:40:57 -07:00 committed by GitHub
commit 085433cd1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 2 deletions

View File

@ -140,7 +140,7 @@ func Execute() {
}
}
for _, c := range RootCmd.Commands() {
applyToAllCommands(RootCmd, func(c *cobra.Command) {
c.Short = translate.T(c.Short)
c.Long = translate.T(c.Long)
c.Flags().VisitAll(func(f *pflag.Flag) {
@ -148,7 +148,8 @@ func Execute() {
})
c.SetUsageTemplate(usageTemplate())
}
})
RootCmd.Short = translate.T(RootCmd.Short)
RootCmd.Long = translate.T(RootCmd.Long)
RootCmd.Flags().VisitAll(func(f *pflag.Flag) {
@ -341,3 +342,13 @@ func addToPath(dir string) {
func validateUsername(name string) bool {
return len(name) <= 60
}
// applyToAllCommands applies the provided func to all commands including sub commands
func applyToAllCommands(cmd *cobra.Command, f func(subCmd *cobra.Command)) {
for _, c := range cmd.Commands() {
f(c)
if c.HasSubCommands() {
applyToAllCommands(c, f)
}
}
}