How can I localize the field labels in Contact Form 7

I have managed to localize my clients site from Spanish to French:

http://www.microcementoeuropeo.com

Read More

…using a combination of the WPML plugin and the Gettext framework. The only thing i have been unsuccessful translating are the labels on the contact form itslef:

<p>Nombre<br />
<span class="wpcf7-form-control-wrap your-name"><input type="text" name="your-name" value=""    class="wpcf7-text wpcf7-validates-as-required" size="40" /></span> </p>
<p>Email<br />
<span class="wpcf7-form-control-wrap your-email"><input type="text" name="your-email" value="" class="wpcf7-text wpcf7-validates-as-email wpcf7-validates-as-required" size="40" /></span> </p>
<p>Teléfono<br />

…so the text i’m trying to translate would be “Nombre” – to the French.

I’ve tried the gettext methods that worked for other hard-coded areas:

 <p><?php _e('Nombre', theme_domain);?><br />

 <p><?php __('Nombre', theme_domain);?><br />

…but this does not work.

(i´m adding these into wordpress through admin and not directly to the .php file).

I’m familiar with creating .po / .mo files and know how to localize normal hard-coded text – but contact form 7 has me baffled.

Any suggestions appreciated.

Related posts

Leave a Reply

4 comments

  1. I think the solution to your problem lies in the ability to run PHP within the Contact Form 7 template. You can do this by writing your own shortcode and then adding this into CF7.

    Firstly, setup a shortcode so your translation functions are available within WP content areas. Something like:

    function e_text_shortcode( $atts ) {
        extract( shortcode_atts( array(
            'text' => 'something',
            'theme_domain' => 'something else',
        ), $atts ) );
    
        return _e($text, $theme_domain);
    }
    add_shortcode('e_text', 'e_text_shortcode');
    

    (disclaimer: I haven’t tested this!)

    Then you can use a plugin like this one so you can add shortcodes to the CF7 template area. With this plugin you have to define the codes you want to use as ‘keys’, something like:

    e_text key='e_text'
    

    Then you should be able to use e_text shortcode in the CF7 form template and email templates.

  2. Thanks for the suggestion JunkMyFunk. I tried to implement this but ran into some issues.

    I eventually found a workaround using the following method:

    1. Install WPML plugin.
    2. Create a contact form for each language in WP CF7
    3. Use the following conditional statement to show the correct contact form for each language.

      <?php if(get_bloginfo('language')=='es-ES') 
      { 
      echo do_shortcode('[contact-form 1 "Contact form 1"]'); 
      } else {   
      echo do_shortcode('[contact-form 2 "Contact_form_2"]');   
      } 
      ?>
      

    Thanks for your help all the same –

  3. The reason why this does not work is because as of v4.4.1 the CF7 plugin does not include language locales anymore, these are maintained in the worpdress translation GlotPress instead. As a result, you need to install locales manually, one for each language you want to create forms for. You can then create a form in a new language by adding the locale attribute in your dashboard url, for example for the German locale de_DE,


    http://<your-domain>/wp-admin/admin.php?page=wpcf7-new&locale=de_DE

    Alternatively, you can use the CF7 PolyLang module which allows you to manage CF7 forms in different languages using the PolyLang plugin (recommended by WP). PolyLang is an alternative to WPML,

    1. Install the PolyLang plugin, and activate.
    2. In the dashboard Settings->Language section, add languages to your polylang settings. Each language you add, polylang will manage the content in each of these languages. Visitors to your site will be able to choose which language content they want to see, or automatically view their browser default language depending on the settings you choose.
    3. Next, install the CF7 module and activate it. It will lookup the languages you set up in the the polylang settings and download the correct CF7 locale. It will also enable the management of the forms in those languages.