Skip to content

Commit 3110d71

Browse files
committed
Add a locale load callback to the Translation Manager
1 parent 5311195 commit 3110d71

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java

+28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.sk89q.worldedit.util.translation;
2121

22+
import com.google.common.collect.Lists;
2223
import com.google.common.collect.Sets;
2324
import com.google.common.collect.Table;
2425
import com.google.common.collect.Tables;
@@ -45,6 +46,7 @@
4546
import java.nio.file.Path;
4647
import java.text.MessageFormat;
4748
import java.util.HashMap;
49+
import java.util.List;
4850
import java.util.Locale;
4951
import java.util.Map;
5052
import java.util.Set;
@@ -53,6 +55,7 @@
5355
import java.util.concurrent.Future;
5456
import java.util.concurrent.locks.Lock;
5557
import java.util.concurrent.locks.ReentrantLock;
58+
import java.util.function.Consumer;
5659
import javax.annotation.Nullable;
5760

5861
import static com.google.common.base.Preconditions.checkNotNull;
@@ -94,6 +97,7 @@ public static String makeTranslationKey(String type, String id) {
9497
private final Map<Locale, Future<Void>> loadFutures = new HashMap<>();
9598
private final Set<Locale> loadedLocales = Sets.newConcurrentHashSet();
9699
private final Lock loadLock = new ReentrantLock();
100+
private final List<Consumer<Locale>> localeChangeListeners = Lists.newArrayList();
97101
private Locale defaultLocale = Locale.ENGLISH;
98102

99103
private final ArchiveUnpacker archiveUnpacker;
@@ -140,6 +144,27 @@ public void reload() {
140144
}
141145
}
142146

147+
/**
148+
* Adds a listener that will be notified when the locale changes.
149+
*
150+
* <p>
151+
* Note: this can be called multiple times with the same locale, for example if the translation manager is reloaded.
152+
* </p>
153+
*
154+
* @param listener a consumer that will be called with the updated locale
155+
* @param notifyExisting if true, the listener will be called with all loaded locales immediately
156+
*/
157+
public void addLocaleChangeListener(Consumer<Locale> listener, boolean notifyExisting) {
158+
localeChangeListeners.add(listener);
159+
160+
if (notifyExisting) {
161+
// Notify the listener of all loaded locales
162+
for (Locale locale : loadedLocales) {
163+
listener.accept(locale);
164+
}
165+
}
166+
}
167+
143168
public Component convertText(Component component, Locale locale) {
144169
return friendlyComponentRenderer.render(component, locale);
145170
}
@@ -241,6 +266,9 @@ private void loadTranslations(Locale locale) throws IOException {
241266
locale, entry.getKey(), format
242267
);
243268
}
269+
270+
// Notify listeners of locale change
271+
localeChangeListeners.forEach(listener -> listener.accept(locale));
244272
}
245273

246274
private String getLocalePath(Locale locale) {

0 commit comments

Comments
 (0)