Skip to content

ResourceBundle

h908714124 edited this page Aug 27, 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(List.of(args))
      .orElseThrow(failure -> {
        StandardErrorHandler.builder()
           .withMessages(toMap(bundle))
           .build().printErrorMessage(notSuccess);
        System.exit(1);
        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