/spring-messagesource-catalog

This package extends the AbstractMessageSource and therefore the MessageSource interface.

Primary LanguageJava

Catalog to create a custom Spring MessageSource

This package extends the AbstractMessageSource and therefore the MessageSource interface.

Quality Gate Status Maven Central

Dependency

Maven

<dependencies>
    <dependency>
        <groupId>io.github.alaugks</groupId>
        <artifactId>spring-messagesource-catalog</artifactId>
        <version>0.1.0</version>
    </dependency>
</dependencies>

Gradle

implementation group: 'io.github.alaugks', name: 'spring-messagesource-catalog', version: '0.1.0'

Packages that use the catalog as a base package

CatalogMessageSource Configuration

Options

builder(List<TransUnit> transUnits, Locale defaultLocale) (required)

  • Argument List<TransUnit> transUnits: List of messages (translations)
  • 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.

TransUnit(Locale locale, String code, String value);

TransUnit(Locale locale, String code, String value, String domain);

Configuration example

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 {
    
    List<TransUnit> 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"), "validation.email.exists", "Your email {0} has been registered."));
        add(new TransUnit(Locale.forLanguageTag("en"), "default-message", "This is a default message."));
        add(new TransUnit(Locale.forLanguageTag("en"), "headline", "Payment", "payment"));
        add(new TransUnit(Locale.forLanguageTag("en"), "form.expiry_date", "Expiry date", "payment"));

        // en-US
        add(new TransUnit(Locale.forLanguageTag("en-US"), "postcode", "Zip code"));
        add(new TransUnit(Locale.forLanguageTag("en-US"), "form.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"), "validation.email.exists", "Ihre E-Mail {0} wurde registriert."));
        add(new TransUnit(Locale.forLanguageTag("de"), "default-message", "Das ist ein Standardtext."));
        add(new TransUnit(Locale.forLanguageTag("de"), "headline", "Zahlung", "payment"));
        add(new TransUnit(Locale.forLanguageTag("de"), "form.expiry_date", "Ablaufdatum", "payment"));
    }};

    @Bean
    public MessageSource messageSource() {
        return CatalogMessageSourceBuilder
            .builder(new TransUnitsCatalog(this.transUnits), 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
validation.email.exists*
messages.validation.email.exists
Your email {0} has been registered. Your email {0} has been registered.** Ihre E-Mail {0} wurde registriert. Your email {0} has been registered.
default-message*
messages.default-message
This is a default message. This is a default message.** Das ist ein Standardtext. This is a default message.
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 section.