Presentation is loading. Please wait.

Presentation is loading. Please wait.

Omaha Java Users Group Java Internationalization Presented by Jason Shepherd 16-May-2005 6:30 PM.

Similar presentations


Presentation on theme: "Omaha Java Users Group Java Internationalization Presented by Jason Shepherd 16-May-2005 6:30 PM."— Presentation transcript:

1 Omaha Java Users Group Java Internationalization Presented by Jason Shepherd 16-May-2005 6:30 PM

2 Outline zConcepts and terminology zInternationalization in J2SE zInternationalization in J2EE (JSTL, Struts) zInternationalization in XSLT zThe Process of Internationalization (Project Plan)

3 Terms zInternationalization yprocess of creating an application so it can be adapted to different languages and countries without coding changes yalso known as “i18n” (since there are 18 characters between the i and n in Unicode)

4 Terms (cont) zCharacteristics of an i18n application yBy adding localized text, the app can display information in any language/country yTextual elements are not hard-coded; they are stored externally and retrieved at run- time

5 Terms (cont) zCharacteristics of an i18n application (cont) ySupport for new languages/countries does not require re-compilation yCulturally dependent data (dates, currencies, etc.) conforms to user’s language/country yCan be localized quickly

6 Terms (cont) zLocalization yprocess of adapting software for a specific language/country by adding locale-specific components and text translations ysometimes abbreviated as l10n (10 letters between L and N in Unicode)

7 Terms (cont) zLocalizing an application ytext translation is the most time-consuming phase of l10n (the human element) ysounds and images may need localized if they are culturally sensitive (red is “purity” in India, but “danger” in the U.S… likewise the icon of a mailbox isn’t familiar outside the U.S., and instead a mail envelope icon should be used) yformatting of numbers, dates, and currencies ymay impact the UI layout strategy

8 Terms (cont) zObject central to l10n is the user’s locale zLocale ypolitical, cultural, and region-specific elements (in Java, expressed as a language code and country code)

9 Terms zLocale (cont) yhas the form xx_YY yxx is two-character language code (ISO-639) yYY is two-character country code (ISO-3166) yExamples: xen_US - United States English xen_GB - Great Britain English xes_MX - Mexico Spanish (Espanol)

10 java.util.Locale Locale enUSLocale = new Locale(“en”, “US”); Locale frCALocale = new Locale(“fr”, “CA”); Locale locale = Locale.US; Locale.getDefault().toString()// “en_US” locale.getLanguage()// “en” locale.getCountry()// “US” locale.getDisplayName()// “English (United States)” locale.getDisplayLanguage()// “English” Locale.setDefault( Locale.FRANCE ); locale.getDisplayName()// “anglais (Etats-Unis)” * Display names are shown according to the default locale

11 Internationalization in J2SE zCreate properties files yexternalized locale-specific UI messages zCreate the locale zCreate a resource bundle (using the locale) zRetrieve UI messages from the resource bundle

12 J2SE zCreating properties files yPlain text file yWill reside in classpath yOne file for each locale (filename convention) yWhen paired with a locale, the closest matching file will be selected MessagesBundle.properties MessagesBundle_en.properties MessagesBundle_en_US.properties MessagesBundle_fr_FR.properties default

13 J2SE zCreating properties files (cont) greetings = Hello. farewell = Goodbye. inquiry = How are you? greetings = Bonjour. farewell = Au revoir. inquiry = Comment allez-vous? MessagesBundle_en_US.properties MessagesBundle_fr_FR.properties

14 J2SE zCreate the locale and resource bundle zRetrieve UI messages from the resource bundle Locale currentLocale = new Locale("fr", “FR", "UNIX"); ResourceBundle messages = ResourceBundle.getBundle(“MessagesBundle", currentLocale); System.out.println( messages.getString(“greetings”) );

15 Eclipse is Almost Helpful zThe “Externalize Strings” option gets you part-way to i18n zDEMO!!

16 J2SE Side Notes zProperties files and resource bundle keys should follow human-readable naming conventions (not “key1”, “key2”, etc.) zSome UI generation engines for thick clients (e.g. SwiXML) have support for i18n; something to consider if you’re creating a Swing app

17 Internationalization in J2EE zLocale stored in HTTP session zResource bundles stored in properties files; told to load in web.xml zLocale-specific messages accessed in the Web tier (generally) using tag libraries zJSTL versus Struts tag libraries zWe’ll refer to Struts for the remainder of the presentation

18 J2EE (w/ Struts) zEnsure your properties files are visible on the classpath yspecial attention must be paid if using Ant or Maven to build your application ymust be placed somewhere that all classes will be able to find it (e.g. a “library” jar file or possibly in the WAR file under WEB- INF/classes)

19 J2EE (w/ Struts) zConfigure web.xml to load the properties files action org.apache.struts.action.ActionServlet application MessagesBundle locale true

20 J2EE (w/ Struts) zAccessing the resource bundles in a JSP zFirst, include the Struts “bean” tag library  zThen, replace any hard-coded messages y Hello y

21 J2EE (w/ Struts) zAccessing outside of a JSP ye.g. back-end translation code (locale passed through to data tier) ye.g. JavaScript messages yAt UPRR, we wrote a utility class to load the properties files and retrieve messages Locale locale = (Locale) session.getAttribute(org.apache.struts.action.Action.LOCALE_KEY); String message = ResourceBundleUtil.getMessage( locale, “hello.message”); * this class comes in handy later on in the presentation… stay tuned!

22 J2EE Apps: The Database Dilemma zTranslating values that come out of a database zIf DB is not shared between applications, localized strings can be stored in the DB yDB must be Unicode-enabled and have plenty of extra disk space zIf localized messages cannot be stored in the DB, keys can be stored in the DB that refer to values in the properties files

23 Storing localized text in the database: Pros zTruly dynamic localization zOne table for supported locales zOther tables for translation zCan correct translation errors on-the-fly yGood when translation error looks silly/unprofessional, or worse yet, offensive

24 Storing localized text in the database: Cons zRequires more database accesses yVery bad for high volume sites where database connections must be managed carefully zMight require complex joins in a well- normalized DB schema zCould be considered bad design to store presentation tier artifacts in the data tier??

25 J2EE Side Notes zProperties files for Struts can be HTML escape encoded (á) or Unicode encoded (\u0000)… Struts is encoding- aware zThe content type in your resulting HTML page must be set correctly

26 Internationalization in XSLT zSome J2EE applications deliver their data to the Web tier as XML and then render the UI using XSLT (or a combination of JSP and XSLT) zNo way in XSLT to natively access your properties files

27 XSLT zDifferent approaches to i18n in this case: yOption 1: Not try to use properties files; instead, make a separate XSLT stylesheet for each locale (yuck! Must be maintained and manually synchronized with the properties files) yOption 2: Write code to insert the locale into the stylesheet as an XSL variable. Then use Java from within your stylesheet to extract the resource bundle messages (Xalan-Java extensions)

28 XSLT zXSLT stylesheets can be localized the same way JSPs are. zBut, XSLT stylesheets don't have access to the Java HTTP session object that stores the user’s locale information  The locale must be inserted into the XSLT and passed to the Xalan-Java objects

29 XSLT: An i18n (not l10n yet) Stylesheet <xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:inl="http://www.insightnl.com" xmlns:message="http://xml.apache.org/xalan/java/co m.uprr.app.inl.util.ResourceBundleUtil" version="0.1">

30 XSLT: Retrieving the Stylesheet and Inserting the Locale * Note: we store our XSLT in the DB

31 XSLT: Retrieving the Stylesheet and Inserting the Locale i18n XSLT locale insert XSLT l10n XSLT

32 The Process of i18n zi18n (create properties files and remove hard-coded values) zSend translations to translators zReceive translations and perform l10n zDeploy and have customers acceptance test

33 Recap zTerms: i18n, l10n, locale zJ2SE support for i18n zJ2EE support for i18n zHow internationalizing a J2EE can impact XSLT-based front-ends zThe Process

34 Questions? zQuestions, comments, taunts, or exclamations? I’ll give you an exclamation: Flippin’ sweet!


Download ppt "Omaha Java Users Group Java Internationalization Presented by Jason Shepherd 16-May-2005 6:30 PM."

Similar presentations


Ads by Google