Select field placeholder (include_blank) translation not working
-
The default “-Please choose an option-” placeholder string for a
<select>
element is not translated in CF7 5.9.6. This is the string generated when using theinclude_blank
option. A Finnish translation for it exists, it is just not used here.Luckily a workaround exists, using
first_as_label
.Examples:
- This encounters the issue, and prints “-Please choose an option-” instead of a translated string:
[select* menu-palvelu include_blank "Palvelu 1" "Palvelu 2"]
- This workaround prints “Valitse” (Finnish for “Choose”):
[select* menu-palvelu first_as_label "Valitse" "Palvelu 1" "Palvelu 2"]
Older threads:
Based on a forum search, this issue first appeared around 5.7, and has been reported also in German and French translations. It seems like the translation for this string is broken more widely, not just for one language.
https://www.ads-software.com/support/topic/select-field-placeholder-language/
https://www.ads-software.com/support/topic/select-topic-include_blank-translation-bug/
https://www.ads-software.com/support/topic/change-select-blank-text-based-on-field-or-form/
- This encounters the issue, and prints “-Please choose an option-” instead of a translated string:
-
Here’s the translation found in contact-form-7-fi.po, which is not applied here:
: modules/select.php:88
msgid “—Please choose an option—”
msgstr “—Valitse vaihtoehto—”The dash, as it’s rendered here, is
& #8212;
in the file, minus the space.Where can we see the website in question?
Hello,
confirm. All plugin translations work except for
__( '—Please choose an option—', 'contact-form-7' )
The translation with & #8212; for__( '—Please choose an option—', 'contact-form-7' )
does not work! For privacy reasons, I cannot disclose the website address. But here’s what I can share:- The latest version of the plugin 5.9.8 is currently installed.
- Steps that have been taken:
- Deactivation of all plugins.
- Forced creation of a .pot template that was missing in the languages folder.
- Download the stable version of all translation files by following the link from the readme in the languages folder.
- I tried removing the & #8212; characters in the original code, but that didn’t work either.
The suggestion “Replace text with a snippet” is not acceptable to me.
Thanks
I added this code snippet for debugging purposes:
add_filter('gettext', function($translated_text, $untranslated_text, $domain) {
if ($domain === 'contact-form-7') {
error_log('—Please choose an option—' . $untranslated_text); // Логируем оригинальный текст
}
return $translated_text;
}, 20, 3);
журнал:Here’s what I see in the error.log (I put a space to demonstrate
& #8212;
) since the site’s system turns into a long tere:[25-Sep-2024 11:58:29 UTC] & #8212;Please choose an option& #8212;Contact Forms
[25-Sep-2024 11:58:29 UTC] & #8212;Please choose an option& #8212;Contact Form
[25-Sep-2024 11:58:29 UTC] & #8212;Please choose an option& #8212;Spam protection
[25-Sep-2024 11:58:29 UTC] & #8212;Please choose an option& #8212;Email marketing
[25-Sep-2024 11:58:29 UTC] & #8212;Please choose an option& #8212;Payments
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Contact Forms
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Contact Form
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Spam protection
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Email marketing
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Payments
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;text
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;email
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;URL
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;tel
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;number
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;date
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;text area
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;drop-down menu
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;checkboxes
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;radio buttons
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;acceptance
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;quiz
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;file
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;submit
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Contact Forms
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Contact Form
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Spam protection
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Email marketing
[25-Sep-2024 11:58:30 UTC] & #8212;Please choose an option& #8212;Payments
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;text
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;email
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;URL
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;tel
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;number
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;date
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;text area
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;drop-down menu
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;checkboxes
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;radio buttons
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;acceptance
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;quiz
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;file
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;submit
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;text
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;email
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;URL
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;tel
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;number
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;date
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;text area
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;drop-down menu
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;checkboxes
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;radio buttons
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;acceptance
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;quiz
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;file
[25-Sep-2024 11:58:31 UTC] & #8212;Please choose an option& #8212;submitIt appears that the string
& #8212;Please choose an option& #8212;
(& #8212;
) is indeed being passed as raw text through the translation function. This indicates that this line is somehow either missing in the translation file, or not properly registered for translation in the template.Try a more aggressive approach via str_replace: If for some reason gettext can’t handle this string, you can use a more aggressive approach using the str_replace function to replace the string right in its raw form:
add_filter('gettext', function($translated_text, $untranslated_text, $domain) {
if ($domain === 'contact-form-7') {
$translated_text = str_replace('& #8212;
Please choose an option& #8212;
', 'Пожалуйста, выберите опцию', $translated_text);
}
return $translated_text;
}, 20, 3);Great that the aggressive approach via str_replace worked! This confirms that the string was processed correctly, but the standard translation mechanism via gettext was not able to handle it. Let’s look into why this might have happened.Possible reasons why the original translation didn’t work:
- HTML-энкодированные символы (
& #8212;
):- One of the main reasons is the use of HTML-encoded characters — (
& #8212;
) for a long dash in a string. When the string goes through the standard gettext function, these characters are not decoded and WordPress sees the string as different from regular text with a dash. This is why the string was not matched with the translation, even though it was in the file. - The gettext function expects the string in its pure form, and the encoded characters can prevent the string from being processed correctly.
- String processing mechanism in Contact Form 7:
- In some plugins, strings can be processed through special functions before they gettext. This may include operations such as filtering or character replacement. If a string goes through a filter or function that modifies it before passing it to gettext, the translation mechanism will not work. In this case, it could be due to encoded characters.
- One of the main reasons is the use of HTML-encoded characters — (
How to solve this in the future:
- Use pure characters instead of encoded characters:
- If possible, avoid using encoded characters such as — (
& #8212;
) in strings that need to be translated. Instead, use normal characters (such as a long dash -) so that the string is processed correctly through gettext.
- If possible, avoid using encoded characters such as — (
So what does the plugin author @takayukister think about this? Can you fix it?
After all, I would like users who display content in different languages to see each of their translations from the corresponding translation file.
Thanks
- This reply was modified 2 months ago by cousinr.
I tried another method
add_filter('gettext', function($translated_text, $untranslated_text, $domain) {
if ($domain === 'contact-form-7' && $untranslated_text === '—Please choose an option—') {
return __('Please choose an option', 'theme-child');
}
return $translated_text;
}, 20, 3);This changed the result to Please choose an option. But! The translation didn’t pull up even from the child theme!
Then I tried the following
functions.php:
add_action('wp_enqueue_scripts', 'localize_select_translation');
function localize_select_translation() {
// Make sure jQuery is connected
wp_enqueue_script('jquery');
// Let's check if Contact Form 7 is used on the page
if (is_singular() && has_shortcode(get_post()->post_content, 'contact-form-7')) {
// Connect your JS file
wp_enqueue_script('select-translation', get_stylesheet_directory_uri() . '/assets/js/select-translation.js', array('jquery'), null, true);
// Localize translation for use in JS
wp_localize_script('select-translation', 'selectTranslations', array(
'pleaseChoose' => __('Please choose an option', 'theme-child'),
));
}
}
select-translation.js:
jQuery(document).ready(function($) {
// We use the translation passed from PHP
$('select option').each(function() {
if ($(this).text() === 'Please choose an option') {
$(this).text(selectTranslations.pleaseChoose);
}
});
});As a result of this manipulation, I got a translated string from the translation file of the child theme. Therefore, the problem lies at the stage of shortcode step processing or contact-form-7 plugin script processing. Something is interfering with translation.
I encountered the same issue. CF7 plugin author @takayukister recommends using
first_as_label
option instead ofinclude_blank
. I ended up replacinginclude_blank
withfirst_as_label "—Please choose an option—"
to have the exact same placeholder but which can now be translated ??
- You must be logged in to reply to this topic.