Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internationalization Slide 1©SoftMoore Consulting.

Similar presentations


Presentation on theme: "Internationalization Slide 1©SoftMoore Consulting."— Presentation transcript:

1 Internationalization Slide 1©SoftMoore Consulting

2 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 ConsultingSlide 2

3 Characteristics of an Internationalized 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©SoftMoore Consulting

4 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 ConsultingSlide 4

5 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 ConsultingSlide 5

6 Running the Example java I18NSample fr FR Bonjour. Comment allez-vous? Au revoir. java I18NSample en US Hello. How are you? Goodbye. ©SoftMoore ConsultingSlide 6

7 Property Files for the Example File MessagesBundle_fr_FR.properties greeting = Bonjour. farewell = Au revoir. inquiry = Comment allez-vous? File MessagesBundle_en_US.properties greeting = Hello. farewell = Goodbye. inquiry = How are you? File names are significant! ©SoftMoore ConsultingSlide 7

8 Topics Locales Using resource bundles Date formatting Message formatting ©SoftMoore ConsultingSlide 8

9 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 ConsultingSlide 9

10 Sample Language Codes Language CodeDescription deGerman enEnglish frFrench jaJapanese koKorean zhChinese ©SoftMoore ConsultingSlide 10

11 Sample Country Codes Country CodeDescription CNChina DEGermany FRFrance INIndia USUnited States ©SoftMoore ConsultingSlide 11

12 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 ConsultingSlide 12

13 Method getLocale() Returns the current locale Defined for –Applet –Component –MessageFormat –Window If no locale is set, method getLocale() returns the default locale. ©SoftMoore ConsultingSlide 13

14 Available Locales To retrieve the available locales Locale 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 use locale.getLanguage() locale.getCountry() locale.getVariant() ©SoftMoore ConsultingSlide 14

15 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: MyResourceBundle MyResourceBundle_en MyResourceBundle_fr_CA MyResourceBundle_fr_FR ©SoftMoore ConsultingSlide 15

16 Creating a Bundle as Compiled Code Simply extend class ListResourceBundle public 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 ConsultingSlide 16

17 Working with a Resource Bundle Getting a bundle Locale l = getLocale(); ResourceBundle labels = ResourceBundle.getBundle("Training", l); Using a bundle p.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 ConsultingSlide 17

18 Supporting a Different Locale Example: Define a Finnish bundle public 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 ConsultingSlide 18

19 Class PropertyResourceBundle Initialized from property file key=value format File naming convention Bundlename_localization-string.properties –TrainingSettingsBundle.properties –TrainingSettingsBundle_fi.properties Example file contents Date=June 3, 1998 Cost=1200.00 Map=SMarea.gif ©SoftMoore ConsultingSlide 19

20 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 ConsultingSlide 20

21 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 ConsultingSlide 21

22 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 ConsultingSlide 22

23 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 ConsultingSlide 23

24 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 ConsultingSlide 24

25 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 ConsultingSlide 25

26 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 ConsultingSlide 26

27 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 array Object[] args = {exceptionName, filename}; Use MessageFormat.format() to fill in placeholders ©SoftMoore ConsultingSlide 27

28 Example: Displaying Messages with MessageFormat 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 ConsultingSlide 28

29 Example: Displaying Messages with MessageFormat (continued) Output after substitutions –I/O Exception while loading: Foobar.java –Foobar.java loaded unsuccessfully: I/O Exception ©SoftMoore ConsultingSlide 29

30 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 ConsultingSlide 30

31 International Training ©SoftMoore ConsultingSlide 31

32 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 ConsultingSlide 32

33 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 ConsultingSlide 33

34 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 ConsultingSlide 34

35 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 ConsultingSlide 35

36 Online Resources Java Tutorial Trail on Internationalization https://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 ConsultingSlide 36


Download ppt "Internationalization Slide 1©SoftMoore Consulting."

Similar presentations


Ads by Google