qtranslate / qtranslate-xt

qTranslate-XT (eXTended) - reviving qTranslate-X multilingual plugin for WordPress. A new community-driven plugin soon. Built-in modules for WooCommerce, ACF, slugs and others.
GNU General Public License v2.0
554 stars 105 forks source link

Default Wordpress Email Notifications do not properly filter/translate site_title out of the box #1222

Open kennypu opened 2 years ago

kennypu commented 2 years ago

Issue Default Wordpress email notifications (new user, password reset, password changed, etc.) that contain the site title does not properly filter the title language.

How to Reproduce

  1. Have an install of Wordpress (tested on a fresh install)
  2. Add translations for the site title in Settings > General > Site Title.
  3. Do something to send out a default Wordpress email (create new user and send notification, send password reset email for a user, etc.)

The default email's subject and content will contain (with a fresh activation) [:en]English Site Title[:de] Translated Title in the email. image

Expected Behavior Site title in email should be either 1. translated properly to the user's language or 2. Default to the website's default language.

Tested on

MKRD-SUPPORT commented 2 years ago

Let me know if you need an urgent solution to this.

kennypu commented 2 years ago

@MKRD-SUPPORT I don't, but thank you. For anyone else having this issue, here is how I have it fixed for the time being:

/** Make sure that blogname is converted properly  */
add_filter( 'option_blogname', function( $value ) {
    global $pagenow, $q_config;

    $ignore_pages = array(
        'options-general.php',
        'options.php',
    );

    if( in_array($pagenow, $ignore_pages) ) return $value;

    // qtranslate uses only the 2 character code (that can be set in back-end) so we need to convert
    // if it isn't available, default to 'en'
    $current_language_code = convert_locale_to_langcode( get_locale() ) ?? 'en';

    return qtranxf_use_language( $current_language_code, $value, false, false );
});

/**
 * Convert PHP/Wordpress Locale to q_config saved language code
 */
function convert_locale_to_langcode( $locale ) {
    global $q_config;

    if( empty($q_config) ) return false;

    return array_search( $locale, $q_config['locale'] );
}

Interestingly, you can find a similar code commented out in qtx_admin_utils.php:355, so I guess it was noticed but never finished.