Skip to content

Commit

Permalink
Add method to inject locale codes in localizer builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed Aug 24, 2024
1 parent 6d16508 commit a0a582c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ plugins {
publishData {
addRepo(Repo.main("", "", false))
addRepo(Repo.snapshot("SNAPSHOT", "", false))
publishingVersion = "2.1.5"
publishingVersion = "2.1.6"
}
version = publishData.getVersion()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class Localizer implements ILocalizer {
private final String localesPrefix;
private final Set<String> includedLocales;
private final Pattern localePattern = Pattern.compile("_(([a-zA-Z]{2})(_[a-zA-Z]{2})?)\\.properties");
private final Map<String, String> runtimeLocaleCodes = new HashMap<>();
private final Map<String, String> runtimeLocaleCodes;
private final Map<String, ResourceBundle> languages = new HashMap<>();
private final Function<Player, String> userLocale;
private final List<ILocalizer> children = new ArrayList<>();
Expand All @@ -85,13 +85,14 @@ public class Localizer implements ILocalizer {
* @param includedLocales internal provided locales
*/
Localizer(Plugin plugin, String localesPath,
String localesPrefix, String fallbackLocale, Function<Player, String> userLocale, Set<String> includedLocales) {
String localesPrefix, String fallbackLocale, Function<Player, String> userLocale, Set<String> includedLocales, Map<String, String> runtimeLocaleCodes) {
this.plugin = plugin;
this.localesPath = localesPath;
this.localesPrefix = localesPrefix;
this.userLocale = userLocale;
this.includedLocales = includedLocales;
defaultLanguage = fallbackLocale;
this.runtimeLocaleCodes = runtimeLocaleCodes;
createDefaults();
bootstrap();
loadLanguage(fallbackLocale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
import org.bukkit.plugin.Plugin;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

public class LocalizerBuilder {
private final Plugin plugin;
private final String fallbackLocale;
private final Set<String> includedLocales = new HashSet<>();
private final Map<String, String> runtimeLocaleCodes = new HashMap<>();
private String localesPath = "messages";
private String localesPrefix = "messages";
private Function<Player, String> userLocale;
Expand Down Expand Up @@ -49,7 +52,17 @@ public LocalizerBuilder setIncludedLocales(String... includedLocales) {
return this;
}

public LocalizerBuilder addLocaleCodes(String key, String value) {
this.runtimeLocaleCodes.put(key, value);
return this;
}

public LocalizerBuilder addLocaleCodes(Map<String, String> runtimeLocaleCodes) {
this.runtimeLocaleCodes.putAll(runtimeLocaleCodes);
return this;
}

public Localizer build() {
return new Localizer(plugin, localesPath, localesPrefix, fallbackLocale, userLocale, includedLocales);
return new Localizer(plugin, localesPath, localesPrefix, fallbackLocale, userLocale, includedLocales, runtimeLocaleCodes);
}
}

0 comments on commit a0a582c

Please sign in to comment.