Using the Message Catalog | KEVIN RESCHENBERG 09-26-2005 |
Last week's list of
"good ideas
that sometimes aren't" included the message
catalog. Today I'd like to expand on a few points
related to using message catalog entries.
There are three very good reasons to use them. First, if
you are building a page containing a long section of text
(an explanation or instructions, for example), there
is no easy way to add it directly to the page. The
text object is very limited. But with a message catalog
entry, you can place all of the text in one object and
it will wrap appropriately on the page. You could use
an HTML area instead, but the message catalog method is
easy and effective.
The second important use for the message catalog is in
multiple-language environments. If you support multiple
languages, it's an easy decision—use it.
The third reason to use the message catalog is that
the Warning and Error functions in PeopleCode have a
strange quirk. If you code, for example,
Error "Widget code cannot be X";
then the error message the user sees will probably
be something like "Widget code cannot be X Return
to the previous level." The mysterious and meaningless "Return to the
previous level" is most commonly seen, but it could be
replaced by any other random text or even gibberish.
To avoid this, you must use a message catalog
item:
Error MsgGet(123, 456, "Message not found");
But I have to say that the message catalog annoys me
sometimes. Yes, it makes it easy to change text if
necessary. But how do you know which PeopleCode programs
or which pages are using the entry unless you do a
full search? Will changing the text change the alignment
of a page? Wouldn't it be better to place the text
directly into your error-checking
code?
The example shown above is very common in
PeopleCode. Unfortunately, there is no way to know what
message "123, 456" is without looking it up. If that entry
happens to have been deleted or not migrated, your error
message will be a not-so-helpful "Message not found."
And I've run across plenty of examples of "MESSAGE NOT FOUND"
in production systems—even on self service pages.
Of course, we can and should comment our code. Placing the
text of a message in a comment is certainly helpful. But if the message
is changed but the comment isn't...
I've seen a clever kludge in which the programmer specifies
an entry that doesn't exist and then uses the default
message parameter to specify the real message:
Error MsgGet(20000, 9999, "Widget code cannot be X");
Of course, this method cannot be used if you need the
multiple-language capability. Also, you must concatenate
strings together to create the message if it is to
contain variable sections. You can't use the
text-replacement functionality of MsgGet. Otherwise, I sort of
like it. Unfortunately, it's far from politically
correct and not something I'd ever officially encourage!
Just a couple of other points to wrap up this topic.
First, you may see "message not found" when the message
actually exists, or your changes to a message might not
be reflected on a page. For some reason, message catalog
objects sometimes seem to "take" very slowly, if at all, in the
cache. Clear the cache or wait. It's a fairly small
annoyance, unless you're really under a tight deadline.
Second, if you are creating new messages, be sure to
use a message set number of 20000 or higher in order to keep
them separated from delivered messages. This will help
you during the next upgrade.
|