Intercept and handle missing translations in helper
<p>I have a helper function that generates useful HTML markup. My functions are usually called with some parameters, including some text translated with I18n <code>my_helper( t(:translation_symbol) )</code>.</p>

<p>I use I18n <code>t</code> translation helper for that, which (if left untouched) outputs some <code>&lt;span class="translation missing"&gt;...&lt;/span&gt;</code> for texts without translation</p>

<p>I was wondering how to best "intercept" this translation missing so </p>

<ul>
<li>I can render them appropriately in my helpers</li>
<li>Normal behavior outside my helpers</li>
</ul>

<p>In my views I should be able to call both</p>

<pre><code>&lt;%= my_helper(t(:my_text)) %&gt; # My helper needs to handle missing translations
&lt;%= t(:my_text) %&gt; # I want default I18n markup (or a custom one) '&lt;span class="translation missing"&gt;fr.my_text&lt;/span&gt;
</code></pre>

<p>My helper</p>

<pre><code>def my_helper(text=nil)
  # Suppose my_text doesn't have a translation, text will have value 
  # =&gt; '&lt;span class="translation-missing"&gt;fr.my_text&lt;/span&gt;'
  if translation_missing?(text)
    # Code to handle missing translation
    doSomething(remove_translation_missing_markup(text))
    notify_translation_missing
  else
    doSomething(text)
  end
end

def translation_missing?(text)
  # Uses some regex pattern to detect '&lt;span class="translation-missing"&gt;'
end
def remove_translation_missing_markup(text)
  # Uses some regex pattern to extract my_text from '&lt;span class="translation-missing"&gt;fr.my_text&lt;/span&gt;'
end
</code></pre>

<p>Is there a better way around this ? I feel bad about using a dirty regex solution.</p>

<p>EDIT : extra requirements</p>

<ul>
<li>No additional markup on views : I don't want to look at my files individually to add <code>raise: true</code> or <code>default: xxx</code> for every translation. If there is a need to change the behavior everywhere I can override the <code>t</code> method. </li>
<li>In my helpers, I need a convenient way to manipulate, just the translated text if the translation was found, but for missing translations, I need to easily be able to extract the full path of the translation (<code>fr.namespace.translation_symbol</code>), and the original translation symbol (<code>translation_symbol</code>), so I can add myself <code>translation_missing</code> in my custom markup.</li>
</ul>

<p>EDIT2 : I am thinking of something like that</p>

<ul>
<li>Override <code>t</code> helper to <code>rescue I18n::MissingTranslationData =&gt; e</code></li>
<li>If the exception is raised, create and return a custom object that

<ul>
<li>If rendered in a html.erb, will output the usual <code>&lt;span class="translation..."</code></li>
<li>Has useful fields (translation path, translation_string) that I can reuse in my helpers</li>
</ul></li>
</ul>