• Resolved arathra

    (@arathra)


    In the middle of a function, I need to change the language in which WPGlobus is working in. At the moment

    while( have_rows( 'countries_additional_option', 'option' ) ) {
    
    	the_row();
    	$value = get_sub_field( 'value' );
    	$label = get_sub_field( 'label' );
    }

    The label always returns in the language the site is currently in. However, I wish to return it in a different language.

    AI suggested wpglobus_switch_language( ‘it’ ) but that function doesn’t seem to exist.

    So how can I temporarily swap languages in the middle of a function?

Viewing 1 replies (of 1 total)
  • Plugin Contributor Alex Gor

    (@alexgff)

    Here is the code

    /**
     * Get the multilingual value of the acf option.
     */
    function wpglobus_acf_get_sub_field($key, $order = 0) {
    	
    	static $options = null;
    	if ( is_null($options) ) {
    		$options = acf_get_option_meta('options');
    		if ( ! is_array( $options ) ) {
    			return false;
    		}
    	}
    	
    	$response = false;
    	
    	foreach( $options as $option_key=>$option_value ) {
    		
    		if ( $option_key[0] === '_' ) {
    			continue;
    		}
    		
    		$_key = '_' . $order . '_' . $key;
    		
    		if ( false === strpos($option_key, $_key) ) {
    			continue;
    		}
    		
    		if ( ! empty( $option_value[0] ) ) {
    			$response = $option_value[0];
    		}
    		
    	}
    	
    	return $response;
    }
    
    $i = 0;
    while( have_rows( 'countries_additional_option', 'option' ) ) {
    
    	the_row();
    	
    	// Get $value and $label in current lanuage.
    	// $value = get_sub_field( 'value' );
    	// $label = get_sub_field( 'label' );
    	
    	// Get multilingual $value.
    	$value = wpglobus_acf_get_sub_field('value', $i);
    	// Get multilingual $label.
    	$label = wpglobus_acf_get_sub_field('label', $i);
    	
    	// For example: Get $value in Italian.
    	$value = WPGlobus_Core::text_filter( $value, 'it' );
    	// For example: Get $label in Italian.
    	$label = WPGlobus_Core::text_filter( $label, 'it' );
    
    	$i++;
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Change default language mid-function’ is closed to new replies.