Resource bundles are used to support internationalization (i18n) and externalize configuration parameters from code. They work by storing configurable key-value pairs in properties files that can be read at runtime. The document discusses how to create properties files, read values from them using the ResourceBundle class, and support multiple languages by creating localized properties files for different locales. It also explains the Locale class used to retrieve the correct localized properties file based on language, country, and variant settings.
2. www.SunilOS.com 2
Introduction
It is used to support multi-language in an
Application that is called internationalization
(i18n).
It is used to remove hard coding from an
application by making configuration files and
read from resource bundle.
It moves all configurable parameters into a
text file in form of key=value pairs.
4. www.SunilOS.com 4
app.properties – create file
Create a text file having extension .properties.
Add key=value pairs of configurable data. Entries
will look like:
o #Database connection Parameters
o url=jdbc:mysql://localhost:3306/ocha
o driver=com.mysql.jdbc.Driver
o username=root
o password=pwd
5. Read from ResourceBundle
Assuming that text file app.properties file is kept in
in.co.sunrays.rb package.
public static void main(String[] args) {
ResourceBundle rb =
ResourceBundle.getBundle("in.co.sunrays.rb.app");
//Pass key and get value
String url = rb.getString("url");
String driver = rb.getString("driver");
String user = rb.getString("username");
String password = rb.getString("password");
}
www.SunilOS.com 5
6. Multilanguage Support
Can be achieved by setting Locale to resource
bundle.
For each locale you have to create a separate
.properties file.
Suppose you have to support three languages
English ( default ), Hindi and Spanish then you will
create:
o app.properties (default is English)
o app_hi.properties ( for Hindi )
o app_sp.properties ( for Spanish )
www.SunilOS.com 6
7. Create Property Files
app.properties
o greeting=Hello, how are you?
app_hi.properties
o greeting=हैलो, कै से हो?
app_sp.properties
o greeting=Hola, cómo estás?
www.SunilOS.com 7
8. www.SunilOS.com 8
Run and see the output
//Default Locale
ResourceBundle rb = ResourceBundle.getBundle("in.co.sunrays.rb.app");
System.out.println(rb.getString("greeting"));
//Set Locale to Spanish
rb = ResourceBundle.getBundle("in.co.sunrays.rb.app", new Locale("sp"));
System.out.println(rb.getString("greeting"));
//Set Locale to Hindi
rb = ResourceBundle.getBundle("in.co.sunrays.rb.app", new Locale("hi"));
System.out.println(rb.getString("greeting"));
Output
o Hello, how are you?
Hola, cómo estás?
हैलो, कै से हो?
10. What is Locale?
A Locale object represents a specific geographical,
political, or cultural region.
Locale has three constructors that accepts:
o Language : uses two character code to represent a
language
i.e. en : English, sp: Spanish, hi : Hindi
o Country : accepts valid international country code
i.e. US: USA, IN : India, UK : United Kingdom.
o Variant: any arbitrary value used to indicate a variation
of a Locale, say state name MP, UP, HP, NY, TX, CT
etc.
www.SunilOS.com 10
11. Locale Constructors
Constructors:
o Locale(String language)
o Locale(String language, String country)
o Locale(String language, String country, String variant)
Locale locale = new Locale(“hi", “IN");
o It searches key in app_hi_IN.properties file.
Locale locale = new Locale(“hi", “IN“,”UP”);
o It searches key in app_hi_IN_UP.properties file.
www.SunilOS.com 11
14. Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 14