Translating Error Messages

If you need to translate your Form error messages, currently, you will need to use static translations (opens new window).

Let's look at a brief overview of this method in the context of the Form plugin.

  1. Wrap your error messages in the translate filter
{% if errors -%}
    <ul class="errors">
        {% for error in errors %}
            <li>{{ error|t('site') }}</li>
        {% endfor %}
    </ul>
{% endif -%}
  1. Add a translations folder to your project


 



config 
storage
translations
vendor
web
  1. Add a locale-specific translation file to your project

Add a file to the craft/translations folder for the locale you wish to provide translations.

craft/translations/en_gb.php

In that file, add add an array of the translations you wish to include:

<?php

return array(
    'Email Field cannot be blank' => 
    'Whatever you want to translate the original message to'
);

The first item in the array is the default message that you want to translate. The second item in the array is your translation. You can translate from one language into another, or even within the same language if you want to change the default error messaging.

If you need to translate multiple phrases, you can add multiple phrases to your translation file and separate them by commas:

<?php

return array(
    'Email Field cannot be blank' => 
    'Whatever you want to translate the original message to',
    
    'Phone Field cannot be blank' => 
    'A new error message'
);
Last Updated: 11/23/2023, 10:32:53 PM