Handle non-country locales better. Fixes #1345

This commit is contained in:
Matthew Miller 2020-05-14 23:48:20 +10:00
parent 267fe1ffe0
commit a2c541bdac
2 changed files with 19 additions and 5 deletions

View File

@ -25,6 +25,8 @@
import java.net.URL;
import java.nio.file.Path;
import javax.annotation.Nullable;
/**
* An abstract loader that handles loading resources from bundled URLs or local
* files.
@ -43,6 +45,7 @@ public interface ResourceLoader {
* @return The URL to this bundled resource
* @throws IOException if an IO issue occurs
*/
@Nullable
default URL getResource(Class<?> clazz, String pathName) throws IOException {
checkArgument(!pathName.startsWith("/"), "pathName must not start with /");
String qualifiedName = clazz.getName().substring(0, clazz.getName().lastIndexOf('.'))
@ -62,6 +65,7 @@ default URL getResource(Class<?> clazz, String pathName) throws IOException {
* @return The URL to this bundled resource
* @throws IOException if an IO issue occurs
*/
@Nullable
default URL getRootResource(String pathName) throws IOException {
checkArgument(!pathName.startsWith("/"), "pathName must not start with /");
return this.getClass().getClassLoader().getResource(pathName);

View File

@ -32,6 +32,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@ -43,6 +44,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.stream.Collectors.toMap;
/**
@ -85,6 +87,7 @@ public static String makeTranslationKey(String type, String id) {
private final Set<Locale> checkedLocales = new HashSet<>();
public TranslationManager(ResourceLoader resourceLoader) {
checkNotNull(resourceLoader);
this.resourceLoader = resourceLoader;
}
@ -106,13 +109,17 @@ private Map<String, String> parseTranslationFile(InputStream inputStream) throws
}
private Optional<Map<String, String>> loadTranslationFile(String filename) {
Map<String, String> baseTranslations;
Map<String, String> baseTranslations = new ConcurrentHashMap<>();
try (InputStream stream = resourceLoader.getRootResource("lang/" + filename).openStream()) {
baseTranslations = parseTranslationFile(stream);
try {
URL resource = resourceLoader.getRootResource("lang/" + filename);
if (resource != null) {
try (InputStream stream = resource.openStream()) {
baseTranslations = parseTranslationFile(stream);
}
}
} catch (IOException e) {
// Seem to be missing base. If the user has provided a file use that.
baseTranslations = new ConcurrentHashMap<>();
}
Path localFile = resourceLoader.getLocalResource("lang/" + filename);
@ -138,7 +145,10 @@ private boolean tryLoadTranslations(Locale locale) {
if (!locale.equals(defaultLocale)) {
baseTranslations.putAll(getTranslationMap(defaultLocale));
}
Optional<Map<String, String>> langData = loadTranslationFile(locale.getLanguage() + "-" + locale.getCountry() + "/strings.json");
Optional<Map<String, String>> langData = Optional.empty();
if (!locale.getCountry().isEmpty()) {
langData = loadTranslationFile(locale.getLanguage() + "-" + locale.getCountry() + "/strings.json");
}
if (!langData.isPresent()) {
langData = loadTranslationFile(locale.getLanguage() + "/strings.json");
}