I would like to thank Erich Meier for his response and help in resolving
this.
The problem wound up being with how the locales had been stored in the
database. Only the country code had been stored, and not the associate
language or variant, i.e. "US, FR, AR,...". Since the default getMessage()
uses the localeKey() method which, returns a string in the form of
language_country_variant, i.e. (en_US, fr_FR, es_AR, ...), and then appends
this to the key requested, the called failed since the values stored in the
messages HashMap as keys were in the form of country.key. With a quick
change to the database, the default getMessage() works fine.
What a silly mistake.
Sorry for the wasted bandwidth but thought someone else may find this useful
in the future.
Again, thanks Erich for the assistance.
Regards,
Todd G. Nist
-Original Message-
From: Erich Meier
-Original Message-
From: Todd G. Nist [mailto:[EMAIL PROTECTED]]
Sent: mercredi 6 février 2002 09:02
To: [EMAIL PROTECTED]
Subject: PropertyMessageResourcesFactory Question
I have a few questions on the creation of a custom MessageResourcesFactory
which I am hoping someone can help answer. We have a database which
contains most of the translation string for our application, so I would like
to create the MessageResources from here in addition to the standard
ApplicationResource.properties file. I have written my own
PropertyMessageResourcesFactory, modified my web.xml so that Struts would
use this factory, and extended the ActionServlet to call a method to load
the data from the database. I thought this would provide a simple yet
elegant solution because nothing else would need to change for it to work.
My questions:
1. While it appears that my code is adding the content of the tables to
the messages HashMap, when I go to reference them with a I get the following exception:
Missing message for key
MLSBuyer.title' javax.servlet.jsp.JspException: Missing message for
key
MLSBuyer.title at
org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:298) at
org.apache.jsp.CompanyZoomBody$jsp._jspService(CompanyZoomBody$jsp.java:84)
at ...
I have not implemented the getMessage(Locale locale, String key) method in
my custom PropertyMessageResources class, I was hoping that the messages
that I created would become part of the default bundle/messages hashmap and
therefore would be available to the Struts frame work. This does no appear
to be what is happening. Is there a specific Servlet context attribute or
some other value which needs to be set for these messages to be included in
the standard bundle? What is the difference between Action.MESSAGE_KEY and
Action.MESSAGES_KEY?
2. What else needs to be done to make these available in the default
bundle so that the Struts frame work can use them without us having to
implement the getMessages() methods? Or can this be done?
3. Are there any major issues/drawbacks with using the database like
this for building the messages HashMap? We will still use the
ApplicationResources.properties file where appropriate, but since the
database already has the other translation information in it, it seems
logical to use it, but I am open for other views on this.
I have attached the MessageResourcesFactory, DBMessageResourcesFactory and
the actual class which is created from within the factory,
BasePropertyMessageResources. Below is the initApplication() method which
we have added to the extended ActionServlet to invoke the method "dbLoad()"
in the BasePropertyMessageResources class.
Thanks in advance.
Todd G. Nist
Email: [EMAIL PROTECTED]
// initApplicationn method of the extended ActionServlet
public void initApplication() throws ServletException {
super.initApplication();
String dataSource =
getServletConfig().getInitParameter("datasource");
MessageResources mr =
(MessageResources)getServletContext().getAttribute(Action.MESSAGES_KEY);
if (mr == null) {
System.out.println("Message Resource is invalid");
return;
}
// load resources from the dataSource
((BasePropertyMessageResources)mr).dbLoad(dataSource);
}