- Forward


Localization and Internationalization
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Definitions
Back SMYC Forward
  • Localization (l10n):
    • The adaptation of a product to meet the requirements of a particular locale (including language and cultural differences)
  • Internationalization (i18n):
    • The design of a product that facilitates localization
Common Requirements
Back SMYC Forward
  • Terminology:
    • Language Differences
  • Formatting and Symbols:
    • Numbers
    • Dates and Times
    • Addresses
    • Currencies
Locales
Back SMYC Forward
  • Encapsulation:
    • The Locale class encapsulates language (ISO 639), a script or writing system (ISO 15924), a country or region (ISO 3166), and a variant (IETF BCP47) like Canadian French or Swiss German
  • Determination:
    • The JVM determines the Locale of the processor it is running on at startup (which can be retrieved using Locale.getDefault() )
Locales (cont.)
Back SMYC Forward
  • Changing from the Command Line:
    • Use the -Duser.country and -Duser.language options (e.g., java -Duser.country=CA -Duser.language=fr MainClass )
  • Changing within a Program:
    • Invoke Locale.setDefault(java.util.Locale)
Techniques for Localizing Terminology
Back SMYC Forward
  • On-Line:
    • Use machine translation at run-time
  • Off-Line:
    • Use machine translation or human translation to create a mapping at build time
Working with Mapping
Back SMYC Forward
  • The Important Class:
    • ResourceBundle
  • Use:
    • Create a file containing a mapping between keys (in the developer's natural language) and values (in the user's natural language)
    • Load the appropriate mapping based on the Locale
Working with Mappings (cont.)
Back SMYC Forward
Some Example Files

Strings_en_US.properties

CANCEL = Cancel FILE = File OPEN = Open

Strings_fr_FR.properties

CANCEL = Annuler FILE = Fichier OPEN = Ouvrir

Working with Mappings (cont.)
Back SMYC Forward
Using the Files
// Declare and initialize a static attribute public static final ResourceBundle STRINGS = ResourceBundle.getBundle("calc.ui.Strings"); // Use the static attribute to create a JButton JButton cancelButton = new JButton(STRINGS.getString("CANCEL")); cancelButton.setActionCommand("CANCEL"); // Respond to an event public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if (ac.equals("CANCEL")) { // Respond appropriately } }
Formatting Numbers
Back SMYC Forward
  • Some Issues:
    • The symbol between the one's place and the one-tenth's place
    • The symbol used for groupings of places
    • Formatting positive and negative numbers
  • Useful Classes/Methods:
    • There are versions of printf() (in PrintStream ) and format() (in String ) that are passed a Locale and the versions that aren't passed a Locale invoke the versions that do, passing the default Locale
    • The NumberFormat class provides even more flexibility, though it is less convenient
Formatting Currencies
Back SMYC Forward
  • Some Issues:
    • The currency symbol
    • The placement of the currency symbol (before or after the amount)
    • All of the issues for numbers
  • Useful Classes/Methods:
    • The NumberFormat class has a static getCurrencyInstance() class that returns a NumberFormat object that will format currency information in a locale-specific way
Formatting Dates and Times
Back SMYC Forward
  • Some Issues:
    • Words or numbers (especially for the month)
    • Ordering of the year, month, and day
  • Useful Classes/Methods:
    • The DateFormat class has a format() method that can be used to format dates in a variety of ways (including SHORT, MEDIUM, LONG and FULL representations)
There's Always More to Learn
Back -