Translate only certain pages

Translate only certain pages

TranslatePress makes it really easy to translate only certain pages or exclude content from translation.
The Do not translate certain paths feature can be used to:

Exclude Paths From Translation – enables translation sitewide but lets you manually exclude entire posts, pages, cpts or products from translation.
Translate Only Certain Paths – disables translation sitewide but lets you manually enable translation for certain content.

This feature can be accessed by going to: Settings -> TranslatePress -> Advanced tab -> Exclude Strings -> Do not translate certain paths

Exclude Paths from Translation
With the URL paths feature, you can use the Exclude Paths From Translation option to exclude certain content from translation by entering its URL path. When adding a URL path, you have three options to target content:

Specific URL path – you can enter the full URL path to only exclude just that specific piece of content.
Wildcard – you can use a wildcard (*) to target multiple pieces of content that match. For example, entering /blog/* would target every piece of content that has /blog/ as its root, including /blog/post-1/, /blog/post-2/, etc.
Home tag – you can enter {{home}} to target your homepage.

TranslatePress will disable translation and hide the language switcher for any content that matches the URL paths that you enter.
Translate Only Certain Pages (Paths)
In addition to excluding content by its URL path, TranslatePress lets you only enable translation for specific content by its URL path using the Translation Only Certain Paths option. This is a good option if you only want to include translation on a few pages on your site and want to disable translation everywhere else.
You can use all the same URL path options that you get for excluding content. The only difference is that you』ll select Translate Only Certain Paths when adding the URL paths:

TranslatePress will enable translation and show the language switcher only for the content that matches the URL paths that you enter.

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

Fix broken HTML

Fix broken HTML

If the HTML from the theme or other plugins is not very well formatted, it can sometimes cause issues when using TranslatePress.
For example, using duplicated attributes in a tag can cause missing attributes, which in turn can lead to unexpected behavior of the website when browsing a translated page.
We have solutions that attempt fixing broken HTML. Go to Settings -> TranslatePress -> Advanced tab.

Activate option Fix spaces between HTML attributes first (attributes without spaces between them are not valid HTML)
If the first fix doesn』t work, try activating option Fix broken HTML. Only try this if you still have problems after using the first solution

If your issue is still not solved, look for bad formatting in the missing HTML and correct it in the plugins/theme.
Common bad formatting includes duplicate attribute on same tag, like the class attribute in the following example:

The correct version would be:

Replace Default Flags

Replace Default Flags

If you』re looking to replace the default flags for certain languages of your multilingual site you can use one of the two options below:
1. Replace Default Flags — directly from the Advanced tab
​You can add a new flag for an existing language directly from TranslatePress Settings –> Advanced tab –> Custom Language section.
From here you add all the information about an existing language, or just use another image for the flag.

 
Once you』ve uploaded and selected the new flag image make sure to click 「Save Changes」.
You can read more about Custom Languages here.
2. Alternative Code Based Method
Note: if you are looking to replace the default flags in order to add a custom language please consult the documentation here.
You can replace the default flags in TranslatePress by using a bit of coding:

Create an empty plugin like this:
https://gist.github.com/sareiodata/76f701e01db6685829dbSince this plugin will contain the new flags, you』ll have to include the plugin file inside a folder like /tp_custom_flags as well as to create /tp_custom_flags/flags subfolder where the new ones will be found.
Add the following code to the end of it:
/**
* Change flags folder path for certain languages.
*
* Add the language codes you wish to replace in the list below.
* Make sure you place your desired flags in a folder called "flags" next to this file.
* Make sure the file names for the flags are identical with the ones in the original folder located at 'plugins/translatepress/assets/images/flags/'.
* If you wish to change the file names, use filter trp_flag_file_name .
*
*/

add_filter( 'trp_flags_path', 'trpc_flags_path', 10, 2 );
function trpc_flags_path( $original_flags_path, $language_code ){

// only change the folder path for the following languages:
$languages_with_custom_flags = array( 'en_US', 'es_ES' );

if ( in_array( $language_code, $languages_with_custom_flags ) ) {
return plugin_dir_url( __FILE__ ) . '/flags/' ;
}else{
return $original_flags_path;
}
}

Modify the code by editing the language codes list with the languages you wish to replace. In the example we are adding custom flags for languages en_US and es_ES. The rest of the flags will not change.
Make sure you place your desired flags in a folder called 「flags」 next to this file. Put both the folder and this file into another folder.
Make sure the file names for the new flags are identical with the ones in the original folder located at 『plugins/translatepress/assets/images/flags/』.
Install this plugin via FTP (copy it inside wp-content/plugins) or create a zip archive with it and install it via the WordPress plugin upload functionality

Show opposite language in language switcher

Show opposite language in language switcher

How to Show Opposite Language in Language Switcher
Showing the opposite language in your language switcher comes in really handy in bilingual websites. You want to set it up so that you see the other language name or flag in the menu, not the current language.
There are two ways to achieve this, which we』ll detail below:
1. In your Menu Language Switcher
Let』s say you want to show just the opposite flag(s) in your menu.
This is possible with a bit of CSS.

go to Appearance -> Menus -> Language Switcher
select Opposite Language

Note:This option only works when there are exactly two languages, the default one and a translation one.
2. In your Shortcode Language Switcher and Floating Language Switcher

Another place where you might want to display the opposite flag(s) could be in the shortcode language switcher and the floting language switcher. You can achieve this by enabling the 「Show opposite language in the language switcher」 option from the Advanced tab. You have to scroll all the way down as this option is above the Custom language section.
 
 

 
Note: This option only works when there are exactly two languages, the default one and a translation one.

Disable SQL errors notices

Disable SQL errors notices

TranslatePress has a system that tracks SQL errors and helps you display them.
This system can be disabled in some cases, for example:

false positives
errors that announce various problems, these being false.

With the help of this code, TranslatePress no longer:

records SQL errors
prevents admin notices from appearing
and disables automatic translation

If you want to insert this custom code, follow these steps:
1. Create an empty plugin like this:
https://gist.github.com/sareiodata/76f701e01db6685829db
2. Add the following code to the end of it:
add_filter('trp_disable_error_manager','__return_true');
 
3. Install this plugin via FTP (copy it inside wp-content/plugins) or create a zip archive with it and install it via the WordPress plugin upload functionality.

Disable translation of Gettext strings

Disable translation of Gettext strings

Strings outputted by WordPress themes or plugins are called Gettext strings. Translating these types of strings through TranslatePress can be unnecessary if they are already translated or you plan to translate them using the .po/.mo translation file system.

Disabling this feature of TranslatePress can improve the page load perfomance of you site in certain cases.

Download and activate the following plugin to disable translation of Gettext strings. No other setting is necessary.

Download Disable Gettext Strings Translation Add-on