0% found this document useful (0 votes)
14 views41 pages

Internalisation and Localisation

The document discusses internationalization and localization in Java. Internationalization is the process of designing an application to support multiple languages and regions. Java supports internationalization through features like Unicode, the Locale class, and resource bundles. The Locale class represents a specific geographical region and is used for formatting numbers, dates, and currencies appropriately for that region. Localization is the process of translating and adapting an internationalized application for a specific locale.

Uploaded by

Sahiti Darika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views41 pages

Internalisation and Localisation

The document discusses internationalization and localization in Java. Internationalization is the process of designing an application to support multiple languages and regions. Java supports internationalization through features like Unicode, the Locale class, and resource bundles. The Locale class represents a specific geographical region and is used for formatting numbers, dates, and currencies appropriately for that region. Localization is the process of translating and adapting an internationalized application for a specific locale.

Uploaded by

Sahiti Darika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

INTERNALISATIO

N AND
LOCALISATION
3
INTRODUCTION
• Internationalization is the process of
designing an application so that it can be
adapted to various languages and regions
without engineering changes.

• Java is the first language designed to support


Internationalization.

Larana University | 2024


5
CHARACTERISTICS
With the addition of localized data, the same executable can run
1 worldwide.Culturally-dependent data, such as dates and currencies, appear in
formats that conform to the end user's region and language.
textual elements, such as status messages and the GUI component labels, are
2 not hardcoded in the program. Instead they are stored outside the source code
and retrieved dynamically.
Support for new languages does not require recompilation.
3
J AVA FEATURES TO SUPPORT
Internationalization

• unicode

• locale class
• resourse bundles
Locale cLass
Locale

• A Locale object represents a geographical, political, or cultural region


in which a specific language or custom is used.
For example, Americans speak English, and the Chinese speak Chinese.

• The conventions for formatting dates, numbers, and currencies


may differ from one country to another.
For example, The Chinese use year/month/day to represent the date,
while Americans use month/day/year.
Locale
• Locale class encapsulates information about a specific locale.

• A Locale object determines how locale-sensitive information, such as


date, time, and number, is displayed.

• The classes for formatting date, time, and numbers, and for sorting
strings are grouped in the java.text package.

• Every Swing class has a locale property inherited from the Component
class.
Creating Locale Object

• Constructor:
Locale (String Language)
Locale (String Language, String Country)
Locale (String Language, String Country, String Variant)
Language and Country Codes
Country Country Code Language Code

Spain ES es

Denmark DK da

Germany DE de

Greece GR el

China CN zh

Sweden SE sv

France FR fr

Norway NO no
Methods of Locale Class

String getCountry() String


getLanguage() String
getVariant() Locale
getDefault()
String getDisplayCountry()
String getDisplayLanguage()
String getDisplayName()
String getDisplayVariant()
Locale[] getAvailableLocales()
Locale Constants

• Predefined Country Constants

Locale.US, Locale.UK, Locale.FRANCE, Locale.GERMANY,


Locale.ITALY, Locale.CHINA, Locale.KOREA, Locale.JAPAN

• Predefined Language Constants


Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH,
Locale.GERMAN, Locale.ITALIAN, Locale.JAPANESE,
Locale.KOREAN, Locale.SIMPLIFIED_CHINESE, and
Locale.TRADITIONAL_CHINESE
Displaying Time and Date
Displaying Date and Time

• Java provides a system independent encapsulation of date and


time in the java.util.Date class.

• It also provides java.util.TimeZone class for dealing with time


zones.

• java.util.Calendar can be used for extracting detailed


information from Date.
TimeZone Class

• TimeZone is an abstract class.

Constructor:
There is only default Constructor for TimeZone.
Methods of TimeZone Class

• static String[] getAvailableIDs()


used to find all the available time-zones supported in Java.

• static TimeZone getDefault()


used to obtain the default time-zone on host machine.

• static TimeZone getTimeZone(String ID)


used to get TimeZone object for specified TimeZoneID.

• String getID()
This method gets the ID of this time zone.
DateFormat Class
• An abstract class for date/time formatting subclasses which formats and
parses dates or time in a language-independent manner.

• Constructor:
protected DateFormat()
• Constants:
• SHORT is completely numeric, such as 01.12.52 or 3:30pm
• MEDIUM is longer, such as Jan 12, 1952
• LONG is even longer, such as January 12, 1952 or 3:30:32pm
• FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD
or 3:30:42pm IST.
Methods of DateFormat Class

• String format(Date date)


Formats a Date into a date/time string.

• static DateFormat getInstance()


Get a default date/time formatter that uses the SHORT style for both
the date and the time.

• void setTimeZone(TimeZone zone)


Sets the time zone for the calendar of this DateFormat object.
SimpleDateFormat Class
• Defined in java.text package.
• Enables to choose any user-defined pattern for date and time
formatting.

public SimpleDateFormat(String Pattern) Example:

SimpleDateFormat formatter = new


SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");

Note: G represents Era designator, and z is TimeZone.


DateFormatSymbols Class

• Constructors:
DateFormatSymbols()
DateFormatSymbols(Locale locale)

• Methods:
String[] getAmPmStrings() String[] getEras()
String[] getMonths()
Formatting numbers
Formatting Numbers
• Numbers are formatted using an abstract base class
java.text.NumberFormat .

• Formatting numbers is highly locale dependent.

Number: 5000.555
United States: 5,000.555

France: 5000,555

Germany: 5.000,555
Methods

• getInstance(): NumberFormat
• getInstance(locale: Locale): NumberFormat
• getIntegerInstance(): NumberFormat
• getIntegerInstance(locale: Locale): NumberFormat
• getCurrencyInstance(): NumberFormat
• getNumberInstance(): NumberFormat
• getNumberInstance(locale: Locale): NumberFormat
• getPercentInstance(): NumberFormat
• getPercentInstance(locale: Locale): NumberFormat
Formatting Numbers

• Plain Number Format:


NumberFormat nf = NumberFormat.getInstance(Locale l);
System.out.println(nf.format(1010.1055));

• Currency Format:
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale l);

• Percent Format:
NumberFormat nf = NumberFormat.getPercentInstance(Locale l);
DecimalFormat Class

• We can use the applyPattern(String pattern) method of the


DecimalFormat class to specify the patterns for displaying the number.

• A pattern can specify the minimum number of digits before the decimal
point and the maximum number of digits after the decimal point.

• The characters '0‘ and '#' are used to specify a required digit and an
optional digit, respectively.

• The optional digit is not displayed if it is zero.


Example
• The pattern "00.0##" indicates minimum two digits before the decimal point and
maximum three digits after the decimal point.

• If there are more actual digits before the decimal point, all of them are displayed.

• If there are more than three digits after the decimal point, the number of digits is
rounded.
Example:
1987.16920 will be formatted as: 1987.169
1389.2387 will be formatted as: 1389.239
Resource Bundles
Resource Bundles

• A resource bundle is a Java class file or text file that provides locale-specific
information.

• This information can be accessed by Java programs dynamically.

• Resource bundles allows to write programs that separate the locale-sensitive


part of our code from the locale-independent part.

• When a locale-specific resource is needed, our program can load it from the
resource bundle appropriate for the desired locale.
Resource Bundles

• The resources are placed inside the classes that extend the
ResourceBundle class or a subclass of ResourceBundle.

• Resource bundles contain key/value pairs.

• Each key uniquely identifies a locale-specific object in the bundle.

• Key is used to retrieve the object.

• ListResourceBundle is a subclass of ResourceBundle that is often used to simplify the


creation of resource bundles.
Example
public class MyResource extends java.util.ListResourceBundle
{
static final Object[][] contents = {
{"nationalFlag", "us.gif"},
{"nationalAnthem", "us.au"},
{"nationalColor", Color.red},
{"annualGrowthRate", new Double(7.8)}
};
public Object[][] getContents() {
return contents; }
}
Properties File
• If all the resources are strings, they can be placed in a
convenient text file with the extension .properties.
localisation
• Localization is the process of adapting internationalized software
for a specific region or country by translating text and adding
locale-specific components.
• So, it is good to develop an application using the concept of
internationalization. It saves time and reduces development cost
and require less efforts to maintain it.
• Numbers
• Dates
• Currencies
• Images and sounds
• Layouts
Locale.Builder

Another option is Locale.Builder. Locale.Builder has five set methods


that we can use to build an object and at the same time validate those
values:

Locale locale = new Locale.Builder( )


.setLanguage( " fr " )
.setRegion( " CA " )
.setVariant( " POSIX " )
.setScript( " Latn " )
.build( ) ;
Constructors of the Java Locale Class
There are the following three constructors of the
Locale class, as follows:

Locale( String language )


Locale( String language, String country )
Locale( String language, String country, String
variant )
methods
getDefault()
public static Locale getDefault( )
Detects the current default location for this Java
Virtual Machine event.
setDefault()
public static void setDefault( Locale newLocale )

Sets the default location for this Java Virtual


Machine event. This does not affect the location of
the host
getISOCountries()

public static String[ ] getISOCountries( )

Returns a list of all two-letter country codes defined


in ISO 3166. Can be used to create locales.
getCountry()
Synatx:
public String getCountry( )
Returns the country/region code for this locale,
which should either be the empty string, an
uppercase ISO 3166 2-letter code, or a UN M.49
3-digit code.
import java.util.* ;
public class LocaleExample {
public static void main( String[ ] args ) {
// using the getDefault function of the Locale class
Locale locale = Locale.getDefault( ) ;
// using the getDisplayCountry( ) function to display locale's country appropriate for user
System.out.println( locale.getDisplayCountry( ) ) ;
// using the getDisplayLanguage( ) function to display locale's language appropriate for user
System.out.println( locale.getDisplayLanguage( ) ) ;
// using the getDisplayName( ) function to display locale that is appropriate for user
System.out.println( locale.getDisplayName( ) ) ;
// using the getISO3Country( ) function to display the three-letter abbreviation for the lacale's country
System.out.println( locale.getISO3Country( ) ) ;
// using the getISO3Language( ) function to display the three-letter abbreviation for the locale's language
System.out.println( locale.getISO3Language( ) ) ;
// using the getLanguage( ) function to display the two-letter abbreviation for the Locale's language
System.out.println( locale.getLanguage( ) ) ;
// using the getCountry( ) function to display the locale's country code which is an uppercase ISO 2-letter code
System.out.println( locale.getCountry( ) ) ;
// using the getDisplayVariant( ) function to display the locale's variant code appropriate for the user
System.out.println( locale.getDisplayVariant( ) ) ;
United States
English
English (United States)
USA
eng
en
US

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy