Make WordPress Contact Form 7 messages multilingual

The Contact Form 7 for WordPress is a nice simple plugin to build contact forms fast. But when it comes to making it completely multilingual it lacks a bit.

I am currently creating a website with 13 different languages, that would mean to create the same contact form 13 times, not really practical :)

I am translating the form labels on the fly with some javascript magic, but for the messages I had to go a bit deeper.

  1. Open wp-content/plugins/contact-form-7/classes.php
  2. Go to line 800 function message
  3. Change this
    1. public function message( $status ) {
    2.                 $messages = $this->messages;
    3.                 $message = isset( $messages[$status] ) ? $messages[$status] : '';
    4.  
    5.                 $message = $this->replace_mail_tags( $message, true );
    6.  
    7.                 return apply_filters( 'wpcf7_display_message', $message, $status);
    8.         }

    to this

    1. public function message( $status ) {
    2.                 $messages = $this->messages;
    3.                 $message = isset( $messages[$status] ) ? $messages[$status] : '';
    4.  
    5.                 $message = $this->replace_mail_tags( $message, true );
    6.  
    7.                 return __(apply_filters( 'wpcf7_display_message', $message, $status ),'textdomain');
    8.         }

    See how I wrapped the gettext function for the return, that does all the magic.

  4. Next we have to make sure that the messages are being found by localization helpers like Codestyling Localization
  5. In your theme functions.php add a translation for each of the Contact 7 Form messages
  6. The first message in the Contact 7 Form admin says: Your message was sent successfully. Thanks.
    1. $contact_form_msgs = array(
    2. 'msg_1' => __('Your message was sent successfully. Thanks.','textdomain'),
    3. 'msg_2' => __('Failed to send your message. Please try later or contact the administrator by another method.','textdomain'),
    4. 'msg_3' => __('Validation errors occurred. Please confirm the fields and submit it again.','textdomain'),
    5. );

     

  7. Do not change these messages in the Contact 7 Form admin anymore or the setup will break. These updates will have to be repeated with each plugin update, unless the author does something similar.
  8. Happy translating.

Cheers
Alex

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago