12

Is there a simple, built-in way to retrieve the text of a message, given it's identifier (such as Power::infy)?

The documentation describes how Message does this: it looks for different language versions (Power::infy::langname), then it looks under the symbol name (Power::infy), then it looks under General (General::infy), then it uses $NewMessage ... it's pretty complicated.

Is there any built-in, hidden/undocumented way that will let me retrieve the text of a message in a simple way, without issuing the message?

I see two possibilities:

  1. Reimplement the whole lookup procedure. This is a lot of work.
  2. Implement a special stream and do something like Block[{$Messages = ...}, Message[symb::name]]. Also a lot of work (implementing the stream methods), and I'm not even sure it would work.

Or maybe there is an undocumented way. ?*`*Message* gives plenty of symbols, but it's not clear which may be interesting and what they do.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263

1 Answers1

19

It is actually straightforward. You use Messages[symbol] to get the list, e.g.

Power::infy (* trigger loading the message *)
Messages[Power]
(* {HoldPattern[Power::infy] :> "Infinite expression `1` encountered."} *)

then, as it is a list of replacement rules, you can simply do

Power::infy /. Messages[Power]
(* "Infinite expression `1` encountered." *)

There is a bit of trickery going on here as messages are not loaded until they are used, so on a fresh kernel, Messages[symbol] will return an empty list. However, using it as a set of replacement rules is guaranteed to work because symbol::msg is evaluated first, triggering the loading process before Messages[symbol] is executed.

rcollyer
  • 33,976
  • 7
  • 92
  • 191
  • I said nothing, I was confused as to how all these works. Big +1 for making me learn something new – Rojo Feb 23 '13 at 14:44
  • @Rojo I learned something new, too, so it's a win for everybody. – rcollyer Feb 23 '13 at 14:47
  • @szabolcs it appears there is at least one case where running MessageName[sym, name] /. Messages[sym] does not work. And, no I don't understand why. – rcollyer Oct 18 '13 at 14:39