Skip to content

alaugks/spring-messagesource-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON MessageSource for Spring

This package provides a MessageSource for using translations from JSON files.

Quality Gate Status Maven Central

Dependency

Maven

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

Gradle

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

MessageSource Configuration

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 json are filtered from the result list.

defaultDomain(String defaultDomain)

  • Defines the default domain. Default is messages. For more information, see JSON Files.

Example

  • 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();
    }

}

JSON Files

  • 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.

Structure of the Translation Filename

# Default language
<domain>.json    // <domain>_<language>.json also works.

# Domain + Language
<domain>[-_]<language>.json

# Domain + Language + Region
<domain>[-_]<language>[-_]<region>.json

Example with JSON Files

  • Default domain is messages.

  • Default locale is en without region.

  • Translations are provided for the locale en, de and en-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     

JSON Files

Mixing JSON versions is possible. Here is an example using JSON 1.2 and JSON 2.1.

messages.json
{
  "headline": "Headline",
  "postcode": "Postcode"
}
messages_de.json
{
  "headline": "Überschrift",
  "postcode": "Postleitzahl"
}
messages_en-US.json
{
  "postcode": "Zip code"
}
payment.json
{
  "headline": "Payment",
  "expiry_date": "Expire date"
}
payment_de.json
{
  "headline": "Zahlung",
  "expiry_date": "Ablaufdatum"
}
payment_en-US.json
{
  "expiry_date": "Expiration date"
}

Target value

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). 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 translations (en) are selected.

Full Example

https://github.com/alaugks/spring-messagesource-json-example

Related MessageSources and Examples

About

This package provides a MessageSource for using translations from JSON files.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages