Sie sind auf Seite 1von 5

Localisation

Base class java.util.ResourceBundle for handling resources,

Subclasses ListResourceBundle and PropertyResourceBundle.

The ListResourceBundle and PropertyResourceBundle Subclasses

A PropertyResourceBundle is backed by a properties file. A properties file


is a plain-text file that contains translatable text. Properties files are not
part of the Java source code, and they can contain values for String
objects only. If you need to store other types of objects, use a
ListResourceBundle instead. The section Backing a ResourceBundle with
Properties Files shows you how to use a PropertyResourceBundle.
The ListResourceBundle class manages resources with a convenient list.
Each ListResourceBundle is backed by a class file. You can store any
locale-specific object in a ListResourceBundle. To add support for an
additional Locale, you create another source file and compile it into a class
file. The section Using a ListResource Bundle has a coding example you
may find helpful.
The ResourceBundle class is flexible. If you first put your locale-specific
String objects in a PropertyResourceBundle and then later decided to use
ListResourceBundle instead, there is no impact on your code. For example,
the following call to getBundle will retrieve a ResourceBundle for the
appropriate Locale, whether ButtonLabel is backed up by a class or by a
properties file:

ResourceBundle introLabels =
ResourceBundle.getBundle("ButtonLabel", currentLocale);

In general:

getBundle iterates over the candidate bundle names to find the first
one for which it can instantiate an actual resource bundle. For each
candidate bundle name, it attempts to create a resource bundle:

• First, it attempts to load a class using the candidate bundle


name. If such a class can be found and loaded using the
specified class loader, is assignment compatible with
ResourceBundle, is accessible from ResourceBundle, and can
be instantiated, getBundle creates a new instance of this class
and uses it as the result resource bundle.
• Otherwise, getBundle attempts to locate a property resource
file. It generates a path name from the candidate bundle
name by replacing all "." characters with "/" and appending
the string ".properties". It attempts to find a "resource" with
this name using ClassLoader.getResource. (Note that a "resource"
in the sense of getResource has nothing to do with the contents
of a resource bundle, it is just a container of data, such as a
file.) If it finds a "resource", it attempts to create a new
PropertyResourceBundle instance from its contents. If successful,
this instance becomes the result resource bundle.

Classfiles
the code you can use to create a classfile, serving the same purpose as
the property file:

public class Message_fr_FR extends ListResourceBundle {


public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"main.title", "Musique sans frontières"},
{"main.subhead", "Sons du village global"},
{"main.addLabel","Ajouter"},
. . .
{"cd.quantityLabel","Quantité"}
}
}

Property files essentially isolate all of the GUI elements from the JSP
pages.

Ensure the ???.properties file exists in the following folder:


WebApp\web\WEB-INF\classes\???.properties

The property files for bundles must follow some standard naming
conventions. The suffix must always be properties. Also, since a locale is
typically defined in Java via a country code and/or a language code and an
optional variant, the name of the bundle has to correlate with the locale it
serves. For instance, all of the following bundle prefixes are valid:

BundleName + "_" + localeLanguage + "_" + localeCountry + "_" +


localeVariant
BundleName + "_" + localeLanguage + "_" + localeCountry
BundleName + "_" + localeLanguage
BundleName

the literals used to identify resources remain the same within the bundles
for all locales; only the values change for the corresponding locale.

helloproperties_el_GR.properties:

main.message=καλωσηρθατε
main.choose=Διαλεξτε γλωσσα
eng=Αγγλικα
helloproperties_en_US.properties:
main.message=Welcome
main.choose= Pick a language
eng=English

• the following establishes the locale for the Web application:

Locale locale=null;
if (lang.equals("German"))
locale=Locale.GERMANY;

When you do not have a predefined constant for a country,


observe that its Locale instance is instantiated by explicitly
passing the language and country code as:

locale=new Locale("el","GR");//Greek

default Locale:

local = Locale.getDefault();

Once the locale has been established, you can retrieve its corresponding
bundle:

ResourceBundle bundle =
ResourceBundle.getBundle("Message", locale);

After retrieving the bundle, you obtain the resources for the locale along
with their associated values and place them into the session as:

for (Enumeration e = bundle.getKeys();e.hasMoreElements();)


{
String key = (String)e.nextElement();
String s = bundle.getString(key);
session.putValue(key,s);
}

Or simply:

day = resource.getString("day");

Or use a hashtable to organize resources to make searching easier:

Hashtable resources = new Hashtable();


ResourceBundle bundle = ResourceBundle.getBundle(file, loc);
for (Enumeration e = bundle.getKeys();e.hasMoreElements();)
{
String key = (String)e.nextElement();
String s = bundle.getString(key);
resources.put(key,s);

}
...

return (String)(resources.get(key));

Example

<%
String sday;
String message;
String choice;

Locale local = null;


String lang = "";

try {
lang = request.getParameter("language");
} catch (Exception e) {
e.printStackTrace();
}

if (lang == null) {
local = new Locale("el", "GR");
} else {
if (lang.equals("English")) {
local = Locale.getDefault();
} else if (lang.equals("Greek")) {
local = new Locale("el", "GR");
}
}
ResourceBundle resource =
ResourceBundle.getBundle("helloproperties", local);
sday = resource.getString("day");
message = resource.getString("main.message");
choice = resource.getString("main.choose");
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
<title><%out.println(message);%></title>
</head>
<body>

<h1><%out.println(sday);%></h1>
<form action="index.jsp" method="post">
<p><%out.println(choice);%></p>

<%out.println(resource.getString("eng"));%><input type="radio"
name="language" value="English" /><br />
<%out.println(resource.getString("grk"));%><input type="radio"
name="language" value="Greek" />

<input type="submit" value="ok" name="lang" />


</form>

</body>
</html>

Das könnte Ihnen auch gefallen