How to use wordpress $polylang->model->set_post_language in custom php code?

Turns out that the language relationship is simple and set in just a few functions:

// Insert the EN post which returns the EN post id.

$post_data_en = array( 
// add EN post data like the title and content
)
$post_id_en = wp_insert_post($post_data_en, true);

// Then set the language of the post

pll_set_post_language($post_id_en, 'en');

// Insert the ES post which returns the ES post id.

$post_data_en = array( 
// add ES post data like the title and content
)
$post_id_es = wp_insert_post($post_data_es, true);

// Then set the language of the post

pll_set_post_language($post_id_en, 'es');

// Then bind them together with a translation relationship

pll_save_post_translations(array('en' => $post_id_en, 'es' => $post_id_es));

The posts are now linked as translations of each other.