Weglot translate on Elementor

With this code, we force translation of content generate by elementor using the elementor/frontend/the_content filter

use Weglot\Parser\Parser;
use Weglot\Parser\ConfigProvider\ServerConfigProvider;
function weglot_content_check( $content ) {

	$request_url_services = weglot_get_service( 'Request_Url_Service_Weglot' );
	$parser_services   = weglot_get_service( 'Parser_Service_Weglot' );
	$generate_switcher = weglot_get_service( 'Generate_Switcher_Service_Weglot' );
	$replace_url_services = weglot_get_service( 'Replace_Url_Service_Weglot' );
	$current_language = $request_url_services->get_current_language()->getExternalCode();
	$original_language = weglot_get_original_language();

	$config             = new ServerConfigProvider();
	$client             = $parser_services->get_client();
	$parser             = new Parser( $client, $config, [] );
	$content = $generate_switcher->generate_switcher_from_dom( $content );
	if ( $original_language == $current_language ) {
		return $content;
	}

	$content = $parser->translate($content, $original_language, $current_language);
	$content = $replace_url_services->replace_link_in_dom( $content );
	return $content;

}
add_action( 'elementor/frontend/the_content', 'weglot_content_check' );

Last updated