Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alaugks/spring-messagesource-catalog
This package extends the AbstractMessageSource and therefore the MessageSource interface.
https://github.com/alaugks/spring-messagesource-catalog
internationalization messagesource spring spring-boot springboot
Last synced: 5 days ago
JSON representation
This package extends the AbstractMessageSource and therefore the MessageSource interface.
- Host: GitHub
- URL: https://github.com/alaugks/spring-messagesource-catalog
- Owner: alaugks
- Created: 2024-05-10T14:51:26.000Z (6 months ago)
- Default Branch: 0.4
- Last Pushed: 2024-10-20T18:21:31.000Z (28 days ago)
- Last Synced: 2024-10-20T22:43:18.053Z (28 days ago)
- Topics: internationalization, messagesource, spring, spring-boot, springboot
- Language: Java
- Homepage:
- Size: 31.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Package to create a custom Spring MessageSource
This package extends the AbstractMessageSource and therefore the MessageSource interface.
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=alaugks_spring-messagesource-base&metric=alert_status&token=3d2b79af1f0f0ab6089e565495b4db6f621e9a13)](https://sonarcloud.io/summary/overall?id=alaugks_spring-messagesource-base)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.alaugks/spring-messagesource-catalog.svg?label=Maven%20Central)](https://central.sonatype.com/artifact/io.github.alaugks/spring-messagesource-catalog/0.4.0)## Dependency
> [!IMPORTANT]
> There may still be a breaking change in the [CatalogInterface.build()](src/main/java/io/github/alaugks/spring/messagesource/catalog/catalog/CatalogInterface.java) up to version 1.0.0.### Maven
```xml
io.github.alaugks
spring-messagesource-catalog
0.4.0
```
### Gradle
```
implementation group: 'io.github.alaugks', name: 'spring-messagesource-catalog', version: '0.4.0'
```## Packages that use the catalog as a base package
* [spring-messagesource-xliff](https://github.com/alaugks/spring-messagesource-xliff): MessageSource for translations from XLIFF files.
* [spring-messagesource-db-example](https://github.com/alaugks/spring-messagesource-db-example): Example custom Spring MessageSource from database
* [spring-messagesource-json-example](https://github.com/alaugks/spring-messagesource-json-example): Example custom Spring MessageSource from JSON## CatalogMessageSource Configuration
### Options
`builder(CatalogInterface catalogSource, Locale defaultLocale)` (required)
* Argument `CatalogInterface catalogSource`: CatalogInterface
* Argument `Locale defaultLocale`: Default Locale`defaultDomain(String defaultDomain)`
* If the default domain not set, the default is **messages**.### TransUnit Record
If the `String domain` argument is not set, the default is the **messages** domain.
```java
TransUnit(Locale locale, String code, String value);TransUnit(Locale locale, String code, String value, String domain);
```### Configuration example
#### MessageConfig with List of TransUnits
```java
import io.github.alaugks.spring.messagesource.catalog.catalog.CatalogHandler;
import io.github.alaugks.spring.messagesource.catalog.records.TransUnitInterface;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MessageConfig {
List transUnits = new ArrayList<>() {{
// en
add(new TransUnit(Locale.forLanguageTag("en"), "headline", "Headline"));
add(new TransUnit(Locale.forLanguageTag("en"), "postcode", "Postcode"));
add(new TransUnit(Locale.forLanguageTag("en"), "headline", "Payment", "payment"));
add(new TransUnit(Locale.forLanguageTag("en"), "expiry_date", "Expiry date", "payment"));// en-US
add(new TransUnit(Locale.forLanguageTag("en-US"), "postcode", "Zip code"));
add(new TransUnit(Locale.forLanguageTag("en-US"), "expiry_date", "Expiration date", "payment"));// de
add(new TransUnit(Locale.forLanguageTag("de"), "headline", "Überschrift"));
add(new TransUnit(Locale.forLanguageTag("de"), "postcode", "Postleitzahl"));
add(new TransUnit(Locale.forLanguageTag("de"), "headline", "Zahlung", "payment"));
add(new TransUnit(Locale.forLanguageTag("de"), "expiry_date", "Ablaufdatum", "payment"));
}};@Bean
public MessageSource messageSource() {
return CatalogMessageSourceBuilder
.builder(this.transUnits, Locale.forLanguageTag("en"))
.build();
}
}
```#### With custom CatalogInterface
##### CatalogInterface
```java
import java.util.List;import io.github.alaugks.spring.messagesource.catalog.catalog.AbstractCatalog;
import io.github.alaugks.spring.messagesource.catalog.catalog.Abstractcatalog;
import io.github.alaugks.spring.messagesource.catalog.records.TransUnitInterface;public class MyCustomCatalog extends AbstractCatalog {
List transUnits;
@Override
public List getTransUnits() {
return this.transUnits;
}@Override
public void build() {
// Build a list with TransUnit from any kind of source.
this.transUnits = new ArrayList<>() {{
// ...
}};
}
}
```##### MessageConfig
```java
import io.github.alaugks.spring.messagesource.catalog.catalog.CatalogHandler;
import io.github.alaugks.spring.messagesource.catalog.records.TransUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MessageConfig {
@Bean
public MessageSource messageSource() {
return CatalogMessageSourceBuilder
.builder(new MyCustomCatalog(), Locale.forLanguageTag("en"))
.build();
}
}
```### Target values
The behaviour of resolving the target value based on the code is equivalent to the ResourceBundleMessageSource or ReloadableResourceBundleMessageSource.
code
en
en-US
de
jp***
headline*
messages.headline
Headline
Headline**
Überschrift
Headline
postcode*
messages.postcode
Postcode
Zip code
Postleitzahl
Postcode
payment.headline
Payment
Payment**
Zahlung
Payment
payment.form.expiry_date
Expiry date
Expiration date
Ablaufdatum
Expiry date
> *Default domain is `messages`.
>
> **Example of a fallback from Language_Region (`en-US`) to Language (`en`). The `id` does not exist in `en-US`, so it tries to select the translation with locale `en`.
>
> ***There is no translation for Japanese (`jp`). The default locale transUnits (`en`) are selected.## Support
If you have questions, comments or feature requests please use the [Discussions](https://github.com/alaugks/spring-messagesource-catalog/discussions) section.