Вы находитесь на странице: 1из 10

Internationalization

(I18N)
Definition

Internationalization is the process of


designing an application so that it can be
adopted to various languages and regions
without engineering changes
Characteristics of
Internationalization
Support for new language does not
require recompilation
No hardcoding in source code is
necessary for separate language because
each languages are stored in separate file
outside the source code
Steps for Internationalization
process
1. Create the properties file
2. Define the locale
3. Create a resource bundle
4. Fetch the text from the resource bundle
5. Execute by passing language as a
parameter in argument
Sample program
import java.util.*;
public class i18nsample
{
public static void main(String args[])
{
String language;
String country;
If (args.length !=2)
{
language=new String(en);
country =new String(US);
}
else
{
language=new String(args[0]);
country=new String(args[1]);
}
Locale currentLocale;
ResourceBundle messages;
currentLocale =new Locale(language,country);
messages=ResourceBundle.getBundle(MessageBundle,currentLocale);
System.out.println(messages.getString(greetings);
System.out.println(messages.getString(inquiry);
System.out.println(messages.getString(farewell);
}
}
1. Create the properties file
A properties file stores information about the characteristics of a program or environment. A
properties file is in plain text format. Create in text editor
In the sample program the properties file stores the translatable of the messages to be displayed
The default properties file which is called MessageBundle.properties consist
greetings=hello
farewell=good bye
inquiry=how are you
Now that the messages are in a properties file, they can be translated into various languages
The French translator created a properties file called MessageBundle_fr_FR.properties which
consist of
greetings=bonjour
farewell=au revoir
inquiry=comment allez-vous
Similarly each translator can create their own properties file.
2.Define the locale
Locale object identifies a particular language and country
For English language and US country
aLocale= new Locale(en,US);
For French languae and France Country
fLocale=new Locale(fr,FR);
Instead of using hardcoded language and country codes the sample
program gets them from the command line at runtime
String language=new String(args[0]);
String country=new String(args[1]);
currentLocale=new Locale(language,country)
3. Create a ResourceBundle
Resource Bundle objects contain locale-specific objects.
It is used to isolate locale-sensitive data such as translatable text.
In the sample program Resource Bundle is packed by the properties file that contain
the message text to be displayed
It is created as follows
message=ResourceBundle.getBundle(MessageBundle,currentLocale);

the arguments passed to the getBundle method identify the properties file to
be accessed.
First argument(MessageBundle) refers to properties file:
MessageBundle.properties
MessageBundle_fr_FR.properties
Second argument currentLocale specifies which of the messagebundle file is chosen.
4. Fetch the text from Resource Bundle
The properties file contain key-value pairs.
Value consists of the translated text
Key for fetching the translated text from the resource bundle with getString method
string msg1=messages.getString(greetings);
greetings is the key which consist of translated text and will be fetched using
getString method (key must be present in properties file)
5. Execute by passing language & country
as a parameter in argument
After compiling while executing pass the language and country as parameter in the
command line argument
ie after javac i18n.java

java i18n fr FR
this will execute french properties file and output will be displayed in french language

Вам также может понравиться