WordPress provides internationalization functions (__(), _e(), etc.) to make static text translatable using a text domain and most of the theme and plugin developers use these functions to print their static texts something like this _e(‘Plugin Settings’,’plugin_domain’); Here ‘Plugin Settings’ is the static text and ‘plugin_domain’ is the text domain that is used for translation.

To translate these static texts we can use third-party software like POEdit or can go with WordPress plugins like Loco Translate but there may be cases that developer might have use wrong text domain for any specific string and in that case this software and plugins normally gets failed and also there might be condition people wanted to translate only couple of strings and in that case using plugins like loco translate is not a good idea at all. So WordPress provides a way to do so using a filter function ‘gettext‘.

add_filter('gettext', 'change_text_filter', 20, 3);

function change_text_filter( $translated_text, $untranslated_text, $domain ) {

   //make the changes to the text

   switch( $untranslated_text ) {

       case 'Static Text Here':

         $translated_text = __( 'Transalated Text Here',$domain );

       break;

    }

  return $translated_text;

}

Here we can create multiple cases for multiple strings and also if wanna go for any specific page string can wrap this under conditional tags like this :

add_filter('gettext', 'change_text_filter', 20, 3);

function change_text_filter( $translated_text, $untranslated_text, $domain ) {

if ( is_singular() ) {

   //make the changes to the text

   switch( $untranslated_text ) {

       case 'Static Text Here':

         $translated_text = __( 'Transalated Text Here',$domain );

       break;

    }

}

  return $translated_text;

}
Welcome to Wbcom Designs!  - Get 10% off on all plugins & themes
Coupon : HELLO10