Support

Account

Home Forums General Issues Translating form field labels in frontend with Polylang

Solved

Translating form field labels in frontend with Polylang

  • I finally found a solution which works easily when using the Polylang plugin. I’m posting it here in case you find it useful 🙂 This code adds the fields to the Polylang String Translations screen and outputs the translated string in the frontend. This has been particularly useful when building an app with multiple acf_forms in the frontend.

    add_action('init', function () {
    	foreach (acf_get_field_groups() as $group) {
    		$fields = acf_get_fields($group['ID']);
    		if (is_array($fields) && count($fields)) {
    			foreach ($fields as &$field) {
    				pll_register_string('form_field_group'.$group['ID'].'_label_'.$field['name'], $field['label'], 'acf_form_fields');
    			}
    		}
    	}
    });
    
    add_filter('acf/prepare_field', function ($field) {
    	if (!is_admin()) {
    		$field['label'] = pll__($field['label']);
    	}
    	return $field;
    }, 10, 1);
  • Thanks, this was great find!

    I just went thru the the code of acf_form processing and came here to suggest a new hook, but you’ve found existing one!

    This works even if you don’t use Polylang, just make sure translations are in your .mo & .po files.

    add_filter('acf/prepare_field', function ($field) {
    	if (!is_admin()) {
    		$field['label'] = __($field['label'], 'textdomain' );
    	}
    	return $field;
    }, 10, 1);
    
  • Cool! 🙂

    • riba86

    • September 3, 2019 at 7:52 pm

    Hi! Thank you for this solution. I now see “acf_form_fields” group under String Translations in Polylang plugin. But when I change label name to translated language there is no effect on website front end. Any idea what could be wrong? Thanks!

  • @riba86 Are you using the acf_form function?

    • riba86

    • September 5, 2019 at 3:42 pm

    I tried adding your code to themes functions.php:

    add_action('init', function () {
    	foreach (acf_get_field_groups() as $group) {
    		$fields = acf_get_fields($group['ID']);
    		if (is_array($fields) && count($fields)) {
    			foreach ($fields as &$field) {
    				pll_register_string('form_field_group'.$group['ID'].'_label_'.$field['name'], $field['label'], 'acf_form_fields');
    			}
    		}
    	}
    });
    
    add_filter('acf/prepare_field', function ($field) {
    	if (!is_admin()) {
    		$field['label'] = pll__($field['label']);
    	}
    	return $field;
    }, 10, 1);
  • Yes, but how are you outputting the ACF fields in the front end?

    • riba86

    • September 5, 2019 at 3:51 pm

    Oh, sorry. I’m just translating this website. This theme was designed by other company for my client. Where can I look for this?

  • Advanced Custom Fields are usually only output in the backend. If they are output in the front end in your project, I’d recommend contacting the developer who made the site and clarifying that with him/her.

    • riba86

    • September 5, 2019 at 4:05 pm

    Ok thank you. They are not cooperating with this developer anymore. I think it’s best they uninstall Polylang and buy WPML?

  • Both WPML and Polylang work fine with ACF. I’m confused about what you’re trying to achieve. Can you send me a screenshot of the strings as they appear in the front end of the website, which you’re trying to translate?

  • A note; the solution here is for translating the ACF input field labels. Not the content. That’s an important difference. If you’re trying to translate the content, then you don’t need anything except the regular Polylang or WPML Plugin functionality. If the strings on the website are fixed in the code and untranslatable, then that’s a different issue. I’d be happy to help directly if you need consultancy in that area.

    • riba86

    • September 5, 2019 at 5:02 pm

    Thank you for your time. I have custom post types named “Projects” and each project has field group “Project data”. I want to translate labels of field group Project data. Website is superform.si. Language switcher is in menu. Here are screenshots:

  • I see what you’re trying to achieve. It’s likely that these strings aren’t coming from ACF at all, but are fixed in the code. Is the code online anywhere, e.g. in Github?

    • riba86

    • September 5, 2019 at 5:26 pm

    It’s custom theme. I think it is coming from ACF. I can edit this field labels in ACF and see them in Polylang String Translations:

  • Yes, I know. But if it’s a custom theme, the strings in your screenshot are probably not coming from ACF, but are fixed in the PHP code. I’ll need to see this code if you need me to help you further, thanks.

    • riba86

    • September 5, 2019 at 5:34 pm

    Thanks. I will tell them to give me FTP access.

    • riba86

    • September 6, 2019 at 6:14 pm
    This reply has been marked as private.
    • riba86

    • September 10, 2019 at 1:25 am

    I just got FTP access. I can give you login info or send you theme if you are still willing to help 🙂 Thanks!

  • This reply has been marked as private.
    • riba86

    • September 10, 2019 at 9:17 pm

    Sorry I can’t see private replies 🙁 Can you send me email to info(at-domain)kult.si. I really appreciate this. Thanks!

    • nenosw

    • October 17, 2019 at 9:50 am

    Great solution. Would this also be possible for subfields of a group field?

  • @nenosw I would expect so. Have you tried the code to see what happens?

    • nenosw

    • October 17, 2019 at 8:33 pm

    Yes I tried, but I get only the first level of fields. I think there is another loop needed if a field is of type group that iterates trough the subfields. But I’m not sure how to achieve this.

  • There’s some code here which looks as if it could contain stuff you could use.

Viewing 25 posts - 1 through 25 (of 27 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.