Answers.Body: <p>I just ran into the same problem.</p>
  
  <p>In the defineAttributes() method in your model, you can set a "label", like so:</p>
  
  <pre><code>class Plugin_ExampleModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'quality'  =&gt; array(
                  AttributeType::Number,
                  'label' =&gt; Craft::t('Quality'),
                  'min' =&gt; 0,
                  'max' =&gt; 100,
                  'required' =&gt; TRUE
              )
          );
      }
  }
  </code></pre>
  
  <p>By using <strong>Craft::t()</strong> on that label, it will be translated in your template when the error is shown. Of course the corresponding label should be present in your plugin's translations file.</p>
  
  <p>Hope this will help!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-19T13:52:01.763Z
Answers.Text: Definitely helps, thanks! I'm validating my record, so I had to add it to my record attributes.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-19T13:01:20.520Z
Answers.LastActivityDate: 2015-05-19T13:01:20.520Z
Answers.UserDisplayName: 24creative
Answers.Body: <p>Craft/Yii sees that string as:</p>
  
  <pre><code>'{attribute} cannot be blank.' =&gt; '{attribute} darf nicht leer sein.',
  </code></pre>
  
  <p>Where <code>{attribute}</code> is the dynamic part - in this case your field handle.</p>
  
  <p>If I'm understanding your question correctly, there isn't a way to translate that field handle in this context because you're in charge of what it's called to begin with.</p>
  
  <p>I would assume, for example, if you're using the German locale, that you would already have a German context field handle name in place already.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T14:40:54.013Z
Answers.Text: Yes. It's a plugin but its not a custom error message that I can translate but I think something coming from Yii?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T14:42:41.350Z
Answers.Text: Right... Yii won't know how to translate 'myField' ahead of time, though. By adding this to your plugin, you're effectively overriding Yii's translation with your own with 'myField' translated.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T14:52:51.023Z
Answers.Text: Sounds good, Brad. But it doesn't work, I got everything else translated but having no luck with the error messages.  :(
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T15:28:11.547Z
Answers.Text: I just noticed that except element fields non / few of the core field names are being translated either within validation error messages. I checked widgets, users and usergroups.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T18:05:09.907Z
Answers.Text: Sorry, now that I've had coffee and re-read your question, I've updated my answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-14T17:17:01.420Z
Answers.Text: Brad, it's not a field type I'm talking about. Or what do you mean with field handle from the German context? It's for a CP input form that I'm validating with `$record->getErrors()`.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-14T17:18:39.823Z
Answers.Text: I was just assuming this was a field handle because that's commonly where you see that error message, but it can be applied anywhere that string is used (settings validation, etc.).
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-14T17:35:47.587Z
Answers.Text: Brad, sorry but I not really get what you're explaining to me. Added the code that "magically" generates the error messages for me. What can I do to have the field's name being translated so that the message is "Mein Feld darf nicht leer sein." instead of "My Field darf nicht leer sein."? Possible?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-14T17:42:13.197Z
Answers.Text: What I mentioned yesterday was that I noticed that some core features display these error messages un-translated as well. When I leave the "Benutzername" field blank in CP → Users → New User the error message reads "Username darf nicht leer sein."!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-14T18:10:52.613Z
Answers.Text: Can you send a database dump along with the plugin over to support@buildwithcraft.com so we can look into it?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-19T13:53:17.853Z
Answers.Text: The other answer solved it for me, Brad. Anyways, thank for help!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-19T15:26:56.870Z
Answers.Text: Hah... goes to show I have no idea what I'm talking about.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T14:38:56.987Z
Answers.LastActivityDate: 2015-05-13T18:05:30.303Z
Answers.LastEditDate: 2015-05-13T18:05:30.303Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm adding validation errors to fields using <code>addErrors($record-&gt;getErrors())</code> which then read like e.g. "My Field cannot be blank."</p>
  
  <p>But I'm wondering where I can translate the field name, which I think is generated from the ID attribute <code>myField</code>?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Template:</p>
  
  <pre><code>&lt;input type="hidden" name="action" value="example/saveExample"&gt;
  
  {{ forms.textField({
      label: 'My Field'|t,
      id: 'myField',
      name: 'myField',
      value: null,
      errors: example is defined ? example.errors('myField'),
  }) }}
  </code></pre>
  
  <p>Service function called from <code>actionSaveExample</code>:</p>
  
  <pre><code>public function saveExample(ExampleModel $example)
  {
      $exampleRecord = ExampleRecord::model()-&gt;findById($example-&gt;id);
      $exampleRecord-&gt;myField = $example-&gt;myField;
  
      if ($exampleRecord-&gt;validate())
      {
          // save
      }
      else
      {
          $rule-&gt;addErrors($ruleRecord-&gt;getErrors());
  
          return false;
      }
  }
  </code></pre>
  
Tags: <plugin-development><error-message><validation>
Title: Translate field name displayed within validation error messages?
ThreadText: # Translate field name displayed within validation error messages?
  
  (Asked by: username_0 on 2015-05-13)
  
  I'm adding validation errors to fields using `addErrors($record->getErrors())` which then read like e.g. "My Field cannot be blank."
  
  But I'm wondering where I can translate the field name, which I think is generated from the ID attribute `myField`?
  
  **Update:**
  
  Template:
  
  ```
  <input type="hidden" name="action" value="example/saveExample">
  
  {{ forms.textField({
      label: 'My Field'|t,
      id: 'myField',
      name: 'myField',
      value: null,
      errors: example is defined ? example.errors('myField'),
  }) }}
  
  ```
  
  Service function called from `actionSaveExample`:
  
  ```
  public function saveExample(ExampleModel $example)
  {
      $exampleRecord = ExampleRecord::model()->findById($example->id);
      $exampleRecord->myField = $example->myField;
  
      if ($exampleRecord->validate())
      {
          // save
      }
      else
      {
          $rule->addErrors($ruleRecord->getErrors());
  
          return false;
      }
  }
  
  ```
  
  **Tags:** plugin-development, error-message, validation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-05-19. Score: 1 | Accepted answer
  
  I just ran into the same problem.
  
  In the defineAttributes() method in your model, you can set a "label", like so:
  
  ```
  class Plugin_ExampleModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'quality'  => array(
                  AttributeType::Number,
                  'label' => Craft::t('Quality'),
                  'min' => 0,
                  'max' => 100,
                  'required' => TRUE
              )
          );
      }
  }
  
  ```
  
  By using **Craft::t()** on that label, it will be translated in your template when the error is shown. Of course the corresponding label should be present in your plugin's translations file.
  
  Hope this will help!
  
  > **Comments:**
  > **username_0**: Definitely helps, thanks! I'm validating my record, so I had to add it to my record attributes.  
  
  ---
  
  ### Answer by username_2 on 2015-05-13. Score: 1
  
  Craft/Yii sees that string as:
  
  ```
  '{attribute} cannot be blank.' => '{attribute} darf nicht leer sein.',
  
  ```
  
  Where `{attribute}` is the dynamic part - in this case your field handle.
  
  If I'm understanding your question correctly, there isn't a way to translate that field handle in this context because you're in charge of what it's called to begin with.
  
  I would assume, for example, if you're using the German locale, that you would already have a German context field handle name in place already.
  
  > **Comments:**
  > **username_0**: Yes. It's a plugin but its not a custom error message that I can translate but I think something coming from Yii?  
  > **username_2**: Right... Yii won't know how to translate 'myField' ahead of time, though. By adding this to your plugin, you're effectively overriding Yii's translation with your own with 'myField' translated.  
  > **username_0**: Sounds good, username_2. But it doesn't work, I got everything else translated but having no luck with the error messages.  :(  
  > **username_0**: I just noticed that except element fields non / few of the core field names are being translated either within validation error messages. I checked widgets, users and usergroups.  
  > **username_2**: Sorry, now that I've had coffee and re-read your question, I've updated my answer.  
  > **username_0**: username_2, it's not a field type I'm talking about. Or what do you mean with field handle from the German context? It's for a CP input form that I'm validating with `$record->getErrors()`.  
  > **username_2**: I was just assuming this was a field handle because that's commonly where you see that error message, but it can be applied anywhere that string is used (settings validation, etc.).  
  > **username_0**: username_2, sorry but I not really get what you're explaining to me. Added the code that "magically" generates the error messages for me. What can I do to have the field's name being translated so that the message is "Mein Feld darf nicht leer sein." instead of "My Field darf nicht leer sein."? Possible?  
  > **username_0**: What I mentioned yesterday was that I noticed that some core features display these error messages un-translated as well. When I leave the "Benutzername" field blank in CP → Users → New User the error message reads "Username darf nicht leer sein."!  
  > **username_2**: Can you send a database dump along with the plugin over to support@buildwithcraft.com so we can look into it?  
  > **username_0**: The other answer solved it for me, username_2. Anyways, thank for help!  
  > **username_2**: Hah... goes to show I have no idea what I'm talking about.  
  
  ---
  
  