Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.transferwise:spring-icu:0.3.0'
}
dependencies {
implementation("com.github.transferwise:spring-icu:0.3.0")
}
<dependency>
<groupId>com.github.transferwise</groupId>
<artifactId>spring-icu</artifactId>
<version>0.3.0</version>
</dependency>
libraryDependencies += "com.github.transferwise" % "spring-icu" % "0.3.0"
:dependencies [[com.github.transferwise/spring-icu "0.3.0"]]
Provides the ICU4J message formatting features, such as named arguments support, flexible plural formatting, rule based number format, date interval formats.
By default Spring allows you to use only numbered arguments in i18n messages. ICU4j support named arguments using Map, which are sometimes are more readable. For example:
numbered={0}, you have {1} unread messages of {2}
names={username}, you have {unread} unread messages of {total}
With ICU4J, you can do this:
Map<String, Object> args = new HashMap<>();
args.put("username", "John");
args.put("unread", 12);
args.put("total", 200);
System.out.println(messageSource.getMessage("names", args, locale));
will output John, you have 12 unread messages of 200
.
Pluralisation in English is pretty simple and can be implemented using embedded ChoiceFormat
. However, many other
languages have more complex pluralisation rules described here,
which cannot be handled by default. The plugin provides a simple pluralization using a language's rules, e.g. for Polish:
plural={0} {0, plural, one {auto} few {auta} many {aut} other{aut}}
System.out.println(messageSource.getMessage("plural", new Object[]{3}, locale));
System.out.println(messageSource.getMessage("plural", new Object[]{7}, locale));
will output 3 auta
, 7 aut
.
amount={0, spellout} dollars
System.out.printlin(messageSource.getMessage("amount", new Objcet[]{12045}, locale));
will output twelve thousand forty-five dollars
.
Define the bean.
@Bean
public MessageSource messageSource() {
ICUReloadableResourceBundleMessageSource messageSource = new ICUReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
return messageSource;
}
Place your message.properties files under src/main/resources
. See this projects test cases for some examples.
While com.transferwise.icu.ICUMessageSource
defines methods that take a named argument map, org.springframework.context.MessageSource
does not. Therefore, in order to support ICU in code that expects a org.springframework.context.MessageSource
message source, such as Thymeleaf's #messages, we check if the first argument of the Object[] args
parameter passed to org.springframework.context.MessageSource#getMessage
is a Map and if so cast that argument to a Map<String, Object>
and call com.transferwise.icu.ICUMessageSource#getMessage
See com.transferwise.icu.ICUAbstractMessageSource.isNamedArgumentsMapPresent
This library is available under the Apache License, Version 2.0.
(c) All rights reserved