Internationalization Slide 1©SoftMoore Consulting.
-
Upload
kathleen-kennedy -
Category
Documents
-
view
218 -
download
0
description
Transcript of Internationalization Slide 1©SoftMoore Consulting.
Internationalization
Slide 1©SoftMoore Consulting
Internationalization
• Internationalization – the process of designing an application so that it can be adapted to various languages and regions without engineering changes.Often abbreviated “i18n”, because there are 18 letters between the first “i” and the last “n”
• Localization – the process of adapting software for a specific region or language by adding locale-specific components and translating text.Often abbreviated as “l10n” for similar reasons
• The primary task of localization is translating the user interface elements and documentation.(language, numbers, dates, currency, etc.)
©SoftMoore Consulting Slide 2
©SoftMoore Consulting
Characteristics of anInternationalized Program
• With the addition of localized data, the same executable can run worldwide.
• Textual elements, such as status messages and the GUI component labels, are not hardcoded in the program. Instead they are stored outside the source code and retrieved dynamically.
• Support for new languages does not require recompilation.• Culturally-dependent data, such as dates and currencies,
appear in formats that conform to the end user's region and language.
• It can be localized quickly.
Slide 3
Example: Before Internationalization
public class NotI18N { static public void main(String[] args) { System.out.println("Hello."); System.out.println("How are you?"); System.out.println("Goodbye."); } }
©SoftMoore Consulting Slide 4
Example: After Internationalization
public class I18NSample { static public void main(String[] args) { String language = new String(args[0]); String country = new String(args[1]);
Locale loc = new Locale(language, country); ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", loc);
System.out.println(messages.getString("greeting")); System.out.println(messages.getString("inquiry")); System.out.println(messages.getString("farewell")); } }
©SoftMoore Consulting Slide 5
Running the Example
• java I18NSample fr FRBonjour.Comment allez-vous?Au revoir.
• java I18NSample en USHello.How are you?Goodbye.
©SoftMoore Consulting Slide 6
Property Files for the Example
• File MessagesBundle_fr_FR.propertiesgreeting = Bonjour.farewell = Au revoir.inquiry = Comment allez-vous?
• File MessagesBundle_en_US.propertiesgreeting = Hello.farewell = Goodbye.inquiry = How are you?
File names are significant!
©SoftMoore Consulting Slide 7
Topics
• Locales• Using resource bundles• Date formatting• Message formatting
©SoftMoore Consulting Slide 8
Locale
• A locale defines local area configuration data– specific language– specific country– specific territory / variant (optional)
• Class Locale resides in the java.util package• Several Java classes provide methods that return
instances of Locale
©SoftMoore Consulting Slide 9
Sample Language Codes
Language Code Description de German en English fr French ja Japanese ko Korean zh Chinese
©SoftMoore Consulting Slide 10
Sample Country Codes
Country Code Description CN China DE Germany FR France IN India US United States
©SoftMoore Consulting Slide 11
Getting the Locale
• Constructing a locale manually:Locale l = new Locale(String lang, String country);Locale l = new Locale(String lang, String country, String variant);
• Getting the default locale:Locale l = Locale.getDefault();
• Setting locale-related properties (dangerous):Properties p = System.getProperties();p.put("user.language", "fr");p.put("user.region", "CA");System.setProperties(p);
©SoftMoore Consulting Slide 12
Method getLocale()
• Returns the current locale• Defined for
– Applet– Component– MessageFormat– Window
• If no locale is set, method getLocale() returns the default locale.
©SoftMoore Consulting Slide 13
Available Locales
• To retrieve the available localesLocale locales[] = Locale.getAvailableLocales();
• Method locale.toString() returns concatenated values– “_fr_FR” (French/France)– “_fr_CA” (French/Canada)– “_en_US” (English/United States)
• To retrieve locale properties as strings uselocale.getLanguage()locale.getCountry()locale.getVariant()
©SoftMoore Consulting Slide 14
Resource Bundles
• Isolate grouping for specific locales• Class ListResourceBundle defines a bundle within
the source code• Class PropertyResourceBundle defines a bundle via
a property file• Access is identical – names for multiple locales:
MyResourceBundleMyResourceBundle_enMyResourceBundle_fr_CAMyResourceBundle_fr_FR
©SoftMoore Consulting Slide 15
Creating a Bundle as Compiled Code
• Simply extend class ListResourceBundlepublic class Training extends ListResourceBundle { static final Object[][] contents = { { "Date", "Date: " }, { "Cost", "Cost: " }, { "Location", "Location: " }, { "Title", "Training" } };
public Object[][] getContents() { return contents; } }
• Array contents provides key-value pairs
©SoftMoore Consulting Slide 16
Working with a Resource Bundle
• Getting a bundleLocale l = getLocale(); ResourceBundle labels =
ResourceBundle.getBundle("Training", l);
• Using a bundlep.add(new Label(labels.getString("Date")));p.add(new Label(labels.getString("Cost")));p.add(new Label(labels.getString("Location")));add(new Label(labels.getString("Title"), Label.CENTER),
BorderLayout.NORTH);
©SoftMoore Consulting Slide 17
Supporting a Different Locale
• Example: Define a Finnish bundlepublic class Training_fi extends ListResourceBundle { static final Object[][] contents = { { "Date", "Päivämäärä: " }, { "Cost", "Hinta: " }, { "Location", "Sijainti: " }, { "Title", "Koulutus" } };
public Object[][] getContents() {...} }
• Simply append "_fi" to base bundle name.
©SoftMoore Consulting Slide 18
Class PropertyResourceBundle
• Initialized from property filekey=value format
• File naming conventionBundlename_localization-string.properties– TrainingSettingsBundle.properties– TrainingSettingsBundle_fi.properties
• Example file contentsDate=June 3, 1998Cost=1200.00Map=SMarea.gif
©SoftMoore Consulting Slide 19
Locale-Specific Formats
• Manipulating data for different locales– NumberFormat handles number formatting and manipulation– DateFormat handles reading and displaying of dates– MessageFormat supports error and informational messages
• Example: For “June 3, 1998”– US: 6/3/98– Italy: 3/6/98– Finland: 03.06.98
• See the java.text package for details
©SoftMoore Consulting Slide 20
Example: Reading Date-Related Data
Local aLocale = getLocale(); ResourceBundle settings = ResourceBundle.getBundle("Training", aLocale);String date = settings.getString("Date");DateFormat dfInput = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
Date inputDate;
try { inputDate = dfInput.parse(dateString)); }catch (ParseException e) { inputDate = null; }©SoftMoore Consulting Slide 21
Example: Writing Date-Related Data
Local aLocale = getLocale();DateFormat dfOutput = DateFormat.getDateInstance(DateFormat.SHORT, aLocale);
String dateLabelString;
try { dateLabelString = dfOutput.format(dfInput.parse(dateString)); }catch (ParseException e) { dateLabelString = dateString; }
©SoftMoore Consulting Slide 22
Class NumberFormat
• Deals with reading and display of numbers, currency, and percentage
• Supports numeric output in locale-appropriate format• Example: For “1200.00”
– US: $1,200.00– Italy: L. 1.200,00– Finland: 1 200,00 mk
©SoftMoore Consulting Slide 23
Example: Manipulating Currency Values
String costString = settings.getString("Cost");NumberFormat nfInput = NumberFormat.getNumberInstance(Locale.US);NumberFormat nfOutput = NumberFormat.getCurrencyInstance(aLocale));
String costLabelString;
try { Number n = nfInput.parse(costString); costLabelString = nfOutput.format(n.longValue()); }catch (ParseException e) { costLabelString = costString; }
©SoftMoore Consulting Slide 24
Class MessageFormat
• Supports fill-in-the-blank (error) messages• Example
MessageFormat format1 = new MessageFormat ("File {1} overflowing filesystem {0}.");
• The {n} syntax designates string substitution area
©SoftMoore Consulting Slide 25
Localized Messages
• Class MessageFormat also assists with localization of messages– can specify format of message with place holders for runtime
context– can reorder placeholders based on locale
• Example output– I/O Exception while loading: Foobar.java– Foobar.java loaded unsuccessfully: I/O Exception
or– The disk G contains 3 files. (English)– Il y a 3 fichiers sur le disque G. (French)
©SoftMoore Consulting Slide 26
Using Class MessageFormat
• Define format styles:– "{0} while loading: {1}"– "{1} loaded unsuccessfully: {0}"
• Define contents– syntax {n} represents a placeholder– values start with 0
• Place parameter values in an Object arrayObject[] args = {exceptionName, filename};
• Use MessageFormat.format() to fill in placeholders
©SoftMoore Consulting Slide 27
Example: Displaying Messages withMessageFormat
public class FormatIt { public static void main(String args[]) { String fmt1 = "{0} while loading: {1}"; String fmt2 = "{1} loaded unsuccessfully: {0}"; String filename = "Foobar.java"; String exceptionName = "I/O Exception"; Object[] fmtargs = {exceptionName, filename};
System.out.println( MessageFormat.format(fmt1, fmtargs)); System.out.println( MessageFormat.format(fmt2, fmtargs)); } }
©SoftMoore Consulting Slide 28
Example: Displaying Messages withMessageFormat (continued)
• Output after substitutions– I/O Exception while loading: Foobar.java– Foobar.java loaded unsuccessfully: I/O Exception
©SoftMoore Consulting Slide 29
Starting an Application
• For default locale, just run it• For different locale
– Set Properties from inside your program– Set Properties from command line
java -Duser.language=it -Duser.region=IT ClassSchedule
©SoftMoore Consulting Slide 30
International Training
©SoftMoore Consulting Slide 31
Displaying Unicode Fonts
• If your fonts are installed correctly, it works• Basic functionality only works for displaying text, not
editing it• Works for Label or Choice, for example, but not TextField
©SoftMoore Consulting Slide 32
Example: Displaying Unicode Fonts
public class MyDisplay extends Frame { public static class MyCanvas extends Canvas { public MyCanvas() { setFont (new Font ("Serif", Font.PLAIN, 36)); }
public void paint (Graphics g) { String s = "\u3041\u3042\u3043\u3044\u3045"; g.drawString(s, 20, 50); } }
©SoftMoore Consulting Slide 33
Example: Displaying Unicode Fonts(continued)
public MyDisplay() { super("Unicode Tester"); add(new MyCanvas(), BorderLayout.CENTER);
Choice c = new Choice(); c.addItem ("\u3041\u3042\u3043\u3044\u3045"); c.addItem ("\u3041\u3042\u3043\u3044\u3046"); add (c, BorderLayout.SOUTH); } }
©SoftMoore Consulting Slide 34
Internationalization Checklist
• Identify culturally dependent data– messages – labels on GUI components– dates/times – numbers– currencies – etc
• Isolate translatable text in resource bundles• Handle compound messages• Format numbers, currencies, dates, times, etc.• Use unicode character properties• Compare strings properly (e.g., with a Collator object)• Convert non-unicode text
©SoftMoore Consulting Slide 35
Online Resources
• Java Tutorial Trail on Internationalizationhttps://docs.oracle.com/javase/tutorial/i18n/
• ISO 639-1 (language codes)https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
• ISO 3166-2 (country codes)https://en.wikipedia.org/wiki/ISO_3166-2
©SoftMoore Consulting Slide 36