Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
www.SunilOS.com 1
www.sunilos.com
www.raystec.com
Resource Bundle
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.
Class Hierarchy
www.SunilOS.com
3
Resource Bundle
PropertyResourceBundle ListResourceBundle
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
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
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
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
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?
 हैलो, कै से हो?
Multi-Language Support
www.SunilOS.com 9
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
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
Say NO to
Hard Coding
www.SunilOS.com 12
Say YES to
Resource Bundle
www.SunilOS.com 13
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
Thank You!
www.SunilOS.com 15
www.SunilOS.com

More Related Content

Resource Bundle

  • 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
  • 12. Say NO to Hard Coding www.SunilOS.com 12
  • 13. Say YES to Resource Bundle www.SunilOS.com 13
  • 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