This package provides a MessageSource for using translations from JSON files.
<dependency>
<groupId>io.github.alaugks</groupId>
<artifactId>spring-messagesource-json</artifactId>
<version>0.1.0</version>
</dependency>implementation group: 'io.github.alaugks', name: 'spring-messagesource-json', version: '0.1.0'
builder(Locale defaultLocale, LocationPattern locationPattern) (required)
- Argument
Locale defaultLocale: Defines the default locale. - Argument
LocationPattern locationPattern:- Defines the pattern used to select the JSON files.
- The package uses the PathMatchingResourcePatternResolver to select the JSON files. So you can use the supported patterns.
- Files with the extension
jsonare filtered from the result list.
defaultDomain(String defaultDomain)
- Defines the default domain. Default is
messages. For more information, see JSON Files.
- Default locale is
en. - The JSON files are stored in
src/main/resources/translations.
import io.github.alaugks.spring.messagesource.json.JsonResourceMessageSource;
import io.github.alaugks.spring.messagesource.catalog.resources.LocationPattern;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Locale;
@Configuration
public class MessageSourceConfig {
@Bean
public MessageSource messageSource() {
return JsonResourceMessageSource
.builder(
Locale.forLanguageTag("en"),
new LocationPattern("translations/*")
)
.build();
}
}- Translations can be separated into different files (domains). The default domain is
messages. - The default domain can be defined.
- Translation files must be stored in the resource folder and have the extension
json.
# Default language
<domain>.json // <domain>_<language>.json also works.
# Domain + Language
<domain>[-_]<language>.json
# Domain + Language + Region
<domain>[-_]<language>[-_]<region>.json
-
Default domain is
messages. -
Default locale is
enwithout region. -
Translations are provided for the locale
en,deanden-US.
[resources]
|-[translations]
|-messages.json // Default domain and default language. messages_en.json also works.
|-messages_de.json
|-messages_en-US.json
|-payment.json // Default language. payment_en.json also works.
|-payment_de.json
|-payment_en-US.json
Mixing JSON versions is possible. Here is an example using JSON 1.2 and JSON 2.1.
{
"headline": "Headline",
"postcode": "Postcode"
}{
"headline": "Überschrift",
"postcode": "Postleitzahl"
}{
"postcode": "Zip code"
}{
"headline": "Payment",
"expiry_date": "Expire date"
}{
"headline": "Zahlung",
"expiry_date": "Ablaufdatum"
}{
"expiry_date": "Expiration date"
}The behaviour of resolving the target value based on the code is equivalent to the ResourceBundleMessageSource or ReloadableResourceBundleMessageSource.
| id (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.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). Theiddoes not exist inen-US, so it tries to select the translation with localeen.***There is no translation for Japanese (
jp). The default locale translations (en) are selected.
https://github.com/alaugks/spring-messagesource-json-example