4

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

3

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) {
     .......
});
| improve this answer | |
  • 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 Aug 26 '14 at 18:17
  • 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 Poirier Jan 4 '17 at 9:13
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 %}
            <li>{{ error | t }}</li>
        {% endfor %}

Salud ;)

| improve this answer | |
  • 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 Aug 20 '14 at 9:27
  • 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 Aug 20 '14 at 17:17
  • See the alternative answer added below, s'il vous plait... – narration_sd Aug 25 '14 at 6:56
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.

| improve this answer | |
  • 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 Aug 25 '14 at 14:21
  • 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 Aug 25 '14 at 23:01
  • 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 Aug 26 '14 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.