Giter Site home page Giter Site logo

pinterest / l10nmessages Goto Github PK

View Code? Open in Web Editor NEW
17.0 6.0 5.0 3.06 MB

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

Home Page: https://l10nmessages.io

License: Apache License 2.0

Shell 1.45% Java 98.55%
i18n java l10n annotation-processor bazel fluent-api gradle icu4j maven internationalization

l10nmessages's Introduction

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!
  }
}

l10nmessages's People

Contributors

aurambaj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.