Documente Academic
Documente Profesional
Documente Cultură
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:
Classfiles
the code you can use to create a classfile, serving the same purpose as
the property file:
Property files essentially isolate all of the GUI elements from the JSP
pages.
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:
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
Locale locale=null;
if (lang.equals("German"))
locale=Locale.GERMANY;
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:
Or simply:
day = resource.getString("day");
}
...
return (String)(resources.get(key));
Example
<%
String sday;
String message;
String choice;
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" />
</body>
</html>