To achieve WP Forms multi-language support, you can use the gettext
filter provided by WordPress. This filter allows you to translate strings in your code, including the strings used in WP Forms.
Here’s an example code snippet that demonstrates how to translate the labels of a WP Form using the gettext
filter:
function wpsnippets_translate_wpform_labels( $translated_text, $text, $domain ) {
if ( $domain === 'wpforms' ) {
switch ( $text ) {
case 'Name':
$translated_text = __( 'Nom', 'your-text-domain' );
break;
case 'Email':
$translated_text = __( 'Email', 'your-text-domain' );
break;
// Add more cases for other form labels as needed
}
}
return $translated_text;
}
add_filter( 'gettext', 'wpsnippets_translate_wpform_labels', 10, 3 );
In this code snippet, we define a custom function wpsnippets_translate_wpform_labels
that takes three parameters: $translated_text
, $text
, and $domain
. We check if the $domain
matches the WP Forms domain, which is 'wpforms'
. If it does, we use a switch
statement to translate specific form labels. You can add more cases for other form labels as needed.
To use this code snippet, you need to replace 'your-text-domain'
with the actual text domain of your theme or plugin. You can find the text domain in the style.css
file of your theme or in the plugin’s code.
By using this code snippet, you can easily translate the labels of your WP Forms to different languages, making your forms multi-language compatible.
Examples
Example 1: Adding multi-language support to WP Forms using a translation plugin
This example demonstrates how to add multi-language support to WP Forms by using a translation plugin such as WPML or Polylang.
function wpsnippets_wpforms_translate( $translated, $text, $domain ) {
if ( 'wpforms' === $domain ) {
switch ( $text ) {
case 'First Name':
$translated = __( 'Prénom', 'wpforms' );
break;
case 'Last Name':
$translated = __( 'Nom de famille', 'wpforms' );
break;
// Add more translations for form fields as needed
}
}
return $translated;
}
add_filter( 'gettext', 'wpsnippets_wpforms_translate', 20, 3 );
This code snippet adds a filter to the gettext
hook to translate specific form field labels in WP Forms. Inside the filter function, we check if the text domain is ‘wpforms’ and then use the __()
function to translate the desired form field labels. You can add more translations for other form fields by extending the switch
statement.
Example 2: Using WPML to translate WP Forms
This example demonstrates how to use the WPML plugin to translate WP Forms.
function wpsnippets_wpforms_wpml_translate( $translated, $original_text, $domain ) {
if ( 'wpforms' === $domain ) {
global $sitepress;
$translated = $sitepress->translate( $original_text, 'wpforms' );
}
return $translated;
}
add_filter( 'wpforms_frontend_form_fields_get', 'wpsnippets_wpforms_wpml_translate', 10, 3 );
This code snippet adds a filter to the wpforms_frontend_form_fields_get
hook to translate form field labels using WPML. Inside the filter function, we check if the text domain is ‘wpforms’ and then use the WPML function $sitepress->translate()
to translate the form field labels.
Example 3: Using Polylang to translate WP Forms
This example demonstrates how to use the Polylang plugin to translate WP Forms.
function wpsnippets_wpforms_polylang_translate( $translated, $original_text, $domain ) {
if ( 'wpforms' === $domain ) {
$translated = pll__( $original_text );
}
return $translated;
}
add_filter( 'wpforms_frontend_form_fields_get', 'wpsnippets_wpforms_polylang_translate', 10, 3 );
This code snippet adds a filter to the wpforms_frontend_form_fields_get
hook to translate form field labels using Polylang. Inside the filter function, we check if the text domain is ‘wpforms’ and then use the Polylang function pll__()
to translate the form field labels.