
A follow-up for this forum post about translating form field labels with Polylang. This time with WPML String Translation.
add_action('init', function(){
if (!is_admin()) {
foreach (acf_get_field_groups() as $group) {
$fields = acf_get_fields($group['ID']);
if (is_array($fields) && count($fields)) {
foreach ($fields as $field) {
do_action('wpml_register_single_string', 'ACF Labels', $field['label'], $field['label']);
do_action('wpml_register_single_string', 'ACF Messages', $field['message'], $field['message']);
do_action('wpml_register_single_string', 'ACF Instructions', $field['instructions'], $field['instructions']);
}
}
}
}
});
add_filter('acf/load_field', function (array $field) {
$field['label'] = apply_filters('wpml_translate_single_string', $field['label'], 'ACF Labels', $field['label']);
$field['message'] = apply_filters('wpml_translate_single_string', $field['message'], 'ACF Messages', $field['message']);
$field['instructions'] = apply_filters('wpml_translate_single_string', $field['instructions'], 'ACF Instructions', $field['instructions']);
return $field;
});
Hi!
I love you solution, and it have helped me alot!
However I’m struggeling with labels from fields in groups. Have you found a solution for that?
I fixed it by hardcoding jquery translation-strings 🙂
Eg:
<?php
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if($my_current_lang == 'en') {
?>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script>
$( document ).ready(function() {
$("form .class").html($("form .class").html().replace('Original string','New string'));
</script>
<?php
}
?>