12
$\begingroup$

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.

$\endgroup$

1 Answer 1

19
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ I said nothing, I was confused as to how all these works. Big +1 for making me learn something new $\endgroup$
    – Rojo
    Feb 23, 2013 at 14:44
  • $\begingroup$ @Rojo I learned something new, too, so it's a win for everybody. $\endgroup$
    – rcollyer
    Feb 23, 2013 at 14:47
  • $\begingroup$ @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. $\endgroup$
    – rcollyer
    Oct 18, 2013 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.