admin管理员组

文章数量:1326123

I'm trying to translate the month names as underlined in this image, without changing the entire WP backend as well, as I require the backend to remain English.

I managed to translate the default strings via's Astra's Default String page, but they don't provide strings for the month names.

Any help would be appreciated!

Thank you :)

I'm trying to translate the month names as underlined in this image, without changing the entire WP backend as well, as I require the backend to remain English.

I managed to translate the default strings via's Astra's Default String page, but they don't provide strings for the month names.

Any help would be appreciated!

Thank you :)

Share Improve this question edited Aug 12, 2020 at 14:35 QQQ asked Aug 12, 2020 at 14:21 QQQQQQ 114 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

One easy solution, if you want the frontend to be a different language than the backend, is to change the site language under Settings > General (Spanish for example) and then change your user account's language by going to Users > Your Profile and changing the Language setting to English.

In addition, you can add this code to functions.php to make sure the language is English for you (or any other administrator) on the frontend. You could tweak the logic if you don't want it to apply to all administrators.

/**
 * Sets the locale to English on the frontend if the current user is an administrator.
 *
 * @param string $locale The locale ID.
 *
 * @return string
 */
function sx372871_set_administrator_locale( $locale ) {
    // If the current user is an admin, make sure locale is set to English.
    if ( current_user_can('administrator') && ! is_admin()  ) {
        return 'en_US';
    }

    return $locale;
}
add_filter( 'locale', 'sx372871_set_administrator_locale' );

The following code will translate month names from English to Spanish on the frontend:

/**
 * Translates month names on the frontend of the site.
 */
function sx372871_translate_month_names( $translated_text ) {
    if ( is_admin() ) {
        return $translated_text;
    }

    $months = array(
        'january'   => 'Enero',
        'february'  => 'Febrero',
        'march'     => 'Marzo',
        'april'     => 'Abril',
        'may'       => 'Mayo',
        'june'      => 'Junio',
        'july'      => 'Julio',
        'august'    => 'Agosto',
        'september' => 'Septiembre',
        'october'   => 'Octubre',
        'november'  => 'Noviembre',
        'december'  => 'Diciembre',
    );

    if ( array_key_exists( strtolower( $translated_text ), $months ) ) {
        return $months[strtolower($translated_text)];
    }

    return $translated_text;
}

add_filter( 'gettext', 'sx372871_translate_month_names' );

本文标签: translationHow do I translate month names in post metadata