Error “Call to a member function switch_to_locale() on null” during insert_with_markers

From your comment:

Those htaccess changes don’t happen as a result of an add_action.
Perhaps they should?

Yes, they should.

And here’s why

insert_with_markers() calls switch_to_locale() which uses $GLOBALS['wp_locale_switcher'] (or $wp_locale_switcher) which is initialized in wp-settings.php, but only after WordPress runs the setup_theme hook.

And looking at the error in question and line #1582 in wp-includes/l10n.php which is return $wp_locale_switcher->switch_to_locale( $locale );, that error occurred most likely because the global variable $wp_locale_switcher has not yet been defined or initialized. Which indicates that you called insert_with_markers() too early.

So if you want to use insert_with_markers(), make sure you use the proper hook; for example, load-{$page_hook} or update_option_{$option}:

// Example 1:
add_action( 'admin_menu', function () {
    $hookname = add_menu_page( ... );
    add_action( 'load-' . $hookname, function () {
        // Call insert_with_markers()
    } );
} );

// Example 2:
add_action( 'update_option_my_field', function ( $old_value, $value ) {
    // Call insert_with_markers()
}, 10, 2 );

Both tried & tested working on WordPress 5.2.4 and 5.3.

But it’s also possible there’s a plugin or custom code which is messing with $wp_locale_switcher, so if using a hook later than setup_theme doesn’t work for you, you can try deactivating plugins..