Skip to content

Commit

Permalink
parseLocale helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rymiel committed Jan 27, 2024
1 parent 74cf6b7 commit f481bbe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.popcraft.bolt.lang.Translator;

import java.util.Locale;

Expand Down Expand Up @@ -76,7 +77,7 @@ public static String translateRaw(final String key, final CommandSender sender)
*/
public static Locale getLocaleOf(CommandSender sender) {
if (sender instanceof Player player) {
return Locale.forLanguageTag(player.getLocale().replace('_', '-')); // reasons
return Translator.parseLocale(player.getLocale());
} else {
return new Locale("");
}
Expand Down
18 changes: 13 additions & 5 deletions common/src/main/java/org/popcraft/bolt/lang/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public static void loadAllTranslations(final Path directory, final String prefer
files.forEach(path -> {
if (!path.toString().endsWith(".properties")) return;

final String localeName = path.getFileName().toString().split("\\.")[0].replace('_', '-');
final Locale locale = Locale.forLanguageTag(localeName);
final Locale locale = parseLocale(path.getFileName().toString().split("\\.", 2)[0]);
final Properties properties = loadTranslation(locale.toString());
languages.put(locale, properties);
});
Expand All @@ -97,8 +96,7 @@ public static void loadAllTranslations(final Path directory, final String prefer
// Load user-defined localization files. This is done after, so it overrides any built-in translations.
try (Stream<Path> files = Files.list(directory).filter((name) -> name.toString().toLowerCase().endsWith(".properties"))) {
files.forEach(path -> {
final String localeName = path.getFileName().toString().split("\\.")[0].replace('_', '-');
final Locale locale = Locale.forLanguageTag(localeName);
final Locale locale = parseLocale(path.getFileName().toString().split("\\.", 2)[0]);
final Properties properties = loadTranslationFromFile(path);
languages.put(locale, properties);
});
Expand All @@ -107,7 +105,7 @@ public static void loadAllTranslations(final Path directory, final String prefer
}

// Load the preferred fallback language
translation = languages.getOrDefault(Locale.forLanguageTag(preferredLanguage.replace('_', '-')), fallback);
translation = languages.getOrDefault(parseLocale(preferredLanguage), fallback);

// This is no longer really that useful, but keep it around
selected = preferredLanguage;
Expand Down Expand Up @@ -143,4 +141,14 @@ private static Properties loadTranslationFromFile(final Path path) {
}
return properties;
}

public static Locale parseLocale(final String string) {
final String[] segments = string.split("_", 3);
return switch (segments.length) {
case 1 -> new Locale(string);
case 2 -> new Locale(segments[0], segments[1]);
case 3 -> new Locale(segments[0], segments[1], segments[2]);
default -> new Locale("");
};
}
}

0 comments on commit f481bbe

Please sign in to comment.