Skip to content

Commit

Permalink
Remove fallback to code
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Apr 10, 2024
1 parent 0c56b5b commit 750bb67
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public String getMessage(String code, @Nullable Object[] args, @Nullable String
}

public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
CatalogHandler.Translation translation = this.internalMessage(code, locale);
if (translation.exists()) {
return this.format(translation.toString(), args);
String translation = this.internalMessage(code, locale);
if (translation != null) {
return this.format(translation, args);
}

throw new NoSuchMessageException(code, locale);
Expand All @@ -124,9 +124,9 @@ public String getMessage(MessageSourceResolvable resolvable, Locale locale) thro
String[] codes = resolvable.getCodes();
if (codes != null) {
for (String code : codes) {
CatalogHandler.Translation translation = this.internalMessage(code, locale);
if (translation.exists()) {
return this.format(translation.toString(), resolvable.getArguments());
String translation = this.internalMessage(code, locale);
if (translation != null) {
return this.format(translation, resolvable.getArguments());
}
}
}
Expand All @@ -144,19 +144,19 @@ public void initCache() {
this.catalogHandler.initCache();
}

private CatalogHandler.Translation internalMessage(String code, Locale locale) throws NoSuchMessageException {
private String internalMessage(String code, Locale locale) throws NoSuchMessageException {
return this.findInCatalog(locale, code);
}

private String internalMessageWithDefaultMessage(String code, @Nullable String defaultMessage, Locale locale) {
CatalogHandler.Translation translation = this.findInCatalog(locale, code);
if (translation.exists()) {
return translation.toString();
String translation = this.findInCatalog(locale, code);
if (translation != null) {
return translation;
}
return defaultMessage;
}

private CatalogHandler.Translation findInCatalog(Locale locale, String code) {
private String findInCatalog(Locale locale, String code) {
return this.catalogHandler.get(locale, code);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import org.springframework.cache.Cache;

public final class CatalogHandler {
Expand All @@ -25,21 +24,17 @@ public Map<String, Map<String, String>> getAll() {
return this.catalogCache.getAll();
}

public Translation get(Locale locale, String code) {
public String get(Locale locale, String code) {
String value = this.catalogCache.get(locale, code);

if (value != null) {
return new Translation(code, value);
return value;
}

// If value not exists then init cache, because it was not in the cache. Cache empty?
// If value not exists then init cache, because it was not in the cache.
this.initCache();

// If not exists then is the value is the code.
// Non-existing code is added to the cache to fetch the non-existing code from
// the cache so as not to continue looking in the messagesource files.
this.put(locale, code, code);
return new Translation(code, code);
return null;
}

void put(Locale locale, String code, String value) {
Expand All @@ -53,23 +48,4 @@ void put(Locale locale, String code, String value) {
public void initCache() {
this.catalogCache.initCache();
}

public static class Translation {

String code;
String value;

public Translation(String code, String value) {
this.code = code;
this.value = value;
}

public boolean exists() {
return !Objects.equals(this.code, this.value);
}

public String toString() {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void beforeEach() {
@Test
void test_put() {
this.catalogHandler.put(this.locale, "my-test-code", "my-test-value");
assertEquals("my-test-value", this.catalogHandler.get(locale, "my-test-code").toString());
assertEquals("my-test-value", this.catalogHandler.get(locale, "my-test-code"));
}

@Test
Expand All @@ -39,14 +39,9 @@ void test_initCache() {

@Test
void test_get() {
assertEquals("Hello World (messages / en)", this.catalogHandler.get(this.locale, "hello_world").toString());
assertEquals("Hello World (messages / en)", this.catalogHandler.get(this.locale, "hello_world"));
assertEquals("Hello World (messages / en)",
this.catalogHandler.get(this.locale, "messages.hello_world").toString());
}

@Test
void test_get_notExists() {
assertEquals("not-exists-id", this.catalogHandler.get(this.locale, "not-exists-id").toString());
this.catalogHandler.get(this.locale, "messages.hello_world"));
}

}

0 comments on commit 750bb67

Please sign in to comment.