Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ public final void unregister(final String key) {
this.translations.remove(key);
}

@Override
public final void unregister(final String key, final Locale locale) {
this.translations.computeIfPresent(requireNonNull(key, "key"), (ignored, translation) -> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why computeIfPresent? I don't know why we need to create one if absent when we are unregistering.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used computeIfPresent specifically to avoid creating a translation entry when unregistering an unknown key. If the key exists, we remove the locale; if no locales remain, returning null removes the key. Otherwise, it’s a no-op.

translation.unregister(requireNonNull(locale, "locale"));
return translation.isEmpty() ? null : translation;
});
}

@Override
public final Key name() {
return this.name;
Expand Down Expand Up @@ -201,6 +209,14 @@ private void register(final Locale locale, final T translation) {
}
}

private void unregister(final Locale locale) {
this.translations.remove(locale);
}

private boolean isEmpty() {
return this.translations.isEmpty();
}

@Override
public boolean equals(final Object other) {
if (this == other) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ default boolean canTranslate(final String key, final Locale locale) {
*/
void unregister(final String key);

/**
* Unregisters a translation for a key and locale.
*
* <p>If this is the last translation registered for the key, the key is also
* unregistered.</p>
*
* @param key a translation key
* @param locale a locale
* @since 5.2.1
*/
void unregister(final String key, final Locale locale);

/**
* An abstract, string-based translation store.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class TranslationStoreTest {
static final TranslationStore.StringBased<MessageFormat> REGISTRY = TranslationStore.messageFormat(Key.key("adventure", "test"));
Expand All @@ -58,6 +60,35 @@ void testRegister_duplicate() {
assertThrows(IllegalArgumentException.class, () -> REGISTRY.register("test", Locale.US, new MessageFormat("Another test.")));
}

@Test
void testUnregister_locale() {
final TranslationStore.StringBased<MessageFormat> store = TranslationStore.messageFormat(Key.key("adventure", "locale-unregister"));
final MessageFormat english = new MessageFormat("Hello", Locale.US);
final MessageFormat german = new MessageFormat("Hallo", Locale.GERMANY);
store.register("hello-world", Locale.US, english);
store.register("hello-world", Locale.GERMANY, german);

store.unregister("hello-world", Locale.GERMANY);

assertEquals(english, store.translate("hello-world", Locale.US));
assertFalse(store.contains("hello-world", Locale.GERMANY));
assertTrue(store.contains("hello-world"));
}

@Test
void testUnregister_locale_lastTranslation() {
final TranslationStore.StringBased<MessageFormat> store = TranslationStore.messageFormat(Key.key("adventure", "locale-unregister-last"));
final MessageFormat translation = new MessageFormat("Hello", Locale.US);
store.register("hello-world", Locale.US, translation);

store.unregister("hello-world", Locale.US);

assertFalse(store.contains("hello-world"));
assertFalse(store.contains("hello-world", Locale.US));
store.register("hello-world", Locale.US, translation);
assertEquals(translation, store.translate("hello-world", Locale.US));
}

@Test
void testTranslate() {
final MessageFormat expected = new MessageFormat("A what?", Locale.CANADA);
Expand Down