Translate WordPress Programmatically With Gettext (Without .PO/.MO)

Add this to the functions.php of your theme.

This example is to be used with my Ask Me Anything (Anonymously) WordPress plugin.

To use it with other themes and plugins, change the text domain of the theme or plugin in the highlighted areas.

/**
 * Translate text strings using gettext
 *
 * This specific example is for the Ask Me Anything (Anonymously) WordPress plugin: https://wordpress.org/plugins/ask-me-anything-anonymously/
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 */
function amamc1206_translate_text_strings ( $original_text, $translated_text, $text_domain ) {
  
  switch ( $original_text ) {
  
    case 'Enter your answer..' :
  
      $translated_text = __( 'Enter your answer in French..', 'abl_amamc_td' );
      break;
  
    case 'Since you are currently logged in, your question will not be anonymous. Please log out or open this page in a private window if you wish to stay anonymous.' :
  
      $translated_text = __( 'Since you are currently logged in, your question will not be anonymous. Please log out or open this page in a private window if you wish to stay anonymous in French.', 'abl_amamc_td' );
      break;
      
    case 'asked:' :
  
      $translated_text = __( 'asked: in French', 'abl_amamc_td' );
      break;
  
    case 'on %1$s' :
  
      $translated_text = __( 'on French.. %1$s', 'abl_amamc_td' );
      break;
      
    case 'on %1$s' :
  
      $translated_text = __( 'on French.. %1$s', 'abl_amamc_td' );
      break;
  }
  
  return $translated_text;
}
add_filter( 'gettext', 'amamc1206_translate_text_strings', 20, 3 );
Hello, I am Arun Basil Lal. Thank you for reading!

I am a WordPress product developer and creator of Image Attributes Pro. I am passionate about solving problems and travelling the world.

Divi WordPress Theme - My Review

Divi WordPress Theme
Divi is a WordPress theme that web designers do not want you to know. It comes with a drag-and-drop theme builder. You can build beautiful looking unique websites without touching a line of code. Just choose from one of the many pre-made layouts, or pick elements and arrange them any way you like.

Divi is every WordPress developer's wet dream. Surprise your clients with neat responsive websites and have fun building them.

Divi comes from Elegant Themes. If you enjoy building websites, you *need* an Elegant Themes membership. 87 beautiful themes and 5 plugins for the cost of less than a candy-bar each!


Note: I am an avid user of Divi myself and this is a honest review. I wouldn't recommend something that I do not personally find amazing.

Leave a Reply

Your email address will not be published. Required fields are marked *

*