1 / 26

Matt Wheeler

Internationalization in the Java Stack. Matt Wheeler. 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 ;). Overview.

luce
Download Presentation

Matt Wheeler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internationalization in the Java Stack 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 change interceptor • Locale resolver

  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. First Steps of Internationalization • Extract translatable text from code • Load resources for a specific locale • Inject locale specific resources into the application

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

  10. For example • Property file(s) • Accessing the resource #bundle.properties abc=some string #bundle_it.properties abc=some Italian string ResourceBundle.getBundle("bundle").getString("abc") //some string ResourceBundle.getBundle("bundle", Locale.ITALIAN).getString("abc") //some Italian string

  11. DEMO

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

  13. MessageSource Example • Place resource bundles in src/main/bundles • Configure the message source as follows: <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:messages</value> <value>classpath:otherbundle</value> </list> </property> </bean>

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

  15. MessageSource vs. ResourceBundle • MessageSource allows all resources to be conglomerated together • MessageSource does parameter replacement automatically • MessageSource allows for a default (in case message is not found) #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)

  16. DEMO

  17. Spring MessageSourcetaglib • http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/spring.tld.html#spring.tld.message <%@tagliburi="http://www.springframework.org/tags" prefix="spring"%> <spring:messagecode="message.key"/> <spring:messagecode="some.key" arguments="aaa, bbb"/>

  18. DEMO

  19. Lab 1: Internationalize a page https://tech.lds.org/wiki/Internationalization_in_Java_Stack#Lab_1_Internationalize_a_Page

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

  21. Example Configuration • Sample native Spring configuration: <mvc:interceptors> <bean ref="localeChangeInterceptor" /> </mvc:interceptors> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="siteLanguage"/> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

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

  23. ChainedLocaleResolver (configuration) • Basic configuration • Or when using WAM • code.lds.org/maven-sites/stack/module.html?module=web-spring/xsddoc/index.html • code.lds.org/maven-sites/stack/module.html?module=web-spring/apidocs/org/lds/stack/web/spring/i18n/ChainedLocaleResolver.html xmlns:stack-web="http://code.lds.org/schema/spring/web" <stack-web:locale-resolver /> <stack-web:locale-resolver use-wam-locale=" true" />

  24. Lab 2: Configure Locale Change and Resolution https://tech.lds.org/wiki/Internationalization_in_Java_Stack#Lab_2_Configure_Locale_Change_and_Resolution

  25. Spring MessageSourcetaglib

  26. Credit where credit is due • http://www.ibm.com/developerworks/java/tutorials/j-i18n/section2.html

More Related