From 750bb67003f5ab7b136a89f3d60d774d6db614ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Laugks?= Date: Wed, 10 Apr 2024 22:43:35 +0200 Subject: [PATCH] Remove fallback to code --- .../xliff/XliffTranslationMessageSource.java | 22 ++++++------- .../xliff/catalog/CatalogHandler.java | 32 +++---------------- .../xliff/catalog/CatalogHandlerTest.java | 11 ++----- 3 files changed, 18 insertions(+), 47 deletions(-) diff --git a/src/main/java/io/github/alaugks/spring/messagesource/xliff/XliffTranslationMessageSource.java b/src/main/java/io/github/alaugks/spring/messagesource/xliff/XliffTranslationMessageSource.java index 0e06ebc..5b76313 100644 --- a/src/main/java/io/github/alaugks/spring/messagesource/xliff/XliffTranslationMessageSource.java +++ b/src/main/java/io/github/alaugks/spring/messagesource/xliff/XliffTranslationMessageSource.java @@ -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); @@ -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()); } } } @@ -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); } diff --git a/src/main/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandler.java b/src/main/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandler.java index 97ca084..1fa853e 100644 --- a/src/main/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandler.java +++ b/src/main/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandler.java @@ -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 { @@ -25,21 +24,17 @@ public Map> 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) { @@ -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; - } - } } diff --git a/src/test/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandlerTest.java b/src/test/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandlerTest.java index 174c085..f3a13b0 100644 --- a/src/test/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandlerTest.java +++ b/src/test/java/io/github/alaugks/spring/messagesource/xliff/catalog/CatalogHandlerTest.java @@ -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 @@ -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")); } }