Java Reference
In-Depth Information
Remember that we used the
SimpleError
class, such as these messages
in the
LoginActionBean
:
@ValidationMethod
public void
validateUser(ValidationErrors errors) {
User user = userDao.findByUsername(username);
if
(user ==
null
) {
errors.add("username",
new
SimpleError("The primary email was not found."));
}
else if
(!user.getPassword().equals(password)) {
errors.add("password",
new
SimpleError("The password is incorrect."));
}
}
The text is hard-coded in the Java code. To make the text translatable,
we use
LocalizableError
, and pass in the resource bundle key:
@ValidationMethod
public void
validateUser(ValidationErrors errors) {
User user = userDao.findByUsername(username);
if
(user ==
null
) {
errors.add("username",
new
LocalizableError("primaryEmailNotFound"));
}
else if
(!user.getPassword().equals(password)) {
errors.add("password",
new
LocalizableError("passwordIncorrect"));
}
}
This allows us to take the text out of the Java code and move it to
resource bundles, making it ready to be translated. Stripes automati-
cally tries keys with the action bean class name prefix before trying pre-
fixless keys, so we can use the same patterns as with form field labels.
Deciding to use the prefixes, we now have the messages in English
and French in
StripesResources.properties
and
StripesResources_fr.properties
as follows:
stripesbook.action.LoginActionBean.primaryEmailNotFound=\
The primary email was not found.
stripesbook.action.LoginActionBean.passwordIncorrect=\
The password is incorrect.
Search WWH ::
Custom Search