Skip to content

ResourceBundle

h908714124 edited this page Jun 21, 2021 · 11 revisions

Call the withMessages(Map<String, String>) method on the generated parser, with a map created from the bundle:

public static void main(String[] args) {
  ResourceBundle bundle = ResourceBundle.getBundle("myResource");
  DeleteCommand command = new DeleteCommandParser().parse(args)
      .orElseThrow(notSuccess -> {
        int code = StandardErrorHandler.builder()
         .withMessages(toMap(bundle))
         .build().handle(notSuccess);
        System.exit(code);
        return new RuntimeException();
     });
  // ...
}

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

Clone this wiki locally