Translating an internal url

TranslatePress automatically converts all internal links to the needed language. This means that the language subdirectory ( i.e. example.com/en/) is added depending on the currently viewed language that a page is loaded in. If there are any slugs that have translations they will be adjusted too. This happens to dynamically generated links using WP functions ( such as get_permalink() ) as well as custom links hard coded in the page.

In case a link is not detected by TranslatePress due to being outputted in an uncommon way or you have a specific case where you need the link in a different language than the currently displayed, you can programmatically apply a conversion function. See example below on how to achieve this.

 

// The target language code. Set this to null if you want the current language. 
$target_language = 'en_US'; 
// The url to convert, works best if it's in default language, but can be in any other language because it's automatically detected
$source_url      = 'https://example.com/my-post/';

$trp             = TRP_Translate_Press::get_trp_instance();
$url_converter   = $trp->get_component( 'url_converter' );

// It's recommended to keep third parameter of the get_url_for_language() an empty string.
$target_url      = $url_converter->get_url_for_language( $language, $url_to_convert, '' );

 

Looking to go multilingual?

Get full access to TranslatePress and join 300.000+ website owners that grow their multilingual traffic.

Unlock PREMIUM Features

Or download FREE version