Skip to content

L10nMessages is a library that makes internationalization (i18n) and localization (l10n) of Java applications easy and safe.

License

Notifications You must be signed in to change notification settings

pinterest/l10nmessages

L10nMessages

Build License Maven Central javadoc Installation

L10nMessages is a library that makes internationalization (i18n) and localization (l10n) of Java applications easy and safe.

It provides a 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.

Full Documentation

The full documentation is here.

Installation

See general instructions and guides for Bazel, Gradle and Maven.

Getting Started

Also available in the full documentation (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:

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.

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:

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.

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

Localization

Localize the root properties file by creating the file Messages_fr.properties for "French"

welcome_user=Bienvenue {username}!

Specify the locale wanted for the messages

@L10nProperties(baseName = "com.pinterest.l10nmessages.example.Messages")
public class Application {

  public static void main(String[] args) {
    L10nMessages<Messages> m = L10nMessages.builder(Messages.class)
        .locale(Locale.FRENCH)
        .build();
    String localizedMsg = m.format(Messages.welcome_user, "username", "Mary");
    System.out.prinln(localizedMsg);
    // Bienvenue Mary!
  }
}