Wordpress Snippets

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 );

Hire Us. Or just say Hi!
Need a job? Apply to get one.