Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Using a Different Language: Do You Speak Spanish? Chapter 6 ■ Using Resource Files As a developer, we want everybody to use our application, but what happens when the spoken language is a barrier? You need to start thinking about how to internationalize your application. In other words, your application needs to support different languages. For example, major banks offer their customers the option of changing the language for their entire web site. So what happens if your Spring application, My Documents, has a Spanish-speaking user? Is this person able to understand English? The Spring Framework has some support classes for this purpose. The ResourceBundleMessageSource class allows reading different locales and retrieving the correct message specifying its key based on the properties file. In order to use this class, it is necessary to have a default properties file with any name, and the locales followed by an underscore and the locale. In your application, you are going to put English as your default language and Spanish as the secondary language. Let’s create the files. You need to create three files, two for English and one for Spanish. It’s always necessary to have a default dictionary in case of some missing language, and you chose in this case English as a default. Listing 6-15 shows your Default English dictionary file. Listing 6-15. dictionary.properties. Default English Dictionary main.title=Welcome to My Documents main.menu.1=Show all My Documents main.menu.2=Show all Type of Documents main.menu.3=Search by Type main.menu.4=Quit login.success=This user is authorized login.failure=WARNING! This user is not authorized! Listing 6-16 shows your English dictionary file. Listing 6-16. dictionary_en.properties. English locale _en main.title=Welcome to My Documents main.menu.1=Show all My Documents main.menu.2=Show all Type of Documents main.menu.3=Search by Type main.menu.4=Quit login.success=This user is authorized login.failure=WARNING! This user is not authorized! Listing 6-17 shows the Spanish Dictionary file. Listing 6-17. dictionary_es.properties. Spanish local _es main.title=Bienvenido a Mis Documentos main.menu.1=Mostrar todos Mis Documentos main.menu.2=Mostrar Tipos de Documentos main.menu.3=Buscar por Tipo main.menu.4=Salir login.success=Usuario Autorizado login.failure=Alerta! Este usuario no esta autorizado! 71

Chapter 6 ■ Using Resource Files Next, let’s create your XML file configuration and see how you are going to define and use your dictionaries. See Listing 6-18. Listing 6-18. mydocuments-i18n-context.xml In Listing 6-18, note that the property to use is basename; this will accept a fully qualified name with the extension properties. In this case, it will look up the dictionary files. Remember that you have dictionary.properties, dictionary_en.properties, and dictionary_es.properties. The dictionary without the underscore character is the default dictionary. The _en means the English dictionary and the _es means the Spanish dictionary. Now let’s create your unit test as shown in Listing 6-19. Listing 6-19. MyDocumentsI18nTest.java package com.apress.isf.spring.test; import static java.lang.System.out; import java.util.Locale; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyDocumentsI18nTest { private static final Logger log = LoggerFactory.getLogger(MyDocumentsI18nTest.class); private ClassPathXmlApplicationContext context; @Before public void setup(){ context = new ClassPathXmlApplicationContext ("META-INF/spring/mydocuments-i18n-context.xml"); } @Test public void testMenu() { log.debug("About to Translate..."); 72

Using a Different Language: Do You Speak Spanish?<br />

Chapter 6 ■ Using Resource Files<br />

As a developer, we want everybody to use our application, but what happens when the spoken language is a barrier?<br />

You need to start thinking about how to internationalize your application. In other words, your application needs to<br />

support different languages. For example, major banks offer their customers the option of changing the language for<br />

their entire web site. So what happens if your <strong>Spring</strong> application, My Documents, has a Spanish-speaking user? Is this<br />

person able to understand English?<br />

The <strong>Spring</strong> <strong>Framework</strong> has some support classes for this purpose. The ResourceBundleMessageSource class<br />

allows reading different locales and retrieving the correct message specifying its key based on the properties file.<br />

In order to use this class, it is necessary to have a default properties file with any name, and the locales followed<br />

by an underscore and the locale. In your application, you are going to put English as your default language and<br />

Spanish as the secondary language. Let’s create the files. You need to create three files, two for English and one for<br />

Spanish. It’s always necessary to have a default dictionary in case of some missing language, and you chose in this<br />

case English as a default. Listing 6-15 shows your Default English dictionary file.<br />

Listing 6-15. dictionary.properties. Default English Dictionary<br />

main.title=Welcome to My Documents<br />

main.menu.1=Show all My Documents<br />

main.menu.2=Show all Type of Documents<br />

main.menu.3=Search by Type<br />

main.menu.4=Quit<br />

login.success=This user is authorized<br />

login.failure=WARNING! This user is not authorized!<br />

Listing 6-16 shows your English dictionary file.<br />

Listing 6-16. dictionary_en.properties. English locale _en<br />

main.title=Welcome to My Documents<br />

main.menu.1=Show all My Documents<br />

main.menu.2=Show all Type of Documents<br />

main.menu.3=Search by Type<br />

main.menu.4=Quit<br />

login.success=This user is authorized<br />

login.failure=WARNING! This user is not authorized!<br />

Listing 6-17 shows the Spanish Dictionary file.<br />

Listing 6-17. dictionary_es.properties. Spanish local _es<br />

main.title=Bienvenido a Mis Documentos<br />

main.menu.1=Mostrar todos Mis Documentos<br />

main.menu.2=Mostrar Tipos de Documentos<br />

main.menu.3=Buscar por Tipo<br />

main.menu.4=Salir<br />

login.success=Usuario Autorizado<br />

login.failure=Alerta! Este usuario no esta autorizado!<br />

71

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!