Horstmann, Ed 11, Vol I, chapter 7.5
Horstmann, Ed 11, Vol II, chapter 7
2. Additional Pointers
If you haven’t read the Horstmann book but you need an introduction into Internationalization, read this tutorial from DZONE here. Make sure to read the bit about Resource Bundles, as you use them in the exercise for this week.
Exercise 1: In the pub (Spanish beer)
In the pub Spanish
For this exercise, you are expanding on the pub exercise again. This time, enums and exceptions are all implemented and tested. The focus this week is on Internationalization! We want to make sure that the pub can run in different languages.
Open the project folder in Netbeans. Let’s first look at the resources of the project. You will notice a bunch of new files in the resources folder. The files contain the translations of error messages into Dutch, German and Spanish. For example, the
inthepub_nl_NL.properties file contains
You\ drank\ too\ much=U hebt te veel gedronken You\ are\ too\ young\ to\ drink=Je bent te jong voor drank We\ are\ sold\ out=Wij zijn uitverkocht
So when the pub raises an
EmptyStockException and the
Locale is Dutch, it should display the Dutch error message, not the English one. You don’t need to touch the language properties files, unless you want to add more languages (or perhaps our Spanish is too horrible!).
In this exercise you work on three files:
LanguageTest.java, which is your main file for testing your implementation. Remember, work test-driven! What you should check is that the Exceptions are thrown with a message in the correct language.
PubException.java, this is a new Exception that all other Exceptions inherit. It is the one responsible for translating the error messages. Your implementation goes here.
HablasEspañol.java, which provides a
mainmethod for you to play around with the languages. Feel free to change this code to your liking to familiarize yourself with Internationalization, it is not tested.
If you want to run the program, you can use the
run.sh file that we provided in the project’s repository. Simply execute it to test the pub simulation. Alternatively, simply type
java -Duser.language=nl -Duser.country=NL -cp target/classes/ pub.ParlezEspañol in your command line interface. Of course, this will throw errors as long as you haven’t written your implementation!
Where should I start?
Start with the
PubException.java class and implement the constructor so that the compilor errors will go away. Then, move to
LanguageTest.java and work on the rest of the exercise, first writing the tests and then implementing in the
There are some inconsistencies in the property bundle files. The keys should be the same and they are not.
The following keys are to be used. Please adapt the property files and or the test and source code where appropriate.
hello GoodMorning You\ drank\ too\ much You\ are\ too\ young\ to\ drink We\ are\ sold\ out
beer\ is\ sold\ out is wrong.
The key and values are actually missing from the default properties as well.
hello=Hello GoodMorning=GoodMorning You\ drank\ too\ much=You drank too much You\ are\ too\ young\ to\ drink=You are too young to drink We\ are\ sold\ out=We are sold out
@echo off rem @author David Greven - https://github.com/grevend if not exist target/classes call mvn clean compile chcp 65001 cls java -Duser.language=nl -Duser.country=NL -cp target/classes/ pub.HablasEspañol java -Duser.language=es -Duser.country=ES -cp target/classes/ pub.HablasEspañol pause