Exclude a certain text or element from being translated

Exclude a certain text or element from being translated

There are situations when you might want to exclude a certain HTML element from being translated by TranslatePress.
How to exclude text or an element from being translated
You can easily achieve this by using the following attribute: data-no-translation.

Attributes provide additional information about HTML elements.

Let』s say we want to exclude a certain paragraph of text from being translated.

This is a paragraph of text that will be excluded from translations.

This can be easily done for any HTML element (list, form, div, etc.) to exclude it from being translated.

You can achieve the same thing programmatically in PHP by using the filter trp_no_translate_selectors.

add_filter( 'trp_no_translate_selectors', 'trpc_no_stranslate_selectors', 10, 2);
function trpc_no_stranslate_selectors($selectors_array, $language){
$selectors_array[] = '.do_not_translate_css_class';
$selectors_array[] = '#do_not_translate_css_id';
return $selectors_array;
}

Note: This will exclude from being translated all the element』s child elements.

To exclude a certain element from being translated in the front-end use the attribute data-no-dynamic-translation. This differs from the data-no-translation attribute because it still allows the translation of the element in the server side. It will only stop being detected in the client side using JS. 

In other words, dynamic changes over this element will be ignored.

This is a paragraph of text that will be translated only on the server side. Dynamic changes to this element using JS will not be detected.

There is also a filter where you can programmatically add other selectors to be excluded from dynamic translation:  trp_skip_selectors_from_dynamic_translation

/**
*
* Skip dynamic detection of strings.
*
*/
add_filter( 'trp_skip_selectors_from_dynamic_translation', 'trpc_skip_dynamic_translation' );
function trpc_skip_dynamic_translation( $skip_selectors ){
$add_skip_selectors = array( '.section', '.other-section' ); // replace with a list of selectors
return array_merge( $skip_selectors, $add_skip_selectors );
}

You might also want to check out the Advanced Settings tab in TranslatePress which lets you:

Exclude selectors from translation
Exclude Gettext strings
Exclude from dynamic translation

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注