Add support for i18n using resource bundles

This commit is contained in:
Paul Schaub 2022-08-04 00:49:08 +02:00
parent f349c701fa
commit e37921b4d4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
14 changed files with 145 additions and 26 deletions

View file

@ -11,8 +11,12 @@ import picocli.CommandLine;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Locale;
import java.util.ResourceBundle;
@CommandLine.Command(name = "get", description = "Retrieve an OpenPGP certificate from the key server")
@CommandLine.Command(
name = "get",
resourceBundle = "msg_get")
public class GetCmd implements Runnable {
@CommandLine.Mixin
@ -22,16 +26,22 @@ public class GetCmd implements Runnable {
Exclusive by;
static class Exclusive {
@CommandLine.Option(names = {"-f", "--by-fingerprint"}, description = "Retrieve a key by its fingerprint (NOT prefixed with '0x')")
@CommandLine.Option(names = {"-f", "--by-fingerprint"})
String fingerprint;
@CommandLine.Option(names = {"-i", "--by-keyid"}, description = "Retrieve a key by its decimal key ID or that of one of its subkeys.")
@CommandLine.Option(names = {"-i", "--by-keyid"})
Long keyId;
@CommandLine.Option(names = {"-e", "--by-email"}, description = "Retrieve a key by email address.")
@CommandLine.Option(names = {"-e", "--by-email"})
String email;
}
private final ResourceBundle msg;
public GetCmd() {
msg = ResourceBundle.getBundle("msg_get", Locale.getDefault());
}
public void run() {
VKS vks;
try {
@ -50,7 +60,7 @@ public class GetCmd implements Runnable {
} else if (by.email != null) {
inputStream = get.byEmail(by.email);
} else {
throw new IllegalArgumentException("Missing --by-* option.");
throw new IllegalArgumentException(msg.getString("error.missing_by_option"));
}
int read;

View file

@ -12,23 +12,32 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
@CommandLine.Command(name = "request-verification", description = "Request verification for unverified user-ids")
@CommandLine.Command(
name = "request-verification",
resourceBundle = "msg_request_verification")
public class RequestVerificationCmd implements Runnable {
@CommandLine.Mixin
VKSCLI.KeyServerMixin keyServerMixin;
@CommandLine.Option(names = {"-t", "--token"}, description = "Access token. Can be retrieved by uploading the certificate.",
@CommandLine.Option(names = {"-t", "--token"},
required = true, arity = "1", paramLabel = "TOKEN")
String token;
@CommandLine.Option(names = {"-l", "--locale"}, description = "Locale for the verification mail")
@CommandLine.Option(names = {"-l", "--locale"})
List<String> locale = Arrays.asList("en_US", "en_GB");
@CommandLine.Option(names = {"-e", "--email"}, description = "Email addresses to request a verification mail for", required = true, arity = "1..*")
@CommandLine.Option(names = {"-e", "--email"}, required = true, arity = "1..*")
String[] addresses = new String[0];
private final ResourceBundle msg;
public RequestVerificationCmd() {
msg = ResourceBundle.getBundle("msg_request_verification", Locale.getDefault());
}
@Override
public void run() {
@ -44,10 +53,9 @@ public class RequestVerificationCmd implements Runnable {
RequestVerify.Response response = requestVerify
.forEmailAddresses(addresses)
.execute(token, locale);
System.out.println("Verification E-Mails for key " + response.getKeyFingerprint() + " have been sent.");
System.out.println("Token: " + response.getToken());
System.out.println("Status:");
System.out.printf(msg.getString("output.mails_sent"), response.getKeyFingerprint());
System.out.printf(msg.getString("output.token"), response.getToken());
System.out.println(msg.getString("output.status"));
for (String address : response.getStatus().keySet()) {
System.out.println("\t" + address + "\t" + response.getStatus().get(address));
}

View file

@ -15,17 +15,26 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
@CommandLine.Command(name = "upload", description = "Upload an OpenPGP certificate to the key server")
@CommandLine.Command(
name = "upload",
resourceBundle = "msg_upload")
public class UploadCmd implements Runnable {
@CommandLine.Mixin
VKSCLI.KeyServerMixin keyServerMixin;
@CommandLine.Option(names = {"-r", "--request-verification"},
description = "Request verification mails for unpublished email addresses")
@CommandLine.Option(names = {"-r", "--request-verification"})
boolean requestVerification;
private final ResourceBundle msg;
public UploadCmd() {
msg = ResourceBundle.getBundle("msg_upload", Locale.getDefault());
}
public void run() {
VKS vks;
try {
@ -51,11 +60,13 @@ public class UploadCmd implements Runnable {
}
}
System.out.println("Uploaded key " + response.getKeyFingerprint());
System.out.println("Token: " + response.getToken());
String msgUpload = String.format(msg.getString("output.uploaded_key"),
response.getKeyFingerprint(), response.getToken());
System.out.println(msgUpload);
String msgStatus = msg.getString("output.status");
if (!requestVerification || unpublished.isEmpty()) {
System.out.println("Status:");
System.out.println(msgStatus);
for (String address : response.getStatus().keySet()) {
Status status = response.getStatus().get(address);
System.out.format("%-" + maxMailLen + "s %s\n", address, status);
@ -65,7 +76,7 @@ public class UploadCmd implements Runnable {
RequestVerify.Response verifyResponse = vks.requestVerification().forEmailAddresses(unpublished.toArray(new String[0]))
.execute(response.getToken());
System.out.println("Status:");
System.out.println(msgStatus);
for (String address : verifyResponse.getStatus().keySet()) {
Status status = response.getStatus().get(address);
System.out.format("%-" + maxMailLen + "s %s\n", address, status);

View file

@ -9,10 +9,12 @@ import pgp.vks.client.VKSImpl;
import picocli.CommandLine;
import java.net.MalformedURLException;
import java.util.Locale;
import java.util.ResourceBundle;
@CommandLine.Command(
name = "vks",
description = "Interact with Verifying Key Servers",
resourceBundle = "msg_vks",
subcommands = {
CommandLine.HelpCommand.class,
GetCmd.class,
@ -32,14 +34,15 @@ public class VKSCLI {
}
public static int execute(String[] args) {
return new CommandLine(VKSCLI.class)
.setExitCodeExceptionMapper(new CommandLine.IExitCodeExceptionMapper() {
CommandLine cmd = new CommandLine(VKSCLI.class);
cmd.setExitCodeExceptionMapper(new CommandLine.IExitCodeExceptionMapper() {
@Override
public int getExitCode(Throwable exception) {
return 1;
}
})
.setCommandName("vkscli")
});
cmd.getSubcommands().get("help").setResourceBundle(ResourceBundle.getBundle("msg_help", Locale.getDefault()));
return cmd.setCommandName("vkscli")
.execute(args);
}
@ -53,7 +56,6 @@ public class VKSCLI {
VKSCLI parent;
@CommandLine.Option(names = "--key-server",
description = "Address of the Verifying Key Server.\nDefaults to 'https://keys.openpgp.org'",
paramLabel = "KEYSERVER")
public void setKeyServer(String keyServer) {
parent.keyServer = keyServer;