Support

Account

Home Forums ACF PRO Combining ACF Pro with WPML

Solved

Combining ACF Pro with WPML

  • I’m a bit confused about what is the best way to combine ACF with WPML. I have read this document (https://wpml.org/documentation/plugins-compatibility/translate-sites-built-with-acf/) and several others, but I can’t wrap my head around it yet.

    I am creating a profile site, where (custom) posts are the profiles.
    I created a field group (in Dutch) which I then duplicated to English.

    My form consists of several checkboxes, radio buttons and selects, which are all defined as “value : label” (if the value is not the same as the label). In the translated version I changed the Field label, the description, placeholder and the label part of some the checkboxes/radio buttons/selects.

    Since I don’t want people to have to enter their info twice, I want the same info in both languages, but with translated labels/output. For example for eye color, this are the options in Dutch:
    blue : blauw
    brown : bruin
    green : groen
    grey : grijs

    For English the options are:
    blue
    brown
    green
    grey
    (since value is the same as label)

    If I create a post through the (Dutch) front-end form, all values get submitted.

    In the admin I then duplicate it. I see all the (translated) labels appear in the back-end.
    So if I select bruin in Dutch, I see it reflect as brown in the English post.

    But if I var_dump get_field_objects( $translated_post_id ) it gives me the same output as get_field_objects( $original_post_id ).

    If I ‘detach’ the translation then the proper values show (in a var_dump and on the front-end), but then any future edits are not automatically synced with the other language(s).

    I tried some functions I found on wpml.org to duplicate a post on save/publish, but none of these worked.

    My questions:
    1) how do I force it so that the values/labels are retrieved from the translated post (when synced) and not the original ?
    2) Or how to set it up properly so posts (aka profiles) can be edited in either language and be shown in both languages (with the translated labels, where needed) ?

  • Does anyone have any insight/intel reg. this ? I’m stumped….

  • Just tagging along. I’m also curious on ways to translate ‘select’ Field Type properly.

    edit: I’ve resolved my issue, let’s see if it can help with yours. First let’s see if I understand properly what you want to do. You have a site where posts are a person’s profile. One of the ACF field for the post is their eye color. Your main language is Dutch, second English.

    You want people/user to create a post in Dutch, add a Title/name and Content/description, then select eye color. You want the user to then create the translation in english, translate the name and description, but have the selected eye color sync. You want the labels for the eye color translated.

    Here is how I made it work for me.
    (ps. all ACF group fields created before WPML install are buggy AFAIK, create new ones)

    Create a new ACF group field in dutch with a ‘select’ field for the eye color (eye_color) with such value:
    blue : blauw
    brown : bruin

    Create translation in english using duplicate. Make separate. Keep Field name but edit ‘select’ values to:
    blue : blue
    brown : brown

    Create a new post. add Dutch content. Publish. In ‘Multilingual Content Setup’ set ‘eye_color’ to Copy. Save. Update.

    Create English translation using Duplicate. Make separate. Edit Title/name and Content/description to English text. Eye color label should be in english, and frontend should display correct value in english.
    I use this code to display the label :
    $field = get_field_object('eye_color'); $value = $field['value']; $label = $field['choices'][ $value ]; echo $label;

    Bugs: Editing the eye_color field in the English post doesn’t work. Modifications needs to be done in the original language. If this feature can’t be added, field should greyed out imo.

    Let me know if that helps you.

  • Hey @westjef, thanks for the reply, sorry it took a bit, but I don’t think our ‘situations’ are similar.

    My base language is English, not Dutch. All values/entries/choices/options are in English.

    I don’t want the user to have to translate anything. There should be one post only for all languages and the label is the thing that needs to be translated (imo).

    They enter the profile in the way the form is presented to them, which is in English right now.

    I think there are 2 steps:
    1) translating the output so visitors can use a different language to view
    2) translating the input so users can use a different language

    But for both the values need to be the same imo.

  • Was hoping that someone has some insight perhaps ?

  • I finally found the proper solution on what I wanted to achieve.

    Language: EN
    Field groups are not translated.
    Post type ‘profile’ set to: “Appear as translated”.

    Then by using load_field I run every (static) value that is possible/present in the form through a function which returns a translatable string.

    With this function I can now create translatable strings from an entire form instead without the need of duplicating the field group etc.

    Right now I translate the following ‘things’, but more might be added (if the need comes for it).
    – labels
    – choices
    – options
    – placeholders
    – button labels
    – instructions

    In case someone is interested, here’s the code.

    // if $field[ 'parent' ] == group_x
    if ( in_array( $field[ 'parent' ], ['group_57e3ed6c8b5f8', 'group_5938c49eb2247'] ) ) {
    
        if ( in_array( $field[ 'label' ], [ 'Tour Schedule' ] ) ) {
            $field[ 'button_label' ] = i18n_values( $field[ 'button_label' ] );
        }
    
        if ( in_array( $field[ 'label' ], [ 'Nationality' ] ) ) {
            if ( is_array( $field['choices'] ) && count( $field['choices'] ) > 0  ) {
                foreach( $field['choices'] as $key => $choice ) {
                    $field['choices'][ $key ] = i18n_countries( $choice );
                }
            }
            asort( $field['choices'] );
        }
    
        if ( in_array( $field[ 'label' ], ['Sex', 'Date type', 'Services', 'Hair colour', 'Eye colour', 'Build'] ) ) {
            if ( is_array( $field['choices'] ) && count( $field['choices'] ) > 0  ) {
                foreach( $field['choices'] as $key => $choice ) {
                    $field['choices'][ $key ] = i18n_values( $choice );
                }
            }
        }
    
        if ( in_array( $field[ 'type' ], ['true_false', 'message'] ) ) {
            $field[ 'message' ] = i18n_values( $field[ 'message' ] );
        }
    
        if ( $field[ 'type' ] == 'text' ) {
            $field[ 'placeholder' ] = i18n_values( $field[ 'placeholder' ] );
        }
    
        $field[ 'label' ]        = i18n_values( $field[ 'label' ] );
        $field[ 'instructions' ] = i18n_values( $field[ 'instructions' ] );
    }

    And I run the values through this function to translate them. It only has 1 value, but in my own file there are tons.

    function i18n_values( $value ) {
    
        $translation = $value;
        switch ( $value ) {
    
            case 'Female':
                $translation = __( 'Female', 'text-domain' );
                break;
    
        }
    
        return $translation;
    }

    i18n_countries does the same but only for country names.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Combining ACF Pro with WPML’ is closed to new replies.