Home

WP translate or replace text

If plugin missing some translations or some text are not suitable you site here is how you replace them same time multiple languages.

This code is from wpml plugin.

add_filter( 'gettext', 'lazy_text_translations', 20, 3 );
function lazy_text_translations( $translated_text, $text, $domain ) {

    $trans = [];
    $trans["en_US"] = array(
		'Text to translate' => 'en translation'
    );
    $trans["fi"] = array(
		'Text to translate' => 'fi translation'
    );
    $trans["ru_RU"] = array(
		'Text to translate' => 'ru translation'
    );

    $lang = get_locale();
    $case = $trans[$lang][$text]; // replace tanslations use $translated_text, use $text if translate text
    
    if( !is_null ($case) ){
        return  $case;
    }

	return $translated_text;
}

Leave a Reply

Your email address will not be published.