https://github.com/pinterest/l10nmessages
L10nMessages is a library that makes internationalization (i18n) and localization (l10n) of Java applications easy and safe.
https://github.com/pinterest/l10nmessages
annotation-processor bazel fluent-api format gradle i18n icu4j internationalization java l10n localization maven
Last synced: 3 months ago
JSON representation
L10nMessages is a library that makes internationalization (i18n) and localization (l10n) of Java applications easy and safe.
- Host: GitHub
- URL: https://github.com/pinterest/l10nmessages
- Owner: pinterest
- License: apache-2.0
- Created: 2022-06-28T22:55:57.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-28T07:08:34.000Z (over 1 year ago)
- Last Synced: 2025-06-09T15:11:35.788Z (4 months ago)
- Topics: annotation-processor, bazel, fluent-api, format, gradle, i18n, icu4j, internationalization, java, l10n, localization, maven
- Language: Java
- Homepage: https://l10nmessages.io
- Size: 2.99 MB
- Stars: 18
- Watchers: 6
- Forks: 6
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# L10nMessages
[](https://github.com/pinterest/l10nmessages/actions/workflows/build.yml)
[](http://www.apache.org/licenses/LICENSE-2.0.txt)
[](https://search.maven.org/search?q=g:%22com.pinterest.l10nmessages%22%20AND%20a:%22l10nmessages%22)
[](https://javadoc.io/doc/com.pinterest.l10nmessages/l10nmessages)
[](https://l10nmessages.io/docs/installation)`L10nMessages` is a library that makes internationalization (i18n) and localization (l10n) of Java
applications easy and safe.It provides a [fluent API](https://l10nmessages.io/docs/fluent-api) to load and
format messages that is built on top of Java standard libraries. In addition to simplifying the
overall setup, it brings strong typing and compile time checks to the mix when used with the
[annotation processor](https://l10nmessages.io/docs/annotation-processor).## Full Documentation
The full documentation is [here](https://l10nmessages.io/).
## Installation
See [general instructions](https://l10nmessages.io/docs/installation) and
guides for [Bazel](https://l10nmessages.io/docs/installation/bazel),
[Gradle](https://l10nmessages.io/docs/installation/gradle) and
[Maven](https://l10nmessages.io/docs/installation/maven).## Getting Started
Also available in the
[full documentation](https://l10nmessages.io/docs/getting-started/) (with file
paths).### Create a resource bundle
Create a root file: `Messages.properties` in the `com.pinterest.l10nmessages.example` package.
The corresponding resource bundle `baseName` is `com.pinterest.l10nmessages.example.Messages`.
`UTF-8` is the recommended encoding for the `properties` files.
In a project that follows the Maven layout, the file would be:
```properties title="src/resources/java/com/pinterest/l10nmessages/example/Messages.properties"
welcome_user=Welcome {username}!
```### Register the resource bundle with `@L10nProperties`
Add the `@L10nProperties` annotation to the application class to register the resource bundle with
the annotation processor.```java title="src/main/java/com/pinterest/l10nmessages/example/Application.java"
import com.pinterest.l10nmessages.L10nProperties;@L10nProperties(baseName = "com.pinterest.l10nmessages.example.Messages")
public class Application {}
```## Enum generated by the annotation processor
Compile your project. The annotation processor should generate the following `enum`:
```java title="target/generated-sources/annotations/com/pinterest/l10nmessages/example/Messages.java"
package com.pinterest.l10nmessages.example;public enum Messages {
welcome_user("welcome_user");public static final String BASENAME = "com.pinterest.l10nmessages.example.Messages";
// ...
}
```### Strong typing using Enum
That `enum` can be used to create the `L10nMessages` instance and then to format a message using the
typed key: `Messages.welcome_user`. The argument: `username` is provided as a key/value pair.```java title="src/main/java/com/pinterest/l10nmessages/example/Application.java"
import com.pinterest.l10nmessages.L10nMessages;
import com.pinterest.l10nmessages.L10nProperties;@L10nProperties(baseName = "com.pinterest.l10nmessages.example.Messages")
public class Application {public static void main(String[] args) {
L10nMessages m = L10nMessages.builder(Messages.class).build();
String localizedMsg = m.format(Messages.welcome_user, "username", "Mary");
System.out.println(localizedMsg);
// Welcome Mary!
}
}
```For extra typing, consider [argument names typing](https://l10nmessages.io/docs/fluent-api#argument-names-typing).
### Localization
Localize the root properties file by creating the file `Messages_fr.properties` for "French"
```properties
welcome_user=Bienvenue {username}!
```Specify the locale wanted for the messages
```java
@L10nProperties(baseName = "com.pinterest.l10nmessages.example.Messages")
public class Application {public static void main(String[] args) {
L10nMessages m = L10nMessages.builder(Messages.class)
.locale(Locale.FRENCH)
.build();
String localizedMsg = m.format(Messages.welcome_user, "username", "Mary");
System.out.prinln(localizedMsg);
// Bienvenue Mary!
}
}
```