Note that there are some explanatory texts on larger screens.

plurals
  1. POInheriting Rails i18n validation error messages in the subclass
    primarykey
    data
    text
    <h2>What I understand</h2> <p>Suppose I have a class with a handy validation like:</p> <pre><code>User &lt; ActiveRecord::Base validates :username, :format =&gt; {/regex/}, :message =&gt; :name_format end </code></pre> <p>In this case, I can use <code>i18n</code> to make the error message translatable, by including the following in my <code>/config/locals/en.yml</code>:</p> <pre><code>en: activerecord: errors: models: user: attributes: username: name_format: 'has the way-wrong format, bro!' </code></pre> <p>This is fine and generally really handy.</p> <h2>What I want to know:</h2> <p>My question is: What happens when I have subclasses that inherit from User:</p> <pre><code>UserSubclassOne &lt; User # extra stuff end UserSubclassTwo &lt; User # extra stuff end ... UserSubclassEnn &lt; User # extra stuff end </code></pre> <p>Now the problem is that Rails can't find the translation <code>user_subclass_one.attributes.username.name_format</code>.</p> <p>It complains: </p> <pre><code>translation missing: en.activerecord.errors.models.user_subclass_one.attributes.username.name_format </code></pre> <p>I'd hope that Rails would look up the hierarchy of <code>UserSubclassOne</code> to <code>User</code> when searching for a string in <code>en.yml</code> and then notice when it gets a 'hit', but (unless I've done something horribly wrong) apparently that doesn't happen. </p> <p>An obvious solution is to duplicate the data in <code>en.yml.en.errors.models</code> for <code>user</code>, <code>user_subclass_one</code>, <code>user_subclass_two</code>, etc, but my Rails-sense tells me that this is deeply wrong.</p> <p>Any ideas, folks?</p> <h2>Potential Complication:</h2> <p><code>User</code> is defined in a gem <code>MyGem</code> that is included in a Rails engine <code>MyEngine</code> that is included in the full-on Rails app <code>MyApp</code> that defines <code>UserSubclassOne</code>, ..., <code>UserSubclassEnn</code>. I don't think this should matter though, since the validations are running in <code>MyGem::User</code>, which is where the <code>en.yml</code> file lives -- just wanted to let people know in case it does.</p> <h1>Ultimate problem/solution:</h1> <p>So it turns out that the problem was namespacing. Recall that <code>MyApp</code> (which defines <code>UserSubclassOne</code>) uses <code>MyGem</code> (which defines <code>User</code>). It turns out <code>User</code> is actually in the namespace <code>MyGem</code> (this is not necessarily always the case), so the full declaration line at the beginning of <code>User</code> is not:</p> <pre><code>User &lt; ActiveRecord::Base </code></pre> <p>but rather</p> <pre><code>MyGem::User &lt; ActiveRecord::Base </code></pre> <p>.</p> <p>When the i18n gem looks up the class hierarchy, it notices this namespace and searches for <code>my_gem/user</code>, rather than simply <code>user</code>, <code>my_gem.user</code>, <code>my_gem: user</code>, etc.</p> <p>Thus I had to change my <code>en.yml</code> file to: <code>/config/locals/en.yml</code>:</p> <pre><code>en: activerecord: errors: models: my_gem/user: attributes: username: name_format: 'has the way-wrong format, bro!' </code></pre> <p>and bingo!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload