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

flag: make the "default" word localizable #42124

Closed
kpym opened this issue Oct 21, 2020 · 6 comments
Closed

flag: make the "default" word localizable #42124

kpym opened this issue Oct 21, 2020 · 6 comments
Labels
FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@kpym
Copy link

kpym commented Oct 21, 2020

In flag module we can write all help messages in any language, but the "default" keyword stays in english.

It will be nice if the corresponding code could be made "translatable".

@ALTree ALTree changed the title Make "default" keyword translatable in flag module. flag: make the "default" word localizable Oct 21, 2020
@ALTree ALTree added FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Oct 21, 2020
@ALTree
Copy link
Member

ALTree commented Oct 21, 2020

Thanks for the request. AFAIK almost nothing in the standard library is localizable, english is the default. So I think it would feel a little "out of place" maybe?

@cagedmantis cagedmantis added this to the Backlog milestone Oct 21, 2020
@kpym
Copy link
Author

kpym commented Oct 21, 2020

@ALTree I understand, but this library is an user interface library. Heaving some log message in english is ok, but producing user interface that has only few non translated words is strange.

Probably this should not be properly "translatable", but only made customizable. As we have flag.Usage maybe we can have flag.DefaultMsg that can be defined to display the default values ?

@ianlancetaylor
Copy link
Contributor

It seems to me that programs that support localization should use the flag.Usage function to control the usage message. They shouldn't call PrintDefaults but should use their own locale-appropriate mechanisms.

@ianlancetaylor
Copy link
Contributor

Alternatively, use an io.Writer that reads the documented format of PrintDefaults and applies its own localization as appropriate.

@kpym
Copy link
Author

kpym commented Oct 22, 2020

@ianlancetaylor Thank you for your advice. I think you are right.

@kpym kpym closed this as completed Oct 22, 2020
@kpym
Copy link
Author

kpym commented Oct 23, 2020

Here is an example of how we can do this:

// Replace "default" with "par défaut" in `flag.PrintDefaults`
func PrintDefaultsFr() {
        // save and restore the output (os.Stderr by default)
	original := flag.CommandLine.Output()
	defer flag.CommandLine.SetOutput(original)
	
	// redirect the output to new buffer
	buf := new(bytes.Buffer)
	flag.CommandLine.SetOutput(buf)
	
	// print the defaults in french
	flag.PrintDefaults()
	original.Write(bytes.ReplaceAll(buf.Bytes(), []byte("default"), []byte("par défaut")))
}

Check it in the playground.

@golang golang locked and limited conversation to collaborators Oct 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants