Added ability to customize translations (#692)

* Added ability to customize translations

* Removed remaining usage of `Paths#get`

As of java 11 its replacement `Path#of` was created, which is called when using `Paths#get`, besides, according to documentation, it mentions that it can be deprecated at any time

And fix a minor typo in LegacyChannelIdentifier
This commit is contained in:
4drian3d
2022-04-15 23:38:44 -05:00
committed by GitHub
parent 3cb10f6ad4
commit acc407a6d5
6 changed files with 37 additions and 23 deletions

View File

@@ -21,7 +21,7 @@ import com.velocitypowered.proxy.config.VelocityConfiguration;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
@@ -42,7 +42,7 @@ public class Metrics {
private MetricsBase metricsBase;
private Metrics(Logger logger, int serviceId, boolean defaultEnabled) {
File configFile = Paths.get("plugins").resolve("bStats").resolve("config.txt").toFile();
File configFile = Path.of("plugins", "bStats", "config.txt").toFile();
MetricsConfig config;
try {
config = new MetricsConfig(configFile, defaultEnabled);

View File

@@ -67,10 +67,10 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.KeyPair;
import java.security.PrivilegedAction;
@@ -252,8 +252,30 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
FileSystemUtils.visitResources(VelocityServer.class, path -> {
logger.info("Loading localizations...");
final Path langPath = Path.of("lang");
try {
Files.walk(path).forEach(file -> {
if (!Files.exists(langPath)) {
Files.createDirectory(langPath);
Files.walk(path).forEach(file -> {
if (!Files.isRegularFile(file)) {
return;
}
try {
Path langFile = langPath.resolve(file.getFileName().toString());
if (!Files.exists(langFile)) {
try (InputStream is = Files.newInputStream(file)) {
Files.copy(is, langFile);
}
}
} catch (IOException e) {
logger.error("Encountered an I/O error whilst loading translations", e);
}
});
}
Files.walk(langPath).forEach(file -> {
if (!Files.isRegularFile(file)) {
return;
}
@@ -263,16 +285,11 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
String localeName = filename.replace("messages_", "")
.replace("messages", "")
.replace('_', '-');
Locale locale;
if (localeName.isEmpty()) {
locale = Locale.US;
} else {
locale = Locale.forLanguageTag(localeName);
}
Locale locale = localeName.isBlank()
? Locale.US
: Locale.forLanguageTag(localeName);
translationRegistry.registerAll(locale,
ResourceBundle.getBundle("com/velocitypowered/proxy/l10n/messages",
locale, UTF8ResourceBundleControl.get()), false);
translationRegistry.registerAll(locale, file, false);
ClosestLocaleMatcher.INSTANCE.registerKnown(locale);
});
} catch (IOException e) {
@@ -289,7 +306,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
@SuppressFBWarnings("DM_EXIT")
private void doStartupConfigLoad() {
try {
Path configPath = Paths.get("velocity.toml");
Path configPath = Path.of("velocity.toml");
configuration = VelocityConfiguration.read(configPath);
if (!configuration.validate()) {
@@ -311,7 +328,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
logger.info("Loading plugins...");
try {
Path pluginPath = Paths.get("plugins");
Path pluginPath = Path.of("plugins");
if (!pluginPath.toFile().exists()) {
Files.createDirectory(pluginPath);
@@ -363,7 +380,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
* @throws IOException if we can't read {@code velocity.toml}
*/
public boolean reloadConfiguration() throws IOException {
Path configPath = Paths.get("velocity.toml");
Path configPath = Path.of("velocity.toml");
VelocityConfiguration newConfiguration = VelocityConfiguration.read(configPath);
if (!newConfiguration.validate()) {

View File

@@ -38,7 +38,6 @@ import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.SecureRandom;
import java.util.HashMap;
@@ -219,7 +218,7 @@ public class VelocityConfiguration implements ProxyConfig {
}
private void loadFavicon() {
Path faviconPath = Paths.get("server-icon.png");
Path faviconPath = Path.of("server-icon.png");
if (Files.exists(faviconPath)) {
try {
this.favicon = Favicon.create(faviconPath);

View File

@@ -26,7 +26,6 @@ import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -83,7 +82,7 @@ public class FileSystemUtils {
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
consumer.accept(Paths.get(uri));
consumer.accept(Path.of(uri));
return true;
}
}