-
Notifications
You must be signed in to change notification settings - Fork 424
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
DOC two-phase parsing for initializing locale (was: Option capable of setting help locale) #1326
Comments
It looks like ResourceBundles are resolved and loaded before the preprocessors are run, which explains the behaviour you are seeing. One idea is to use a two-phase approach to parsing in the application:
Something like this: class InitLocale {
@Option(names = { "-l", "--locale" }, descriptionKey = "locale for messages", paramLabel = "<name>")
void setLocale(String locale) {
Locale.setDefault(new Locale(locale));
}
@Unmatched
List<String> remainder; // ignore any other parameters and options in the first parsing phase
}
@Command(name = "myapp", resourceBundle = "mybundle")
class ActualApplication implements Runnable {
@Option(names = { "-l", "--locale" }, descriptionKey = "locale for messages", paramLabel = "<name>")
private String ignored;
@Option(names = { "-x", "--other-option" }, descriptionKey = "some other option")
private String value;
public void run() { // business logic here
}
public static void main(String[] args) {
// first phase: configure locale
new CommandLine(new InitLocale()).parseArgs(args);
// second phase: parse all args (ignoring --locale) and run the app
new CommandLine(new ActualApplication()).execute(args);
}
} |
I imagine that your use case is a common requirement for localized CLI apps. Generally speaking, this two-phase parsing approach can be an elegant and simple solution to a certain category of problems. Would you be interested in providing a pull request to add this example (or something like it, after you verified that it actually works 😄 ) to the documentation (and the |
Yes! I can also contribute our reasoning on this. We have two: both having in common inter-locale human relationships. The user is not using a system configured for his native locale. It may be that they are (say) operating some remote thin container, or some server where nobody cared about this during installation. Or it may be that company policy requires that all systems are configured to the same locale, but that is different from the "native" locale for this particular person. I personally think this is of low impact. The user has a system confortably configured for himself, but, not for some other person with whom she is interacting. For example, the user may need to share an error message with (say) Stack Overflow. But sharing such messages localized will effectively impede communication. I personally think this is of high impact -- this is the hard reason we adopt the requirement. |
About the first story above, an anecdote: the company used to mandate every system must be localized to |
Great, thank you in advance! I modified the issue title accordingly. The file to modify is After running the I am thinking it may be good to add a new subsection "Controlling the Locale" at the end of the "Internationalization" section. Let me know what you think. |
The technique you suggested seems to work well. |
Excellent! Glad to hear that. |
Sorry for not contributing this in time. Thank you for your hard work! |
All credit goes to @deining! 😄 |
… controlling the locale" This reverts commit bf39d4c.
…)" This reverts commit bf520ae.
This reverts commit a974f94.
… controlling the locale" This reverts commit bf39d4c.
…)" This reverts commit bf520ae.
This reverts commit a974f94.
First, thank you for this excellent project!
We have made an effort to implement our requirements with picocli and with little effort have mostly succeeded!
Unfortunately, we are stuck in a single requirement on locales.
We have confirmed that picocli is localizable and we have successfully used ResourceBundles.
But we require that option
locale
must change the default locale, affecting also the tool'shelp
.In our own tool suite, we do that by setting
Locale.setDefault
as early as possible.With picocli, we have attempted the following:
but for reasons we don't understand the code above does not cause the locale for
help
to change even iflocale
is the very first option in the command line.Is the above expected to cause the locale used by picocli to change?
The text was updated successfully, but these errors were encountered: