<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>

CC BY-SA 3.0
2015-05-19T13:52:01.763Z
Definitely helps, thanks! I'm validating my record, so I had to add it to my record attributes.
carlcs
CC BY-SA 3.0
2015-05-19T13:01:20.520Z
2015-05-19T13:01:20.520Z
24creative
<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>

CC BY-SA 3.0
2015-05-13T14:40:54.013Z
Yes. It's a plugin but its not a custom error message that I can translate but I think something coming from Yii?
carlcs
CC BY-SA 3.0
2015-05-13T14:42:41.350Z
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.
Brad Bell
CC BY-SA 3.0
2015-05-13T14:52:51.023Z
Sounds good, Brad. But it doesn't work, I got everything else translated but having no luck with the error messages.  :(
carlcs
CC BY-SA 3.0
2015-05-13T15:28:11.547Z
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.
carlcs
CC BY-SA 3.0
2015-05-13T18:05:09.907Z
Sorry, now that I've had coffee and re-read your question, I've updated my answer.
Brad Bell
CC BY-SA 3.0
2015-05-14T17:17:01.420Z
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()`.
carlcs
CC BY-SA 3.0
2015-05-14T17:18:39.823Z
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.).
Brad Bell
CC BY-SA 3.0
2015-05-14T17:35:47.587Z
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?
carlcs
CC BY-SA 3.0
2015-05-14T17:42:13.197Z
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."!
carlcs
CC BY-SA 3.0
2015-05-14T18:10:52.613Z
Can you send a database dump along with the plugin over to support@buildwithcraft.com so we can look into it?
Brad Bell
CC BY-SA 3.0
2015-05-19T13:53:17.853Z
The other answer solved it for me, Brad. Anyways, thank for help!
carlcs
CC BY-SA 3.0
2015-05-19T15:26:56.870Z
Hah... goes to show I have no idea what I'm talking about.
Brad Bell
CC BY-SA 3.0
2015-05-13T14:38:56.987Z
2015-05-13T18:05:30.303Z
2015-05-13T18:05:30.303Z
Brad Bell
<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>

<plugin-development><error-message><validation>
Translate field name displayed within validation error messages?
# 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:


{{ 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, Brad. 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**: 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()`.  
> **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**: 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?  
> **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, Brad. Anyways, thank for help!  
> **username_2**: Hah... goes to show I have no idea what I'm talking about.  

---

