Wrap long i18n strings for use in API

Some i18n property values can be used by the API if long strings are
wrapped before written as commments to json payment account forms, or
written to the CLI console.

This change anticipates the addition of the more complex Swift payment
method (PR 5672). PR 5672's i18n property value for key "payment.swift.info"
will be wrapped and appended to the comments of the Swift payment account's
json form.
This commit is contained in:
ghubstan 2021-09-06 12:12:09 -03:00
parent 8a42109a7a
commit f39e611150
No known key found for this signature in database
GPG key ID: E35592D6800A861E
2 changed files with 39 additions and 5 deletions

View file

@ -52,9 +52,11 @@ import java.nio.file.Paths;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
@ -337,12 +339,12 @@ public class Utilities {
public static void openURI(URI uri) throws IOException {
if (!DesktopUtil.browse(uri))
throw new IOException("Failed to open URI: " + uri.toString());
throw new IOException("Failed to open URI: " + uri);
}
public static void openFile(File file) throws IOException {
if (!DesktopUtil.open(file))
throw new IOException("Failed to open file: " + file.toString());
throw new IOException("Failed to open file: " + file);
}
public static String getDownloadOfHomeDir() {
@ -480,6 +482,16 @@ public class Utilities {
}
public static List<String> toListOfWrappedStrings(String s, int wrapLength) {
StringBuilder sb = new StringBuilder(s);
int i = 0;
while (i + wrapLength < sb.length() && (i = sb.lastIndexOf(" ", i + wrapLength)) != -1) {
sb.replace(i, i + 1, "\n");
}
String[] splitLine = sb.toString().split("\n");
return Arrays.asList(splitLine);
}
public static String getRandomPrefix(int minLength, int maxLength) {
int length = minLength + new Random().nextInt(maxLength - minLength + 1);
String result;

View file

@ -33,6 +33,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
@ -42,6 +44,9 @@ import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import static bisq.common.util.Utilities.toListOfWrappedStrings;
import static java.nio.charset.StandardCharsets.UTF_8;
@Slf4j
public class Res {
public static void setup() {
@ -125,13 +130,30 @@ public class Res {
return key;
}
}
public static List<String> getWrappedAsList(String key, int wrapLength) {
String[] raw = get(key).split("\n");
List<String> wrapped = new ArrayList<>();
for (String s : raw) {
List<String> list = toListOfWrappedStrings(s, wrapLength);
for (String line : list) {
if (!line.isEmpty())
wrapped.add(line);
}
}
return wrapped;
}
}
// Adds UTF8 support for property files
class UTF8Control extends ResourceBundle.Control {
public ResourceBundle newBundle(String baseName, @NotNull Locale locale, @NotNull String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
public ResourceBundle newBundle(String baseName,
@NotNull Locale locale,
@NotNull String format,
ClassLoader loader,
boolean reload)
throws IOException {
// Below is a copy of the default implementation.
final String bundleName = toBundleName(baseName, locale);
final String resourceName = toResourceName(bundleName, "properties");
@ -152,7 +174,7 @@ class UTF8Control extends ResourceBundle.Control {
if (stream != null) {
try {
// Only this line is changed to make it read properties files as UTF-8.
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
bundle = new PropertyResourceBundle(new InputStreamReader(stream, UTF_8));
} finally {
stream.close();
}