This package extends the AbstractMessageSource and therefore the MessageSource interface.
Important
There may still be a breaking change in the CatalogInterface.build() up to version 1.0.0.
<dependencies>
<dependency>
<groupId>io.github.alaugks</groupId>
<artifactId>spring-messagesource-catalog</artifactId>
<version>0.4.0</version>
</dependency>
</dependencies>
implementation group: 'io.github.alaugks', name: 'spring-messagesource-catalog', version: '0.4.0'
- spring-messagesource-xliff: MessageSource for translations from XLIFF files.
- spring-messagesource-db-example: Example custom Spring MessageSource from database
- spring-messagesource-json-example: Example custom Spring MessageSource from JSON
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.
If the String domain
argument is not set, the default is the messages domain.
TransUnit(Locale locale, String code, String value);
TransUnit(Locale locale, String code, String value, String domain);
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<TransUnitInterface> 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();
}
}
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<TransUnitInterface> transUnits;
@Override
public List<TransUnitInterface> getTransUnits() {
return this.transUnits;
}
@Override
public void build() {
// Build a list with TransUnit from any kind of source.
this.transUnits = new ArrayList<>() {{
// ...
}};
}
}
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();
}
}
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
). Theid
does not exist inen-US
, so it tries to select the translation with localeen
.***There is no translation for Japanese (
jp
). The default locale transUnits (en
) are selected.
If you have questions, comments or feature requests please use the Discussions section.