- JAVA Internalization Tutorial
- JAVA I18N - Home
- JAVA I18N - Overview
- JAVA I18N - Environment Setup
- Locale Class Examples
- JAVA I18N - Locale Class
- JAVA I18N - Locale Details
- JAVA I18N - Display Language
- ResourceBundle Class Examples
- JAVA I18N - ResourceBundle Class
- NumberFormat Class Examples
- JAVA I18N - NumberFormat Class
- JAVA I18N - Format Currencies
- JAVA I18N - Format Percentages
- JAVA I18N - Set Min/Max Precision
- JAVA I18N - Set Rounding Mode
- JAVA I18N - Parsing Numbers
- DecimalFormat Class Examples
- JAVA I18N - DecimalFormat Class
- JAVA I18N - Formatting Patterns
- JAVA I18N - Locale Specific DecimalFormat
- JAVA I18N - DecimalFormatSymbols Class
- JAVA I18N - Grouping Digits
- DateFormat Class Examples
- JAVA I18N - DateFormat Class
- JAVA I18N - Formatting Dates
- JAVA I18N - Formatting Time
- JAVA I18N - Formatting Date and Time
- SimpleDateFormat Class Examples
- JAVA I18N - SimpleDateFormat Class
- JAVA I18N - Formatting Date
- JAVA I18N - DateFormatSymbols Class
- JAVA I18N - Date Format Patterns
- Time Zones Examples
- JAVA I18N - UTC
- Unicode Conversion
- JAVA I18N - From and To String Conversion
- JAVA I18N - From Reader and To Writer Conversion
- JAVA Internalization Useful Resources
- Java I18N - Quick Guide
- Java I18N - Useful Resources
- Java I18N - Discussion
Java Internalization - ResourceBundle Class
ResourceBundle class is used to store text and objects which are locale sensitive. Generally we use property files to store locale specific text and then represent them using ResourceBundle object. Following are the steps to use locale specific properties file in a java based application.
Step 1: Create properties files.
Suppose we need properties file for English locale. Then create a properties file name XXX_en_US.properties where XXX is the name of the file and en_US represents the locale for English(US).
Messages_en_US.properties
message=Welcome to TutorialsPoint.COM!
Let's now create properties file for French locale. Then create a properties file name XXX_fr_FR.properties where XXX is the name of the file and fr_FR represents the locale for French(France).
Messages_fr_FR.properties
message=Bienvenue sur TutorialsPoint.COM!
Here you can figure out that the key is same but the value is locale specific in both the properties file.
Step 2: Create ResourceBundle object
Create ResourceBundle object with properties file name and locale using following syntax.
ResourceBundle bundle = ResourceBundle.getBundle("Messages", Locale.US);
Step 3: Get the value from ResourceBundle object.
Get the value from ResourceBundle object by passing the key.
String value = bundle.getString("message");
Example
Following example illustrate the use of ResourceBundle objects to display locale specific values from properties files.
IOTester.java
import java.util.Locale; import java.util.ResourceBundle; public class I18NTester { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("Messages", Locale.US); System.out.println("Message in "+Locale.US +": "+bundle.getString("message")); bundle = ResourceBundle.getBundle("Messages", Locale.FRANCE); System.out.println("Message in "+Locale.FRANCE +": "+bundle.getString("message")); } }
Output
It will print the following result.
Message in en_US: Welcome to TutorialsPoint.COM! Message in fr_FR: Bienvenue sur TutorialsPoint.COM!
Notes for Naming Conventions
Following are the naming conventions for the properties file.
For properties file mapped to default locale, no prefix is mandatory. message_en_US.properties is equivalent to message.properties.
For properties file mapped to locale, prefix can be attached in two ways. message_fr.properties is equivalent to message_fr_FR.properties.