Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 23 and 24: Chapter 2 ■ Working with Classes
- Page 25 and 26: Chapter 2 ■ Working with Classes
- Page 27 and 28: Chapter 2 ■ Working with Classes
- Page 29 and 30: Chapter 3 Applying Different Config
- Page 31 and 32: Chapter 3 ■ Applying Different Co
- Page 33 and 34: Chapter 3 ■ Applying Different Co
- Page 35 and 36: Chapter 3 ■ Applying Different Co
- Page 37 and 38: Chapter 3 ■ Applying Different Co
- Page 39 and 40: Chapter 3 ■ Applying Different Co
- Page 41 and 42: Spring Stereotypes Chapter 3 ■ Ap
- Page 43 and 44: Chapter 3 ■ Applying Different Co
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
- Page 73: Chapter 6 ■ Using Resource Files
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
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