Skip to content

This package extends the AbstractMessageSource and therefore the MessageSource interface.

Notifications You must be signed in to change notification settings

alaugks/spring-messagesource-catalog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Package 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.4.0</version>
    </dependency>
</dependencies>

Gradle

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

Packages that use the catalog as a base package

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.

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

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

Configuration example

MessageConfig with List of TransUnits

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"), "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
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.TransUnit;

public class MyCustomCatalog extends AbstractCatalog {

	List<TransUnit> transUnits;

	@Override
	public List<TransUnit> getTransUnits() {
		return this.transUnits;
	}

	@Override
	public void build() {
		// Build a list with TransUnit from any kind of source.
		this.transUnits = new ArrayList<>() {{
			// ...
		}};
	}
}
MessageConfig
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 section.

About

This package extends the AbstractMessageSource and therefore the MessageSource interface.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages