- Notifications
You must be signed in to change notification settings - Fork6
L10nMessages is a library that makes internationalization (i18n) and localization (l10n) of Java applications easy and safe.
License
pinterest/l10nmessages
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
L10nMessages
is a library that makes internationalization (i18n) and localization (l10n) of Javaapplications easy and safe.
It provides afluent API to load andformat messages that is built on top of Java standard libraries. In addition to simplifying theoverall setup, it brings strong typing and compile time checks to the mix when used with theannotation processor.
The full documentation ishere.
Seegeneral instructions andguides forBazel,Gradle andMaven.
Also available in thefull documentation (with filepaths).
Create a root file:Messages.properties
in thecom.pinterest.l10nmessages.example
package.
The corresponding resource bundlebaseName
iscom.pinterest.l10nmessages.example.Messages
.
UTF-8
is the recommended encoding for theproperties
files.
In a project that follows the Maven layout, the file would be:
welcome_user=Welcome {username}!
Add the@L10nProperties
annotation to the application class to register the resource bundle withthe annotation processor.
importcom.pinterest.l10nmessages.L10nProperties;@L10nProperties(baseName ="com.pinterest.l10nmessages.example.Messages")publicclassApplication {}
Compile your project. The annotation processor should generate the followingenum
:
packagecom.pinterest.l10nmessages.example;publicenumMessages {welcome_user("welcome_user");publicstaticfinalStringBASENAME ="com.pinterest.l10nmessages.example.Messages";// ...}
Thatenum
can be used to create theL10nMessages
instance and then to format a message using thetyped key:Messages.welcome_user
. The argument:username
is provided as a key/value pair.
importcom.pinterest.l10nmessages.L10nMessages;importcom.pinterest.l10nmessages.L10nProperties;@L10nProperties(baseName ="com.pinterest.l10nmessages.example.Messages")publicclassApplication {publicstaticvoidmain(String[]args) {L10nMessages<Messages>m =L10nMessages.builder(Messages.class).build();StringlocalizedMsg =m.format(Messages.welcome_user,"username","Mary");System.out.println(localizedMsg);// Welcome Mary! }}
For extra typing, considerargument names typing.
Localize the root properties file by creating the fileMessages_fr.properties
for "French"
welcome_user=Bienvenue {username}!
Specify the locale wanted for the messages
@L10nProperties(baseName ="com.pinterest.l10nmessages.example.Messages")publicclassApplication {publicstaticvoidmain(String[]args) {L10nMessages<Messages>m =L10nMessages.builder(Messages.class) .locale(Locale.FRENCH) .build();StringlocalizedMsg =m.format(Messages.welcome_user,"username","Mary");System.out.prinln(localizedMsg);// Bienvenue Mary! }}
About
L10nMessages is a library that makes internationalization (i18n) and localization (l10n) of Java applications easy and safe.