Internationalization_in_the_Java_Stack - LDSTech

Download Report

Transcript Internationalization_in_the_Java_Stack - LDSTech

Internationalization
Matt Wheeler
Notes
• This is a training NOT a presentation
• Please ask questions
• Prerequisites
– Introduction to Java Stack
– Basic Java and XML skills
– Installed LdsTech IDE (or other equivalent – good luck
there ;)
Overview
•
•
•
•
•
Internationalization in general
Java Internationalization (ResourceBundle)
Spring Internationalization (MessageSource)
MessageSource vs. ResourceBundle
Spring Helpers
• Locale change interceptor
• Locale resolver
• JSP tags
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.“
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. For example, the text of error messages
must be kept separate from program source code
because they must be translated during localization.“
– http://www.ibm.com/developerworks/java/tutorials/ji18n/section2.html
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."
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/ji18n/section2.html
First Step of Internationalization
• Extract translatable text from code
• Load resources for a specific locale
• Inject locale specific resources into the
application
Java Internationalization
(ResourceBundle)
• ResourceBundle is the basis of Java
internationalization
– Backed by different data stores
• Property files
• Java source code
• Represents a collection of key/value pairs for a
given location
• For example:
ResourceBundle.getBundle("bundle").getString("abc")
ResourceBundle.getBundle("bundle", Locale.ITALIAN).getString("abc")
DEMO
Spring Internationalization
(MessageSource)
• MessageSource is the root of Spring
internationalization
• MessageSource interface
– And abstraction to the actual text store of translated
files
• Data store can be properties files, database, MarkLogic, …
• Implement the interface for the given resource store
– Implementation of MessageSource can use resource
bundles as in native Java
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>
Inject MessageSource
• Utilize the MessageSource
@Inject
private MessageSource messageSource;
@RequestMapping(value="/", method=RequestMethod.GET)
public void getAStringInCode(ModelMap model) {
String message = messageSource.getMessage("abc", null, "default", Locale.US);
//do something with message
return;
}
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)
Spring MessageSource taglib
• http://static.springsource.org/spring/docs/3.1.x/spri
ng-frameworkreference/html/spring.tld.html#spring.tld.message
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<spring:message code=“messsage.key"/>
MessageSource Expression Resolver
• Provide message resolver can be utilized in EL
<%@ taglib prefix="web-spring" uri="http://code.lds.org/web/spring" %>
<!-- This makes the internationalized properties of the default
MessageSource available as a map in the specified scope-->
<web-spring:message-source var="messages" scope="request"/>
<!- Referecence the messages in EL by key -->
${messages['abc.def.ghi']}
• http://code.lds.org/mavensites/stack/module.html?module=webspring/#El_Message_Source
Spring MessageSource taglib
Lab 1: Internationalize a page
https://tech.lds.org/wiki/Introduction_to_Spring#L
ab_2_Dependency_Injection
Credit where credit is due
• http://www.ibm.com/developerworks/java/tutor
ials/j-i18n/section2.html