mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
locale-loading-changes
This commit is contained in:
1 parent
3d721282e4
commit
c688ec7a50
1 file changed
+38
-31
@@ -118,33 +118,21 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
|
|||||||
&& pathString.endsWith(".properties");
|
&& pathString.endsWith(".properties");
|
||||||
})
|
})
|
||||||
.forEach(localeFile -> {
|
.forEach(localeFile -> {
|
||||||
final String localeString = localeFile.getFileName().toString().substring("messages-".length()).replace(".properties", "");
|
final String localeString = localeString(localeFile);
|
||||||
// MC uses no_NO when the player selects nb_NO...
|
final @Nullable Locale locale = parseLocale(localeString);
|
||||||
final @Nullable Locale locale = Translator.parseLocale(localeString.replace("nb_NO", "no_NO"));
|
|
||||||
|
|
||||||
if (locale == null) {
|
if (locale == null) {
|
||||||
this.logger.warn("Unknown locale '{}'?", localeString);
|
this.logger.warn("Unknown locale '{}'?", localeString);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.info("Found locale {} ({}) in: {}", locale.getDisplayName(), locale, localeFile);
|
readLocale(localeDirectory, localeFile, locale);
|
||||||
|
|
||||||
final Properties properties = new Properties();
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.loadProperties(properties, localeDirectory, localeFile);
|
|
||||||
this.locales.put(locale, properties);
|
|
||||||
|
|
||||||
this.logger.info("Successfully loaded locale {} ({})", locale.getDisplayName(), locale);
|
|
||||||
} catch (final IOException ex) {
|
|
||||||
this.logger.warn("Unable to load locale {} ({}) from source: {}", locale.getDisplayName(), locale, localeFile, ex);
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
try (final Stream<Path> paths = Files.walk(localeDirectory)) {
|
try (final Stream<Path> paths = Files.list(localeDirectory)) {
|
||||||
paths.filter(Files::isRegularFile).forEach(path -> {
|
paths.filter(Files::isRegularFile).forEach(localeFile -> {
|
||||||
final String localeString = path.getFileName().toString().substring("messages-".length()).replace(".properties", "");
|
final String localeString = localeString(localeFile);
|
||||||
final @Nullable Locale locale = Translator.parseLocale(localeString.replace("nb_NO", "no_NO"));
|
final @Nullable Locale locale = parseLocale(localeString);
|
||||||
|
|
||||||
if (locale == null) {
|
if (locale == null) {
|
||||||
this.logger.warn("Unknown locale '{}'?", localeString);
|
this.logger.warn("Unknown locale '{}'?", localeString);
|
||||||
@@ -155,22 +143,26 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.info("Found locale {} ({}) in: {}", locale.getDisplayName(), locale, path);
|
this.readLocale(localeDirectory, localeFile, locale);
|
||||||
|
|
||||||
final Properties properties = new Properties();
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.loadProperties(properties, localeDirectory, path);
|
|
||||||
this.locales.put(locale, properties);
|
|
||||||
|
|
||||||
this.logger.info("Successfully loaded locale {} ({})", locale.getDisplayName(), locale);
|
|
||||||
} catch (final IOException ex) {
|
|
||||||
this.logger.warn("Unable to load locale {} ({}) from source: {}", locale.getDisplayName(), locale, path, ex);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void readLocale(final Path localeDirectory, final Path localeFile, final Locale locale) {
|
||||||
|
this.logger.info("Found locale {} ({}) in: {}", locale.getDisplayName(), locale, localeFile);
|
||||||
|
|
||||||
|
final Properties properties = new Properties();
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.loadProperties(properties, localeDirectory, localeFile);
|
||||||
|
this.locales.put(locale, properties);
|
||||||
|
|
||||||
|
this.logger.info("Successfully loaded locale {} ({})", locale.getDisplayName(), locale);
|
||||||
|
} catch (final IOException ex) {
|
||||||
|
this.logger.warn("Unable to load locale {} ({}) from source: {}", locale.getDisplayName(), locale, localeFile, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String messageOf(final Audience receiver, final String messageKey) {
|
public String messageOf(final Audience receiver, final String messageKey) {
|
||||||
Audience audience = receiver;
|
Audience audience = receiver;
|
||||||
@@ -236,6 +228,10 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
|
|||||||
final Path localeFile
|
final Path localeFile
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
final Path savedFile = localeDirectory.resolve(localeFile.getFileName().toString());
|
final Path savedFile = localeDirectory.resolve(localeFile.getFileName().toString());
|
||||||
|
if (localeFile.normalize().toAbsolutePath().equals(savedFile.normalize().toAbsolutePath())) {
|
||||||
|
// Origin file and destination file are the same (ie locale was not in the jar, user added)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// If the file in the localeDirectory exists, read it to the properties
|
// If the file in the localeDirectory exists, read it to the properties
|
||||||
if (Files.isRegularFile(savedFile)) {
|
if (Files.isRegularFile(savedFile)) {
|
||||||
@@ -265,4 +261,15 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String localeString(final Path localeFile) {
|
||||||
|
return localeFile.getFileName().toString().substring("messages-".length()).replace(".properties", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static @Nullable Locale parseLocale(String localeString) {
|
||||||
|
// MC uses no_NO when the player selects nb_NO...
|
||||||
|
localeString = localeString.replace("nb_NO", "no_NO");
|
||||||
|
|
||||||
|
return Translator.parseLocale(localeString);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user