Friendly reminder: Foreman exceptions and i18n


there are two “magic” exceptions in the Foreman codebase: Foreman::Exception and Foreman::WrappedException. Both are generic exceptions with “ERF” error code generation embedded and both are i18n-friendly. Use the following style to properly format messages:

raise"Localized error message"))
raise"Localized error message: %s", substring_message))
raise"Localized error message %{a} and %{b}", {:a => a, :b => b}))
raise, N_("Localized error message"))

Note this is incorrect (percent vs comma interpolation):

raise"Localized error message: %s") % substring_message)

It is important to use N_ and not the simple _ (underscore) function and to avoid using Ruby string interpolation, because those exceptions prints out error code which are generated from exception class names and main (untranslated) messages. There is also a rake task that goes through our codebase and generates list of all possible error codes which we keep on the wiki page ErrorCodes.

If an exception is needed to be untranslated, it can be used without underscore functions. Beware, that parameters can be also passed, but Ruby only support hash interpolation:

raise"This works fine")
raise"This works fine %{foo}", {:foo => "too"})
raise"But this %{s} will not work", 'example')

TL;DR - To get your error code right, interpolate correctly.

Maybe we could write a Hound rule and run this via We would probably find couple of other things :slight_smile:

1 Like

For more visit: Translating - Foreman