Fix loading dev i18n files

This forces the repo file to FULLY OVERRIDE the default locale. Nothing
will be loaded from the default locale in either the config folder, or
the i18n dependency.
This commit is contained in:
Octavia Togami 2020-10-17 14:05:18 -07:00
parent 56182ad079
commit 83e744bae2
3 changed files with 47 additions and 31 deletions

View File

@ -125,6 +125,8 @@
exclude(".cache") exclude(".cache")
exclude("LICENSE*") exclude("LICENSE*")
exclude("META-INF/maven/**") exclude("META-INF/maven/**")
// it's in the i18n zip, we only use it in dev
exclude("**/lang/strings.json")
minimize() minimize()
} }
} }

View File

@ -109,8 +109,6 @@
} }
tasks.named<Copy>("processResources") { tasks.named<Copy>("processResources") {
// it's in the zip too
exclude("**/lang/strings.json")
from(configurations.named("languageFiles")) { from(configurations.named("languageFiles")) {
rename { rename {
"i18n.zip" "i18n.zip"

View File

@ -39,6 +39,7 @@
import java.io.Reader; import java.io.Reader;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -183,35 +184,7 @@ private void loadLocale(Locale locale) {
} }
try { try {
String localePath = getLocalePath(locale); loadTranslations(locale);
Map<String, String> entries = new HashMap<>();
// From lowest priority to highest
putTranslationData(entries, this.internalZipRoot.resolve(localePath));
if (this.userProvidedZipRoot != null) {
putTranslationData(entries, this.userProvidedZipRoot.resolve(localePath));
}
putTranslationData(entries, this.userProvidedFlatRoot.resolve(localePath));
// Load message formats
for (Map.Entry<String, String> entry : entries.entrySet()) {
MessageFormat format;
try {
format = new MessageFormat(entry.getValue().replace("'", "''"), locale);
} catch (IllegalArgumentException e) {
LOGGER.warn(
"Failed to load translation"
+ ", locale=" + locale
+ ", key=" + entry.getKey()
+ ", value=" + entry.getValue(),
e
);
continue;
}
translationTable.put(
locale, entry.getKey(), format
);
}
} catch (Exception t) { } catch (Exception t) {
LOGGER.warn( LOGGER.warn(
"Failed to load translations" "Failed to load translations"
@ -224,6 +197,49 @@ private void loadLocale(Locale locale) {
} }
} }
private void loadTranslations(Locale locale) throws IOException {
Map<String, String> entries = new HashMap<>();
// If needed, load the dev strings as an OVERRIDE!
// In dev, reading lang/strings.json from either i18n.zip or the config folder
// WILL NOT OCCUR!
URL devStrings;
if (locale == defaultLocale
&& (devStrings = resourceLoader.getRootResource("lang/strings.json")) != null) {
try (InputStream in = devStrings.openStream()) {
putTranslationData(entries, in);
}
} else {
String localePath = getLocalePath(locale);
// From lowest priority to highest
putTranslationData(entries, this.internalZipRoot.resolve(localePath));
if (this.userProvidedZipRoot != null) {
putTranslationData(entries, this.userProvidedZipRoot.resolve(localePath));
}
putTranslationData(entries, this.userProvidedFlatRoot.resolve(localePath));
}
// Load message formats
for (Map.Entry<String, String> entry : entries.entrySet()) {
MessageFormat format;
try {
format = new MessageFormat(entry.getValue().replace("'", "''"), locale);
} catch (IllegalArgumentException e) {
LOGGER.warn(
"Failed to load translation"
+ ", locale=" + locale
+ ", key=" + entry.getKey()
+ ", value=" + entry.getValue(),
e
);
continue;
}
translationTable.put(
locale, entry.getKey(), format
);
}
}
private String getLocalePath(Locale locale) { private String getLocalePath(Locale locale) {
if (defaultLocale.equals(locale)) { if (defaultLocale.equals(locale)) {
return "strings.json"; return "strings.json";