Translate link

Add method to translate specifix link

By default, Weglot translate link based on some pattern. In the exemple below, we want to translate link in data-link2 attr. To do this we use the weglot_get_replace_modify_link filter to add a new pattern to our parser.

add_filter( 'weglot_get_replace_modify_link', 'custom_weglot_get_replace_modify_link' );
function custom_weglot_get_replace_modify_link( $data ) {
	$data['datalink2'] = '/<([^\>]+?)?data-link2=(\"|\')([^\s\>]+?)(\"|\')([^\>]+?)?>/';
    return $data;
}

function replace_datalink2( $translated_page, $current_url, $quote1, $quote2, $sometags = null, $sometags2 = null ) {
	$request_url_services = weglot_get_service( 'Request_Url_Service_Weglot' );
	$replace_url_Service = weglot_get_service( 'Replace_Link_Service_Weglot' );
	$current_language = $request_url_services->get_current_language();
	$translated_page  = preg_replace( '/<' . preg_quote( $sometags, '/' ) . 'data-link2=' . preg_quote( $quote1 . $current_url . $quote2, '/' ) . '/', '<' . $sometags . 'data-link2=' . $quote1 . $replace_url_Service->replace_url( $current_url, $current_language ) . $quote2, $translated_page );
	return $translated_page;
}

Last updated