Skip to content

Commit

Permalink
Localization support for help text (#4814)
Browse files Browse the repository at this point in the history
* Translating help text

* explicity set usage template so we can localize it

* cleaning up extract and translation files

* Add extraction for flags help text

* some comments for extract

* refactor to account for new behavior

* Make usage template translations more palatable.

* remove commented code

* fix go mod

* pin to proper version in go.mod

* translate command grouping headers
  • Loading branch information
sharifelgamal authored Aug 12, 2019
1 parent 345ed1b commit 294f880
Show file tree
Hide file tree
Showing 11 changed files with 567 additions and 102 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var profileListCmd = &cobra.Command{
validProfiles, invalidProfiles, err := config.ListProfiles()

if len(validProfiles) == 0 || err != nil {
exit.UsageT("No minikube profile was found. you could create one using: `minikube start`")
exit.UsageT("No minikube profile was found. You can create one using `minikube start`.")
}
for _, p := range validProfiles {
validData = append(validData, []string{p.Name, p.Config.MachineConfig.VMDriver, p.Config.KubernetesConfig.NodeIP, strconv.Itoa(p.Config.KubernetesConfig.NodePort), p.Config.KubernetesConfig.KubernetesVersion})
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var dashboardCmd = &cobra.Command{
if dashboardURLMode {
out.Ln(url)
} else {
out.ErrT(out.Celebrate, "Opening {{.url}} in your default browser...", out.V{"url": url})
out.T(out.Celebrate, "Opening {{.url}} in your default browser...", out.V{"url": url})
if err = browser.OpenURL(url); err != nil {
exit.WithCodeT(exit.Software, "failed to open browser: {{.error}}", out.V{"error": err})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (
// logsCmd represents the logs command
var logsCmd = &cobra.Command{
Use: "logs",
Short: "Gets the logs of the running instance, used for debugging minikube, not user code",
Short: "Gets the logs of the running instance, used for debugging minikube, not user code.",
Long: `Gets the logs of the running instance, used for debugging minikube, not user code.`,
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.Load()
Expand Down
62 changes: 54 additions & 8 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/notify"
"k8s.io/minikube/pkg/minikube/translate"
)

var dirs = [...]string{
Expand Down Expand Up @@ -101,12 +102,57 @@ var RootCmd = &cobra.Command{
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
for _, c := range RootCmd.Commands() {
c.Short = translate.T(c.Short)
c.Long = translate.T(c.Long)
c.Flags().VisitAll(func(flag *pflag.Flag) {
flag.Usage = translate.T(flag.Usage)
})

c.SetUsageTemplate(usageTemplate())
}
RootCmd.Short = translate.T(RootCmd.Short)
RootCmd.Long = translate.T(RootCmd.Long)
RootCmd.Flags().VisitAll(func(flag *pflag.Flag) {
flag.Usage = translate.T(flag.Usage)
})

if err := RootCmd.Execute(); err != nil {
// Cobra already outputs the error, typically because the user provided an unknown command.
os.Exit(exit.BadUsage)
}
}

// usageTemplate just calls translate.T on the default usage template
// explicitly using the raw string instead of calling c.UsageTemplate()
// so the extractor can find this monstrosity of a string
func usageTemplate() string {
return fmt.Sprintf(`%s:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
%s:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
%s:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
%s:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
%s:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
%s:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
%s:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
%s{{end}}
`, translate.T("Usage"), translate.T("Aliases"), translate.T("Examples"), translate.T("Available Commands"), translate.T("Flags"), translate.T("Global Flags"), translate.T("Additional help topics"), translate.T(`Use "{{.CommandPath}} [command] --help" for more information about a command.`))
}

// Handle config values for flags used in external packages (e.g. glog)
// by setting them directly, using values from viper when not passed in as args
func setFlagsUsingViper() {
Expand All @@ -127,12 +173,13 @@ func setFlagsUsingViper() {
}

func init() {
translate.DetermineLocale()
RootCmd.PersistentFlags().StringP(config.MachineProfile, "p", constants.DefaultMachineName, `The name of the minikube VM being used. This can be set to allow having multiple instances of minikube independently.`)
RootCmd.PersistentFlags().StringP(configCmd.Bootstrapper, "b", constants.DefaultClusterBootstrapper, "The name of the cluster bootstrapper that will set up the kubernetes cluster.")

groups := templates.CommandGroups{
{
Message: "Basic Commands:",
Message: translate.T("Basic Commands:"),
Commands: []*cobra.Command{
startCmd,
statusCmd,
Expand All @@ -142,14 +189,14 @@ func init() {
},
},
{
Message: "Images Commands:",
Message: translate.T("Images Commands:"),
Commands: []*cobra.Command{
dockerEnvCmd,
cacheCmd,
},
},
{
Message: "Configuration and Management Commands:",
Message: translate.T("Configuration and Management Commands:"),
Commands: []*cobra.Command{
configCmd.AddonsCmd,
configCmd.ConfigCmd,
Expand All @@ -158,22 +205,22 @@ func init() {
},
},
{
Message: "Networking and Connectivity Commands:",
Message: translate.T("Networking and Connectivity Commands:"),
Commands: []*cobra.Command{
serviceCmd,
tunnelCmd,
},
},
{
Message: "Advanced Commands:",
Message: translate.T("Advanced Commands:"),
Commands: []*cobra.Command{
mountCmd,
sshCmd,
kubectlCmd,
},
},
{
Message: "Troubleshooting Commands:",
Message: translate.T("Troubleshooting Commands:"),
Commands: []*cobra.Command{
sshKeyCmd,
ipCmd,
Expand All @@ -185,15 +232,14 @@ func init() {
}
groups.Add(RootCmd)

// any not grouped command will show in Other Commands group.
// Ungrouped commands will show up in the "Other Commands" section
RootCmd.AddCommand(completionCmd)
templates.ActsAsRootCommand(RootCmd, []string{"options"}, groups...)

pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
if err := viper.BindPFlags(RootCmd.PersistentFlags()); err != nil {
exit.WithError("Unable to bind flags", err)
}

cobra.OnInitialize(initConfig)

}
Expand Down
6 changes: 3 additions & 3 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func validateUser() {
func validateConfig() {
diskSizeMB := pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize))
if diskSizeMB < pkgutil.CalculateSizeInMB(constants.MinimumDiskSize) {
exit.WithCodeT(exit.Config, "Requested disk size {{.size_in_mb}} is less than minimum of {{.size_in_mb2}}", out.V{"size_in_mb": diskSizeMB, "size_in_mb2": pkgutil.CalculateSizeInMB(constants.MinimumDiskSize)})
exit.WithCodeT(exit.Config, "Requested disk size {{.requested_size}} is less than minimum of {{.minimum_size}}", out.V{"requested_size": diskSizeMB, "minimum_size": pkgutil.CalculateSizeInMB(constants.MinimumDiskSize)})
}

err := autoSetOptions(viper.GetString(vmDriver))
Expand All @@ -497,7 +497,7 @@ func validateConfig() {

memorySizeMB := pkgutil.CalculateSizeInMB(viper.GetString(memory))
if memorySizeMB < pkgutil.CalculateSizeInMB(constants.MinimumMemorySize) {
exit.UsageT("Requested memory allocation {{.size_in_mb}} is less than the minimum allowed of {{.size_in_mb2}}", out.V{"size_in_mb": memorySizeMB, "size_in_mb2": pkgutil.CalculateSizeInMB(constants.MinimumMemorySize)})
exit.UsageT("Requested memory allocation {{.requested_size}} is less than the minimum allowed of {{.minimum_size}}", out.V{"requested_size": memorySizeMB, "minimum_size": pkgutil.CalculateSizeInMB(constants.MinimumMemorySize)})
}
if memorySizeMB < pkgutil.CalculateSizeInMB(constants.DefaultMemorySize) {
out.T(out.Notice, "Requested memory allocation ({{.memory}}MB) is less than the default memory allocation of {{.default_memorysize}}MB. Beware that minikube might not work correctly or crash unexpectedly.",
Expand Down Expand Up @@ -769,7 +769,7 @@ func validateKubernetesVersions(old *cfg.Config) (string, bool) {

nvs, err := semver.Make(strings.TrimPrefix(rawVersion, version.VersionPrefix))
if err != nil {
exit.WithCodeT(exit.Data, `Unable to parse "{{.kubenretes_version}}": {{.error}}`, out.V{"kubenretes_version": rawVersion, "error": err})
exit.WithCodeT(exit.Data, `Unable to parse "{{.kubernetes_version}}": {{.error}}`, out.V{"kubernetes_version": rawVersion, "error": err})
}
nv := version.VersionPrefix + nvs.String()

Expand Down
2 changes: 0 additions & 2 deletions cmd/minikube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/translate"
_ "k8s.io/minikube/pkg/provision"
)

Expand All @@ -51,7 +50,6 @@ func main() {
}
out.SetOutFile(os.Stdout)
out.SetErrFile(os.Stderr)
translate.DetermineLocale()
cmd.Execute()
}

Expand Down
4 changes: 2 additions & 2 deletions hack/jenkins/minikube_cross_build_and_upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ git diff ${ghprbActualCommit} --name-only \
| grep -q deploy/iso/minikube && rebuild=1 || rebuild=0

if [[ "${rebuild}" -eq 1 ]]; then
echo "ISO changes detected ... rebuilding ISO"
make release-iso
echo "ISO changes detected ... rebuilding ISO"
make release-iso
fi

cp -r test/integration/testdata out/
Expand Down
2 changes: 1 addition & 1 deletion hack/jenkins/minikube_set_pending.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs=(
'Hyper-V_Windows'
'VirtualBox_Linux'
'VirtualBox_macOS'
'VirtualBox_Windows'
'VirtualBox_Windows'
# 'KVM-GPU_Linux' - Disabled
'KVM_Linux'
'none_Linux'
Expand Down
Loading

0 comments on commit 294f880

Please sign in to comment.