Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom text for printed strings #1724

Closed
3 tasks done
m-horky opened this issue Apr 20, 2023 · 3 comments
Closed
3 tasks done

Allow custom text for printed strings #1724

m-horky opened this issue Apr 20, 2023 · 3 comments
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this

Comments

@m-horky
Copy link

m-horky commented Apr 20, 2023

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here.
  • Did you perform a search about this feature? Here's the GitHub guide about searching.

What problem does this solve?

I'd like to be able to change any (English) string that is printed to the console. It would be useful for translating applications (is this issue a duplicate of #980?) -- not being able to alter texts during runtime prevents that.

Solution description

Similarly to how it is possible to change texts of VersionFlag or HelpFlag, I'd like to be able to access:

  • command.go: Incorrect Usage: in Run()
  • context.go: no such flag -%s in Set()
  • errors.go: most of the error messages
  • flag.go: default in formatDefault()
  • parse.go: const lag provided but not defined: -
  • and possibly more

Describe alternatives you've considered

An alternative could be to add full i18n support as per #980. But that also comes with its set of challenges -- mainly how to allow external applications to extend or include the translations of this project in theirs (languages not supported by this package, or to change the provided translation for whatever reasons).

@m-horky m-horky added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Apr 20, 2023
@m-horky m-horky changed the title Allow custom text for "(default: value)" Allow custom text for printed strings Apr 20, 2023
@dearchap
Copy link
Contributor

@m-horky Thanks for this. yes i18n support is planned. If you have any ideas on how to do this it would be greatly appreciated

@m-horky
Copy link
Author

m-horky commented May 18, 2023

Most of the library is already translatable, which should work well until some library breaking changes.

Assuming translation function T() (I'm using gotext's gotext.Get() aliased to T) that takes in English string and returns appropriately translated string, you can translate default flags, suggestions and templates:

VersionFlag, HelpFlag, SuggestDidYouMean can be simply overwritten.
cli.VersionFlag = &cli.BoolFlag{
	Name:    "version",
	Aliases: []string{"v"},
	Usage:   T("show version and exit"),
}
cli.SuggestDidYouMeanTemplate = T("Did you mean %q?")
AppHelp can be redefined
// ui/help.go
func GetAppHelp() string {
	templateHeader := "{{.Name}}\n  {{.Usage}}"

	templateUsage := T("USAGE") + "\n  " + "{{.HelpName}} " + T("[global options]") + " " + T("COMMAND [command  options]") + " " + fmt.Sprintf("{{if .ArgsUsage}}{{.ArgsUsage}}{{else}}%s{{end}}", T("[arguments]"))
	templateCommands := T("COMMANDS") + `{{range .Commands}}{{if not .HideHelp}}  {{ "\n  " }}{{join .Names ", "}}{{ "\t"}}{{.Usage}}{{end}}{{end}}`
	templateGlobals := T("GLOBAL OPTIONS") + `{{range .VisibleFlags}}{{ "\n  " }}{{.}}{{end}}`
	templateVersion := fmt.Sprintf("%s\n  {{.Version}}", T("VERSION"))

	return templateHeader + "\n\n" + templateUsage + "\n\n" + templateCommands + "\n\n" + templateGlobals + "\n\n" + templateVersion + "\n"
}
// main.go
cli.AppHelpTemplate = ui.GetAppHelp()

...which is not pretty, but gets its job done.


As noted in the opening issue comment, some strings cannot be translated like that, because they are hardcoded in the functions or are private.

The solution that seems the easiest to me is to move all strings that are printed or returned into public variables that can be overwritten. Just like SuggestDidYouMeanTemplate, each could be a variable anyone can overwrite if they need to.

I'm not sure how to deal with the longer templates -- while it is possible to overwrite whole string, it would be cool it it was possible to split them into individual blocks (header, usage, commands) that could be translated individually, and developers using the library wouldn't need to deal with the conditionals inside of the template. But that's rather a nice to have, just having a way to translate any string would be a huge step forward into i18n-supportable space.

@dearchap
Copy link
Contributor

@m-horky yes it looks like a duplicate of #980 . I will get working on this. Thanks for raising the issue

@dearchap dearchap closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this
Projects
None yet
Development

No branches or pull requests

2 participants