Skip to content

ResourceBundle

h908714124 edited this page Aug 27, 2021 · 11 revisions

In order to do internationalization, you need a Map<String, String> containing message keys and the corresponding translations.

If you have a ResourceBundle, you have to convert it to a map first:

private static Map<String, String> toMap(ResourceBundle bundle) {
  return Collections.list(bundle.getKeys()).stream()
    .collect(Collectors.toMap(Function.identity(), bundle::getString));
}

Call the withMessages(Map<String, String>) method on the generated parser as follows:

public static void main(String[] args) {
  Map<String, String> messages = Map.of("key.apple", "manzana");
  DeleteCommand command = new DeleteCommandParser().parse(List.of(args))
      .orElseThrow(failure -> {
        StandardErrorHandler.builder()
           .withMessages(messages)
           .build().printErrorMessage(notSuccess);
        System.exit(1);
        return new RuntimeException();
     });
  // ...
}

In the command class, use the descriptionKey attribute to associate the message key with an option or parameter:

@Option(names = "--gateway-host",
        description = "apple",
        descriptionKey = "key.apple")
abstract String gatewayHost();
Clone this wiki locally