Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internationalization and the Java Stack Part 1 Matt Wheeler.

Similar presentations


Presentation on theme: "Internationalization and the Java Stack Part 1 Matt Wheeler."— Presentation transcript:

1 Internationalization and the Java Stack Part 1 Matt Wheeler

2 Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Introduction to Spring – Basic Java and XML skills – Installed LdsTech IDE (or other equivalent – good luck there ;)

3 Overview Internationalization in general Java Internationalization (ResourceBundle) Spring Internationalization (MessageSource) MessageSource vs. ResourceBundle Spring Helpers JSP tags Locale resolver Locale change interceptor

4 Internationalization in General (I18n) "Internationalization, in relation to computer programming, is the process of designing and writing an application so that it can be used in a global or multinational context. An internationalized program is capable of supporting different languages, as well as date, time, currency, and other values, without software modification.“

5 Internationalization (continued) "Internationalization is the process of designing software so that it can be adapted (localized) to various languages and regions easily, cost- effectively, and in particular without engineering changes to the software. This generally involves isolating the parts of a program that are dependent on language and culture....“ – http://www.ibm.com/developerworks/java/tutorials/ j-i18n/section2.html

6 Localization (L10n) "Localization is the process of adapting a program for use in a specific locale. A locale is a geographic or political region that shares the same language and customs. Localization includes the translation of text such as user interface labels, error messages, and online help. It also includes the culture-specific formatting of data items such as monetary values, times, dates, and numbers." http://www.ibm.com/developerworks/java/tutorials/j- i18n/section2.html

7 Internationalization vs. Localization Internationalization is developing the application so that it can handle multiple locales without code change Localization is the process of adding a new locale to an application – Includes translation of resources, …

8 Localization (continued) "Localization is the process of designing and writing an application capable of dealing with a specific regional, country, language, cultural, business, or political context. In a sense, every application written for a specific area is localized, although most of these effectively support only one locale. Usually, though, true localization is achieved by core code that accesses locale, location, political, or other specific components and modules, along with translating text as appropriate for the audience. A properly internationalized program facilitates and provides a foundation for localization.“ – http://www.ibm.com/developerworks/java/tutorials/j- i18n/section2.html

9 First Steps of Internationalization Extract translatable text from code Load resources for a specific locale Inject locale specific resources into the application

10 Java Internationalization (ResourceBundle) ResourceBundle is the cornerstone of Java internationalization – Backed by different data stores Property files (PropertyResourceBundle) Java source code (ListResourceBundle) Represents a collection of key/value pairs for a given locale

11 In Practice Property file(s) Accessing locale specific resources at runtime #bundle.properties abc=where is the beef #bundle_es.properties #abc=¿dónde está la carne de vaca abc=\u00a8d\u00a2nde est\u00a0 la carne de vaca ResourceBundle.getBundle("bundle").getString("abc") //where is the beef ResourceBundle.getBundle("bundle", new Locale("es")).getString("abc") // ¿dónde está la carne de vaca ResourceBundle.getBundle("bundle", new Locale("es", "AR")).getString("abc") // ¿dónde está la carne de vaca

12 Bundle Resolution The resolution pattern Concrete example basename + “_” + language1 + “_” + country1 + “_” + variant1 basename + “_” + language1 + “_” + country1 basename + “_” + language1 basename + “_” + language2 + “_” + country2 + “_” + variant2 basename + “_” + language2 + “_” + country2 basename + “_” + language2 basename messages_pt_BR.properties messages_pt.properties messages_en_GB.properties messages_en_US.properties messages.properties #basename – messages (and we have a default bundle with that name) # fr would resolve to message.properties # pt_PT_CO would resolve to messages_pt.properties # en would resolve to messages.properties

13 DEMO

14 Spring Internationalization (MessageSource) MessageSource is the cornerstone of Spring internationalization MessageSource interface – An abstraction to the actual text store of translated resources Data store can be properties files, database, MarkLogic, … Implement the interface for the given resource store – Many MessageSource implementations available out of the box including a basic resource bundle source

15 MessageSource Example Place resource property files in src/main/bundles Configure the message source as follows: MessageSource must have an id of “messageSource” classpath:messages classpath:otherbundle

16 Inject MessageSource Utilize the MessageSource @Inject private MessageSource messageSource; public void getAStringInCode(ModelMap model) { String message = messageSource.getMessage("abc", null, "Default text.", Locale.ENGLISH); //do something with the message return; }

17 MessageSource vs. ResourceBundle MessageSource being an interface is far more extensible MessageSource allows all resources to be conglomerated together MessageSource does parameter replacement automatically MessageSource can set default encoding #born={0} was born on {1}. String pattern = ResourceBundle.getBundle("whatever", Locale.ENGLISH).getString("born"); MessageFormat.format(pattern, "Billy", new Date()) messageSource.getMessage("born", new Object[] {"Billy", new Date()}, "default", Locale.ENGLISH)

18 DEMO

19 Spring MessageSource taglib http://static.springsource.org/spring/docs/3.1.x/spri ng-framework- reference/html/spring.tld.html#spring.tld.message

20 DEMO

21 Lab 1: Internationalize a page https://tech.lds.org/wiki/Internationalization_and_ the_Java_Stack_- _Part_1#Lab_1_Internationalize_a_Page

22 Spring Internationalization Architecture LocaleResolver – Attempts to determine the current user’s locale – Provides a way to set / cache current user’s locale LocaleChangeInterceptor – Picks up locale changes (from request parameter by default) – Sets locale on the resolver

23 Example Configuration Sample native Spring configuration: LocaleResolver must have id of “localeResolver” Now /whatever?sitelanguage=it will change the locale

24 ChainedLocaleResolver Based on Spring LocaleResolver interface Locale resolution on steroids Sets up multiple locale resolvers from which to determine the user’s locale

25 ChainedLocaleResolver (configuration) Basic configuration Or when using WAM http://code.lds.org/maven- sites/stack/module.html?module=web- spring/xsddoc/index.html http://code.lds.org/maven- sites/stack/module.html?module=web- spring/apidocs/org/lds/stack/web/spring/i18n/ChainedLocaleR esolver.html xmlns:stack-web="http://code.lds.org/schema/spring/web"

26 Lab 2: Configure Locale Change and Resolution https://tech.lds.org/wiki/Internationalization_and_ the_Java_Stack_- _Part_1#Lab_2_Configure_Locale_Change_and_ Resolution

27 Credit where credit is due http://www.springsource.org/spring- framework#documentation http://www.ibm.com/developerworks/java/tutor ials/j-i18n/section2.html


Download ppt "Internationalization and the Java Stack Part 1 Matt Wheeler."

Similar presentations


Ads by Google