From c7aad8f9a74c334b90c156c95e60a81b6b13c62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Laugks?= Date: Sun, 1 Sep 2024 14:40:09 +0200 Subject: [PATCH] Remove cache is required --- README.md | 115 ++++-------------- pom.xml | 4 +- .../xliff/XliffTranslationMessageSource.java | 16 ++- ...ourcePatternResolverInternalCacheTest.java | 15 +++ 4 files changed, 54 insertions(+), 96 deletions(-) create mode 100644 src/test/java/io/github/alaugks/spring/messagesource/xliff/XliffMatchingResourcePatternResolverInternalCacheTest.java diff --git a/README.md b/README.md index 7dd5c85..1fc4c6f 100644 --- a/README.md +++ b/README.md @@ -7,19 +7,18 @@ This package provides a **MessageSource** for using translations from XLIFF file 1. [Version](#a1) 2. [Dependency](#a2) 3. [MessageSource Configuration](#a3) -4. [CacheManager Configuration](#a4) -6. [Cache warming with an ApplicationRunner (recommended)](#a5) -7. [XLIFF Translation Files](#a6) -8. [Using the MessageSource](#a7) -9. [Full Example](#a8) -10. [Support](#a9) -11. [More Information](#a10) +4. [XLIFF Translation Files](#a4) +5. [Using the MessageSource](#a5) +6. [Full Example](#a6) +7. [Support](#a7) +8. [More Information](#a8) ## 1. Versions | Version | Description | |:---------------|:------------------------------------------------------------------------------------------| +| 1.3.0 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.3.0) | | 1.2.1 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.2.1) | | 1.2.0 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.2.0) | | 1.1.2 | [Release notes](https://github.com/alaugks/spring-messagesource-xliff/releases/tag/1.1.2) | @@ -33,7 +32,7 @@ This package provides a **MessageSource** for using translations from XLIFF file | 2.0.0-SNAPSHOT | [SNAPSHOT](https://github.com/alaugks/spring-messagesource-xliff/tree/snapshot/2.0.0) | | -[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=alaugks_spring-xliff-translation&metric=alert_status)](https://sonarcloud.io/summary/overall?id=alaugks_spring-xliff-translation) [![Maven Central](https://img.shields.io/maven-central/v/io.github.alaugks/spring-messagesource-xliff.svg?label=Maven%20Central)](https://central.sonatype.com/artifact/io.github.alaugks/spring-messagesource-xliff/1.2.1) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=alaugks_spring-xliff-translation&metric=alert_status)](https://sonarcloud.io/summary/overall?id=alaugks_spring-xliff-translation) [![Maven Central](https://img.shields.io/maven-central/v/io.github.alaugks/spring-messagesource-xliff.svg?label=Maven%20Central)](https://central.sonatype.com/artifact/io.github.alaugks/spring-messagesource-xliff/1.3.0) @@ -44,13 +43,13 @@ This package provides a **MessageSource** for using translations from XLIFF file io.github.alaugks spring-messagesource-xliff - 1.2.1 + 1.3.0 ``` **Gradle** ```text -implementation group: 'io.github.alaugks', name: 'spring-messagesource-xliff', version: '1.2.1' +implementation group: 'io.github.alaugks', name: 'spring-messagesource-xliff', version: '1.3.0' ``` @@ -71,9 +70,7 @@ The class XliffTranslationMessageSource implements the [MessageSource](https://d * Defines the default language. `setDefaultDomain(String defaultDomain)` -* Defines the default domain. Default is `messages`. For more information, see [XlIFF Translations Files](#a6). - -> Please note the [CacheManager Configuration](#a4). +* Defines the default domain. Default is `messages`. For more information, see [XlIFF Translations Files](#a4). ```java import de.alaugks.spring.XliffTranslationMessageSource; @@ -87,8 +84,8 @@ import java.util.Locale; @Configuration public class MessageConfig { - public MessageSource messageSource(CacheManager cacheManager) { - XliffTranslationMessageSource messageSource = new XliffTranslationMessageSource(cacheManager); + public MessageSource messageSource() { + XliffTranslationMessageSource messageSource = new XliffTranslationMessageSource(); messageSource.setDefaultLocale(Locale.forLanguageTag("en")); messageSource.setBasenamePattern("translations/*"); return messageSource; @@ -97,75 +94,7 @@ public class MessageConfig { } ``` - -## 4. CacheManager Configuration - -You may already have an existing CacheManager configuration. If not, the following minimum CacheManager configuration is required. All [Supported Cache Providers](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#io.caching.provider) can also be used. Here is an [example using Caffeine](https://github.com/alaugks/spring-messagesource-xliff-example/blob/main/src/main/java/io/github/alaugks/config/CacheConfig.java). - - - -The CacheName must be set with the constant `CatalogCache.CACHE_NAME`. The specific cache identifier is stored in the constant. - -[ConcurrentMapCacheManager](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/concurrent/ConcurrentMapCacheManager.html) is the default cache in Spring Boot and Spring. - -### CacheConfig with ConcurrentMapCacheManager - -```java -import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogCache; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.concurrent.ConcurrentMapCacheManager; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.List; - -@Configuration -@EnableCaching -public class CacheConfig { - - @Bean - public CacheManager cacheManager() { - return new ConcurrentMapCacheManager(CatalogCache.CACHE_NAME); - } - -} -``` - - -## 5. Cache warming with an ApplicationRunner (recommended) - -In the following example, the cache of translations is warmed up after the application starts. - -```java -import io.github.alaugks.spring.messagesource.xliff.XliffMessageSourcePatternResolver; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.context.MessageSource; -import org.springframework.stereotype.Component; - -@Component -public class MessageSourceCacheWarmUp implements ApplicationRunner { - - private final MessageSource messageSource; - - public AppStartupRunner(MessageSource messageSource) { - this.messageSource = messageSource; - } - - @Override - public void run(ApplicationArguments args) { - if (this.messageSource instanceof XliffTranslationMessageSource) { - ((XliffTranslationMessageSource) this.messageSource).initCache(); - } - } - -} -``` - - - ## 6. XLIFF Translation Files * Translations can be separated into different files (domains). The default domain is `messages`. @@ -411,15 +340,15 @@ Mixing XLIFF versions is possible. Here is an example using XLIFF 1.2 and XLIFF > ***There is no translation for Japanese (`jp`). The default locale translations (`en`) are selected. - + ## 7. Using the MessageSource -With the implementation and use of the MessageSource interface, the translations are also available in [Thymeleaf](#a7.1), as [Service (Dependency Injection)](#a7.2) and [Custom Validation Messages](#a7.3). Also in packages and implementations that use the MessageSource. +With the implementation and use of the MessageSource interface, the translations are also available in [Thymeleaf](#a5.1), as [Service (Dependency Injection)](#a5.2) and [Custom Validation Messages](#a5.3). Also in packages and implementations that use the MessageSource. - + ### Thymeleaf -With the configured MessageSource, the translations are available in Thymeleaf. See the example in the [Full Example](#a8). +With the configured MessageSource, the translations are available in Thymeleaf. See the example in the [Full Example](#a6). ```html @@ -450,10 +379,10 @@ With the configured MessageSource, the translations are available in Thymeleaf. ``` - + ### Service (Dependency Injection) -The MessageSource can be set via Autowire to access the translations. See the example in the [Full Example](#a8). +The MessageSource can be set via Autowire to access the translations. See the example in the [Full Example](#a6). ```java import org.springframework.context.MessageSource; @@ -496,14 +425,14 @@ this.messageSource.getMessage("payment.headline", null, locale); this.messageSource.getMessage("payment.expiry-date", null, locale); ``` - + ### Custom Validation Messages The article [Custom Validation MessageSource in Spring Boot](https://www.baeldung.com/spring-custom-validation-message-source) describes how to use custom validation messages. - + ## 8. Full Example A Full Example using Spring Boot, mixing XLIFF 1.2 and XLIFF 2.1 translation files: @@ -512,13 +441,13 @@ Repository: https://github.com/alaugks/spring-messagesource-xliff-example
Website: https://spring-boot-xliff-example.alaugks.dev - + ## 9. Support If you have questions, comments or feature requests please use the [Discussions](https://github.com/alaugks/spring-xliff-translation/discussions) section. - + ## 10. More Information ### MessageSource, Internationalization and Thymeleaf diff --git a/pom.xml b/pom.xml index 64109b4..fdf1045 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ io.github.alaugks spring-messagesource-xliff - 1.2.1 + 1.3.0 jar ${project.groupId}:${project.artifactId} @@ -35,7 +35,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.2 + 3.2.7 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 9a96aba..032520c 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 @@ -1,14 +1,17 @@ package io.github.alaugks.spring.messagesource.xliff; import io.github.alaugks.spring.messagesource.xliff.catalog.Catalog; +import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogCache; import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogWrapper; import io.github.alaugks.spring.messagesource.xliff.catalog.xliff.XliffCatalogBuilder; +import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceRuntimeException; import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoader; import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoaderInterface; import java.text.MessageFormat; import java.util.List; import java.util.Locale; import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceResolvable; import org.springframework.context.NoSuchMessageException; @@ -22,6 +25,14 @@ public class XliffTranslationMessageSource implements MessageSource { private final ResourcesLoaderInterface resourcesLoader = new ResourcesLoader(); private final XliffCatalogBuilder xliffCatalogBuilder = new XliffCatalogBuilder(); + /** + * @deprecated + */ + @Deprecated(since = "2.0.0") + public XliffTranslationMessageSource() { + this(new ConcurrentMapCacheManager(CatalogCache.CACHE_NAME)); + } + /** * @deprecated */ @@ -135,7 +146,10 @@ private CatalogWrapper.Translation findInCatalog(Locale locale, String code) { } public void initCache() { - this.catalogWrapper.initCache(); + if (this.resourcesLoader.getDefaultLocale() == null || this.resourcesLoader.getDefaultLocale().toString().isEmpty()) { + throw new XliffMessageSourceRuntimeException("Default language is not set or empty."); + } + //this.catalogWrapper.initCache(); } private String format(@Nullable String message, @Nullable Object[] args, Locale locale) { diff --git a/src/test/java/io/github/alaugks/spring/messagesource/xliff/XliffMatchingResourcePatternResolverInternalCacheTest.java b/src/test/java/io/github/alaugks/spring/messagesource/xliff/XliffMatchingResourcePatternResolverInternalCacheTest.java new file mode 100644 index 0000000..0b5574e --- /dev/null +++ b/src/test/java/io/github/alaugks/spring/messagesource/xliff/XliffMatchingResourcePatternResolverInternalCacheTest.java @@ -0,0 +1,15 @@ +package io.github.alaugks.spring.messagesource.xliff; + +import java.util.Locale; +import org.junit.jupiter.api.BeforeAll; + +@SuppressWarnings({"java:S2187"}) +class XliffMatchingResourcePatternResolverInternalCacheTest extends XliffMatchingResourcePatternResolverAbstract { + + @BeforeAll + static void beforeAll() { + messageSource = new XliffTranslationMessageSource(); + messageSource.setBasenamePattern("translations/*"); + messageSource.setDefaultLocale(Locale.forLanguageTag("en")); + } +}