<p>Well, the problem was that I was posting my Ajax request to / from all languages, therefore the site replied back with the site main language, which was French in my case.</p>

<p>The following finally works:</p>

<pre><code>// Get the proper url to post to

if ( '{{ craft.i18n.getCurrentLocale() }}' == '{{ craft.i18n.getPrimarySiteLocale() }}' {
     var url = '/';
} else {
     var url = '/{{ craft.i18n.getCurrentLocale() }}/';
}

// Send it to the server

$.post(url, data, function(response) {
     .......
});
</code></pre>

CC BY-SA 3.0
2014-08-26T18:17:09.737Z
Glad you found it, and makes sense. That's one more path through the Locale system, then, to be aware of for everyone.
narration_sd
CC BY-SA 3.0
2017-01-04T09:13:44.530Z
Hi maunoxyd, any idea how to deal with locale when post URL is initially something like 'sproutForms/entries/saveEntry'? Where should I add the current locale following the solution you found? Something like posting to 'nl/sproutForms/entries/saveEntry/' or 'sproutForms/entries/saveEntry/nl/' is not working, whereas '/nl/' posts to rootUrl instead of 'sproutForms/entries/saveEntry'. Thank you!
Romain P.
CC BY-SA 3.0
2014-08-26T13:56:28.893Z
2014-08-26T14:04:52.967Z
2014-08-26T14:04:52.967Z
maunoxyd
<p>There may be a better answer, or one forthcoming, but why don't you try using translation files?</p>

<p>Note that the translations in the file can be any phrase, not just single words, so should fit well here.</p>

<p><a href="http://buildwithcraft.com/help/static-translations" rel="nofollow">Craft doc on translation files</a></p>

<p>I suspect this will work simply if you add the | t within the errorList macro at the to of the example contact form:</p>

<pre><code>        {% for error in errors %}
            &lt;li&gt;{{ error | t }}&lt;/li&gt;
        {% endfor %}
</code></pre>

<p>Salud ;)</p>

CC BY-SA 3.0
2014-08-20T09:27:23.347Z
Well, I'm submitting the form and receiving the errors via Ajax, so Craft won't pick it up.. I can think of a few dirty solutions, but better if Craft would spit the correct lang directly (it has all translations already)..
maunoxyd
CC BY-SA 3.0
2014-08-20T17:17:44.723Z
Good point, for your case. I'd send support@buildwithcraft.com a feature suggestion, with link to this SE question, so they see what's been looked at. Good fortune; think it's one of the extensions that can round out the great Locales basis in Craft. Meanwhile this answer I think can get people going who are not using Ajax - though Ajax making a form interactive, definitely nice. Good for Craft to see that there are people  working at this level, also.
narration_sd
CC BY-SA 3.0
2014-08-25T06:56:20.623Z
See the alternative answer added below, s'il vous plait...
narration_sd
CC BY-SA 3.0
2014-08-20T08:39:17.313Z
2014-08-20T08:39:17.313Z
narration_sd
<p>I'm going to edit this as another answer, so it will get seen - and will removed it as a comment, since it offers a new path.</p>

<p>I started looking at the problem in detail, and tracked the error handling and its translation back into Yii as handler. Translation of Yii stock form error messages should just work.</p>

<p>Then I looked again at your site, and messages are in French, e.g. invalid email, even though the site is presenting in English, which I presume is your problem. I imagine French is the base language of the site, which shows I think that the translation is active for templates. </p>

<p>So maybe your problem is not fully switching to English as far as Yii is concerned.</p>

<p>Are you setting define('CRAFT_LOCALE', 'en_us'); in your /en/index.php file?? In my experience it has to be en_us to match the standard translations. If you set it up as en only, for example, your templates could use your English template versions depending on your other settings choices, but not I think access the stock translations, like these form errors.</p>

<p>Maybe that's your problem??  I don't think Ajax is any part of the problem, as the same (translated) error strings would be passed there as are made available for the template. If you look at the error handler which does Ajax also.</p>

<p>Here's another edit. I'd like to present the relevant portion of a properly configured general.php file for three languages, which successfully presents in-locale Contact Form error messages in English, Russian, or Swedish depending on the operating Locale:</p>

<pre><code>    'environmentVariables' =&gt; array(
       'siteStore' =&gt; '/storage/avNNNNN/www/the_site_html/public/',
       'sitePath' =&gt; 'http://www.thesite.com/',
    ),

    'siteUrl' =&gt; array(
        'en' =&gt; 'http://www.thesite.com/en/',
        'ru' =&gt; 'http://www.thesite.com/ru/',
        'sv' =&gt; 'http://www.thesite.com/sv/',
    ),
</code></pre>

<p>etc..</p>

<p>The magic coding in Craft will properly handle https operation automatically, and this configuration pattern also makes sure that Live View operates properly in Entry edits. Note you match the 'en' 'ru' etc..  to however you've defined locales in Locale CP config screens, not necessarily to more specific variants like en_us.</p>

<p>I use the sitePath and siteStore as {sitePath} for example in Asset definitions, so that their paths will come out correctly on the base site rather than the language variant path. This affects Live View for instance also, makes it work.</p>

<p>Yes, it's all very sensitive, precise, and not yet fully described in doc here, unless they caught up on it in the last update which included much doc. I've asked...and they are very busy producing towards all of need.</p>

CC BY-SA 3.0
2014-08-25T14:21:54.513Z
Well, we're getting closer, unfortunately the messages doesn't get displayed in English when I change to en_us in the index.php file. However, when I change the language ordering in the cp settings they get displayed in the first one listed only (so when I put en as the first, the validations messages are in English, same for nl), even when I access the site in other languages
maunoxyd
CC BY-SA 3.0
2014-08-25T23:01:10.163Z
Wow. Now I really think you need to email Craft support. I am sure they would be very interested to take  this on. May request logs, etc.. Order in Locale settings only affects the default language, so you are still not successfully setting the non-default Locale. Let Brad/Brandon/Andris help. I'm sure they will, and mention this Stack question as they like those...
narration_sd
CC BY-SA 3.0
2014-08-26T05:14:49.870Z
Note I added some further config to end of this answer: to check if you match it. Also, that Contact Form validation message translation is working on a 3-language site for me, latest Craft, so I think the problem likely to be in your settings.
narration_sd
CC BY-SA 3.0
2014-08-25T06:50:40.330Z
2014-08-25T23:21:27.293Z
2014-08-25T23:21:27.293Z
narration_sd
<p>The validations error message get displayed in main language instead of being translated, you can try here (contact form at bottom):
<a href="http://lequartiergeneral.be/en/" rel="nofollow">http://lequartiergeneral.be/en/</a></p>

<p>The form is being displayed and get response via Ajax.</p>

<p>How to display them correctly in each language?</p>

<p>Thanks</p>

<ajax><language><plugin-contactform>
P&T Contact Form plugin: How to display error messages in other languages via Ajax
# P&T Contact Form plugin: How to display error messages in other languages via Ajax

(Asked by: username_0 on 2014-08-19)

The validations error message get displayed in main language instead of being translated, you can try here (contact form at bottom):
<http://lequartiergeneral.be/en/>

The form is being displayed and get response via Ajax.

How to display them correctly in each language?

Thanks

**Tags:** ajax, language, plugin-contactform


---

## 3 answers

### Answer by username_0 on 2014-08-26. Score: 3 | Accepted answer

Well, the problem was that I was posting my Ajax request to / from all languages, therefore the site replied back with the site main language, which was French in my case.
The following finally works:
// Get the proper url to post to

if ( '{{ craft.i18n.getCurrentLocale() }}' == '{{ craft.i18n.getPrimarySiteLocale() }}' {
     var url = '/';
} else {
     var url = '/{{ craft.i18n.getCurrentLocale() }}/';
}

// Send it to the server

$.post(url, data, function(response) {
     .......
});

> **Comments:**
> **username_1**: Glad you found it, and makes sense. That's one more path through the Locale system, then, to be aware of for everyone.  
> **username_2**: Hi username_0, any idea how to deal with locale when post URL is initially something like 'sproutForms/entries/saveEntry'? Where should I add the current locale following the solution you found? Something like posting to 'nl/sproutForms/entries/saveEntry/' or 'sproutForms/entries/saveEntry/nl/' is not working, whereas '/nl/' posts to rootUrl instead of 'sproutForms/entries/saveEntry'. Thank you!  

---

### Answer by username_1 on 2014-08-20. Score: 2

There may be a better answer, or one forthcoming, but why don't you try using translation files?
Note that the translations in the file can be any phrase, not just single words, so should fit well here.
Craft doc on translation files
I suspect this will work simply if you add the | t within the errorList macro at the to of the example contact form:
        {% for error in errors %}
            {{ error | t }}
        {% endfor %}

Salud ;)

> **Comments:**
> **username_0**: Well, I'm submitting the form and receiving the errors via Ajax, so Craft won't pick it up.. I can think of a few dirty solutions, but better if Craft would spit the correct lang directly (it has all translations already)..  
> **username_1**: Good point, for your case. I'd send support@buildwithcraft.com a feature suggestion, with link to this SE question, so they see what's been looked at. Good fortune; think it's one of the extensions that can round out the great Locales basis in Craft. Meanwhile this answer I think can get people going who are not using Ajax - though Ajax making a form interactive, definitely nice. Good for Craft to see that there are people  working at this level, also.  
> **username_1**: See the alternative answer added below, s'il vous plait...  

---

### Answer by username_1 on 2014-08-25. Score: 0

I'm going to edit this as another answer, so it will get seen - and will removed it as a comment, since it offers a new path.
I started looking at the problem in detail, and tracked the error handling and its translation back into Yii as handler. Translation of Yii stock form error messages should just work.
Then I looked again at your site, and messages are in French, e.g. invalid email, even though the site is presenting in English, which I presume is your problem. I imagine French is the base language of the site, which shows I think that the translation is active for templates. 
So maybe your problem is not fully switching to English as far as Yii is concerned.
Are you setting define('CRAFT_LOCALE', 'en_us'); in your /en/index.php file?? In my experience it has to be en_us to match the standard translations. If you set it up as en only, for example, your templates could use your English template versions depending on your other settings choices, but not I think access the stock translations, like these form errors.
Maybe that's your problem??  I don't think Ajax is any part of the problem, as the same (translated) error strings would be passed there as are made available for the template. If you look at the error handler which does Ajax also.
Here's another edit. I'd like to present the relevant portion of a properly configured general.php file for three languages, which successfully presents in-locale Contact Form error messages in English, Russian, or Swedish depending on the operating Locale:
    'environmentVariables' => array(
       'siteStore' => '/storage/avNNNNN/www/the_site_html/public/',
       'sitePath' => 'http://www.thesite.com/',
    ),

    'siteUrl' => array(
        'en' => 'http://www.thesite.com/en/',
        'ru' => 'http://www.thesite.com/ru/',
        'sv' => 'http://www.thesite.com/sv/',
    ),

etc..
The magic coding in Craft will properly handle https operation automatically, and this configuration pattern also makes sure that Live View operates properly in Entry edits. Note you match the 'en' 'ru' etc..  to however you've defined locales in Locale CP config screens, not necessarily to more specific variants like en_us.
I use the sitePath and siteStore as {sitePath} for example in Asset definitions, so that their paths will come out correctly on the base site rather than the language variant path. This affects Live View for instance also, makes it work.
Yes, it's all very sensitive, precise, and not yet fully described in doc here, unless they caught up on it in the last update which included much doc. I've asked...and they are very busy producing towards all of need.

> **Comments:**
> **username_0**: Well, we're getting closer, unfortunately the messages doesn't get displayed in English when I change to en_us in the index.php file. However, when I change the language ordering in the cp settings they get displayed in the first one listed only (so when I put en as the first, the validations messages are in English, same for nl), even when I access the site in other languages  
> **username_1**: Wow. Now I really think you need to email Craft support. I am sure they would be very interested to take  this on. May request logs, etc.. Order in Locale settings only affects the default language, so you are still not successfully setting the non-default Locale. Let Brad/Brandon/Andris help. I'm sure they will, and mention this Stack question as they like those...  
> **username_1**: Note I added some further config to end of this answer: to check if you match it. Also, that Contact Form validation message translation is working on a 3-language site for me, latest Craft, so I think the problem likely to be in your settings.  

---

