JavaSercer Pages (Hosting tomcat) The languages are listed in order
JavaSercer Pages The languages are listed in order of preference, with each language represented either by just the language code or by the language code and country code separated by a dash (-). This example header specifies the first choice as U.S. English, followed by any type of English, and finally Swedish. The HTTP specification allows an alternative to listing the codes in order of preference, namely adding a so-called q-value to each code. The q-value is a value between 0.0 and 1.0 indicating the relative preference between the codes. Very few browsers, if any, use this alternative today, however. The Accept-Language header helps you localize your application. You could write code that reads this header and creates the corresponding Locale instances. The good news is you don’t have to do this yourself; the servlet container takes care of it for you and makes the locale information available through two methods on the implicit request object: java.util.Locale preferredLocale = request.getLocale( ); java.util.Enumeration allLocales = request.getLocales( ); The getLocale( ) method returns the Locale with the highest preference ranking, and the getLocales( ) method returns an Enumeration of all locales in order of preference. All you have to do is match the preferred locales to the ones that your web application supports. The easiest way to do this is to loop through the preferred locales and stop when you find a match. As you will see later, the custom actions developed for this book relieve you of all of this, but now you know how it’s done. 11.1.2 Formatting Numbers and Dates Let’s look at how a locale can be used. One thing that we who live on this planet have a hard time agreeing upon is how to write dates and numbers. The order of the month, the day, and the year; if the numeric value or the name should be used for the month; what character to use to separate the fractional part of a number: all of these details differ between countries, even between countries that speak the same language. And even though these details may seem picky, using the wrong format can cause a great deal of confusion. For instance, if you ask for something to be done by 5/2, an American thinks you mean May 2 while a Swede believes that it’s due by February 5. Java provides two main classes to deal with formatting of numbers and dates for a specific locale, appropriately named java.text.NumberFormat and java.text.DateFormat, respectively. The NumberFormat class was used in Chapter 9, to format the price information for items in a shopping cart according to the customs of the country where the server is located. By default, the NumberFormat class uses the locale of the underlying operating system. If used on a server configured to use a U.S. English locale, it formats numbers according to American customs; on a server configured with an Italian locale, it formats them according to Italian customs, and so forth. But you can also explicitly specify the locale to format numbers according to the rules for locales other than the one used by the operating system: java.util.Locale locale = request.getLocale( ); java.text.NumberFormat nf = java.text.NumberFormat.getNumberInstance(locale); String localNumber = nf.format(10000.00); This piece of code creates a String with the number 10000.00 formatted according to the locale that corresponds to the preferred language specified by the Accept-Language header in a request. Besides the getNumberInstance( ) method, you can use the getPercentInstance( ) and the getCurrency-Instance( ) to format a decimal number as a percentage string or any number as a currency string. The DateFormat class works basically the same way, but how dates are written differs a lot more between locales than numbers do, since the day and month names are sometimes spelled out in the local language. Besides the locale, a formatting style is also specified as one of DEFAULT, SHORT, MEDIUM, LONG, or FULL: java.util.Locale locale = request.getLocale( ); java.text.DateFormat df = java.text.DateFormat.getDateInstance(df.SHORT, locale); String localDate = df.format(new java.util.Date( )); If the current date is May 2, 2000, this code formats the date as 5/2/00 with an American locale and as 2000-05-02 with a Swedish locale. If you use the FULL formatting style, the results are Tuesday, May 2, 2000 and den 2 maj 2000 instead. As with the NumberFormat class, there are other specialized date formatters besides the one used here. You can use the getDateTimeInstance( ) and getTimeInstance( ) methods to produce strings including both the date and time or just the time. page 150
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services