Internationalization_and_the_Java_Stack_Part_2

Download Report

Transcript Internationalization_and_the_Java_Stack_Part_2

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
• Stack provided internationalizaton tools
• Message source expression language resolver
• JS Message source
• Internationalization best practices
• Internationalization testing
• Dynamic pseudo translation
Expression Language 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"/>
<!- Reference the messages in EL by key -->
${messages['abc.def.ghi']}
• http://code.lds.org/mavensites/stack/module.html?module=webspring/#El_Message_Source
JavaScript Source
• JS Configuration
<bean id="messageSourceController" class="org.lds.stack.web.spring.i18n.MessageSourceController">
<property name="messageSource" ref="messageSource" />
</bean>
<!-- Create adapter to handle all non-annotation controllers -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- Map the properties url to the controller -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/messageSource.js">messageSourceController</prop>
</props>
</property>
<property name="order"
value="#{T(org.springframework.core.Ordered).HIGHEST_PRECEDENCE}"/>
</bean>
MessageSource with JS
• Usage in a JSP page
<script charset="utf-8" src="${pageContext['request'].contextPath}/messageSource.js"
type="text/javascript"></script>
<script type="text/javascript">
alert(msgs['abc']);
</script>
Some I18n Best Practices
•
•
•
•
Externalize ALL translatable text
Do not concatenate translations
Create duplicate / separate resources
Avoid text in images
Externalize ALL Text
• Page label - "Name: “
• "Name:" including the colon, and not just "Name" externalized
– Otherwise require contatenation - the “Name”coming out of the
bundle with : on the page
• : and associated formatting can be different in different
languages
– It might also be in a different order
Do NOT Concatenate Translations
• For example:
key1=Someone named
key2=likes hiking.
• Attempted usage
${messages['key1']} Billy ${messages['key2']}
More Correct Way
• Create a single string with replaceable
parameters
• Sentence maintains context
– Parameters can be moved around to accommodate
language grammar
• For example:
key=Someone named {0} likes hiking.
• And then it will be used as follows:
<spring:message code="key"
arguments="${hikersName}" text="Default text."/>
Create Separate Resources
• Counter intuitive to code reuse concept
• Same English strings should not be shared
• Within the same application, or even the same page
• Create a separate key value pair for each occurrence
• Words change based on context
• Concept of masculine and/or feminine, age classes, …
• Or usage
• Similar English words may not be similar in another language
Avoid Placing Text in Images
• An image that contains text will require a new image for each
language
• It will also require a custom way to load the image
• Impose the text over the image, using .css or other ingenious
alternative
– Then the text can be stored in the resource bundles with all other
strings
– And it won’t require a new image for each language
I18n Testing (Dynamic Pseudo Translation)
• Configured to translate for specified locales
• Simplifies testing
–
–
–
–
Expansion
Special characters
Completeness
zz locale
• http://code.lds.org/mavensites/stack/module.html?module=webspring/#Pseudo_Translation_Message_Source_Faca
de
Dynamic Pseudo Translation
• The trick (delegate)
<!-- Application Message Bundle -->
<bean id="messageSource"
class="org.lds.stack.web.spring.i18n.message.PseudoMessageSourceFacade">
<constructor-arg ref="delegatingMessageSource" />
</bean>
<bean id="delegatingMessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename">
<list>
<value>classpath:messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
Lab
asdf
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