|
19 | 19 |
|
20 | 20 | package com.sk89q.worldedit.util.translation;
|
21 | 21 |
|
| 22 | +import com.google.common.collect.Lists; |
22 | 23 | import com.google.common.collect.Sets;
|
23 | 24 | import com.google.common.collect.Table;
|
24 | 25 | import com.google.common.collect.Tables;
|
|
45 | 46 | import java.nio.file.Path;
|
46 | 47 | import java.text.MessageFormat;
|
47 | 48 | import java.util.HashMap;
|
| 49 | +import java.util.List; |
48 | 50 | import java.util.Locale;
|
49 | 51 | import java.util.Map;
|
50 | 52 | import java.util.Set;
|
|
53 | 55 | import java.util.concurrent.Future;
|
54 | 56 | import java.util.concurrent.locks.Lock;
|
55 | 57 | import java.util.concurrent.locks.ReentrantLock;
|
| 58 | +import java.util.function.Consumer; |
56 | 59 | import javax.annotation.Nullable;
|
57 | 60 |
|
58 | 61 | import static com.google.common.base.Preconditions.checkNotNull;
|
@@ -94,6 +97,7 @@ public static String makeTranslationKey(String type, String id) {
|
94 | 97 | private final Map<Locale, Future<Void>> loadFutures = new HashMap<>();
|
95 | 98 | private final Set<Locale> loadedLocales = Sets.newConcurrentHashSet();
|
96 | 99 | private final Lock loadLock = new ReentrantLock();
|
| 100 | + private final List<Consumer<Locale>> localeChangeListeners = Lists.newArrayList(); |
97 | 101 | private Locale defaultLocale = Locale.ENGLISH;
|
98 | 102 |
|
99 | 103 | private final ArchiveUnpacker archiveUnpacker;
|
@@ -140,6 +144,27 @@ public void reload() {
|
140 | 144 | }
|
141 | 145 | }
|
142 | 146 |
|
| 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 | + |
143 | 168 | public Component convertText(Component component, Locale locale) {
|
144 | 169 | return friendlyComponentRenderer.render(component, locale);
|
145 | 170 | }
|
@@ -241,6 +266,9 @@ private void loadTranslations(Locale locale) throws IOException {
|
241 | 266 | locale, entry.getKey(), format
|
242 | 267 | );
|
243 | 268 | }
|
| 269 | + |
| 270 | + // Notify listeners of locale change |
| 271 | + localeChangeListeners.forEach(listener -> listener.accept(locale)); |
244 | 272 | }
|
245 | 273 |
|
246 | 274 | private String getLocalePath(Locale locale) {
|
|
0 commit comments