Support

Account

Home Forums ACF PRO Combining ACF Pro with WPML Reply To: Combining ACF Pro with WPML

  • 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.