Translating php’s imap extension in WordPress

I’m using php’s imap extension in a WordPress plugin (Post from Email) to fetch email messages from POP3 email servers. I had to figure out how to internationalize (i18nize) the error messages from imap_errors(). These messages are English-language text strings. I had to try a lot of scenarios to work out what English messages are possible, as I could not find a specification. Here’s my resulting code.

 function localize_imap_errors( $errors ) {
      if ( ! is_array( $errors ) ) {
        return $errors;
      }
      $result     = array();
      $localizers = array(
        /* translators: For the imap error 'No such host as example.com' */
        'Mailbox is empty' => esc_attr__( 'Mailbox is empty', 'my-domain' ),
        /* translators: For the imap error 'No such host as example.com' */
        'No such host as'   => esc_attr__( 'No such host as', 'my-domain' ),
        /* translators: For the imap error 'TLS/SSL failure for example.com: SSL negotiation failed' */
        'TLS/SSL failure for' => esc_attr__( 'TLS/SSL failure for', 'my-domain' ),
        /* translators: For the imap error 'TLS/SSL failure for example.com: SSL negotiation failed' */
        'SSL negotiation failed' => esc_attr__( 'SSL negotiation failed', 'my-domain' ),
        /* translators: For the imap error 'Can't connect to example.com,1110: Connection timed out' */
        'Can\'t connect to'   => esc_attr__( 'Cannot connect to', 'my-domain' ),
        /* translators: For the imap error 'Can't connect to example.com,1110: Connection timed out' */
        'Connection timed out'  => esc_attr__( 'Connection timed out', 'my-domain' ),
        /* translators: For the imap error 'Can not authenticate to POP3 server: [AUTH] Authentication failed.' */
        'Can not authenticate to POP3 server' => esc_attr__( 'Can not authenticate to POP3 server', 'my-domain' ),
        /* translators: For the imap error 'Can not authenticate to POP3 server: [AUTH] Authentication failed.' */
        'Authentication failed'               => esc_attr__( 'Authentication failed', 'my-domain' ),
        /* translators: For the imap error 'Can not authenticate to POP3 server: POP3 connection broken in response' */
        'Can not authenticate to POP3 server: POP3 connection broken in response'
            => esc_attr__( 'Can not authenticate to POP3 server: POP3 connection broken in response', 'my-domain' ),
      );
      foreach ( $errors as $error ) {
        foreach ( $localizers as $str => $replacement ) {
          $error = str_replace( $str, $replacement, $error );
        }
        $result [] = $error;
      }
    return $result;
  }

Leave a Comment