Web hosting plans - JavaSercer Pages 11.1.3 Using Localized Text Automatic translation

JavaSercer Pages 11.1.3 Using Localized Text Automatic translation of numbers and dates into the local language is a great help. But until automatic translation software is a lot smarter than it is today, you have to translate all the text used in the application yourself. A set of Java classes then helps you pick the right version for a specific locale. The main class for dealing with localized resources (such as text, images, and sounds) is named java.util.ResourceBundle. This class is actually the abstract superclass for the two subclasses that do the real work, ListResourceBundle and PropertyResourceBundle, but it provides methods that let you get an appropriate subclass instance, hiding the details about which subclass actually provides the resources. Details about the difference between these two subclasses are beyond the scope of this java blog. It suffices to say, however, that the ListResourceBundle is overkill for our needs when developing web applications, so we will be using an instance of the PropertyResourceBundle. To learn more about these classes, I suggest glancing at the Java API documentation. A PropertyResourceBundle instance is associated with a named set of localized text resources, where each resource is identified by a key. The keys and their corresponding text strings are stored in a regular text file as key-value pairs: site_name=The Big Corporation Inc. company_logo=/images/logo_en.gif welcome_msg=Hello! Here, three keys, site_name, company_logo, and welcome_msg, have been assigned string values. The key is a string, without spaces or other special characters, and the value is any text. If the value spans more than one line, the line break must be escaped with a backslash character (): multi_line_msg=This text value continues on the next line. The file must use the extension .properties, for instance sitetext.properties, and be located in the class path used by the Java Virtual Machine. In the case of web applications, you should store the file in the application’s WEB-INF/classes directory, since this directory is always included in the class path. When you have created a properties file, you can obtain the text corresponding to a key like this: java.util.Locale locale = request.getLocale( ); java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle(”sitetext”, locale); String msg = bundle.getString(”welcome_msg”); Note that the getBundle( ) method takes two arguments: a Locale argument, the same as the methods for getting number and date formatters; and a bundle name. These arguments are used like this: the method gets the language and country codes from the Locale object and starts looking for a file with a name composed of both the bundle name and the language and country codes. If you pass it a locale for Mexican Spanish, for example, it first looks for a file named sitetext_es_MX.properties, where es is the language code for Spanish and MX is the country code for Mexico. If it can’t find a file with this name, it looks for sitetext_es.properties, ignoring the country code. If there’s still no such file, it uses the file with just the bundle name, sitetext.properties. As you can see, this makes it possible for you to create multiple properties files, each with the text values translated into a specific language for a specific country. In other words, you can create one file for each supported locale. The ResourceBundle ensures that when you ask for a bundle, you get the one that most closely matches the specified locale, or the default bundle if there is no match. We’ll look at an example in detail in the next section. Besides the ResourceBundle class, there’s a class named java.text.MessageFormat that you can use for messages composed of fixed text plus variable values, such as, “An earthquake measuring 6.7 on the Richter scale hit Northridge, CA, on January 17, 1994.” Here, each underlined word represents a variable value. Another class related to localization is the java.text.Collator class, used for localized string comparison and sorting. These classes are less commonly used, so they are not covered in detail here. You can read more about them in the Java API documentation. page 151

Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services

Comments are closed.