I am not sure if I am doing this the right way but I want to avoid using a plugin. I have links on my page to change languages between Spanish and English. I am taking care of everything but when I get the date of the post, it is formatted in the locale of the Wordpress site, for instance:

September 21, 2017

From the Wordpress Codex I found this code to change the locale via a filter.

So I ended up writing this ($language is a global variable that holds the language currently selected by the user):

function set_my_locale( $lang ) {
global $language;
if ( $language == SPANISH_LOCALE) {
    // set to Spanish
    return 'es_ES';
else if ( $language == ENGLISH_LOCALE) {
    // set to English
    return 'en_US';

The function gets called and nothing is changed, the date is still shown in English.

Oh, I do have es_ES.mo and es_ES.po files inside the languages folder and I can switch to Spanish in the admin interface, so they are correctly installed.

I have run out of ideas, any clues why the dates are displayed in English regardless of what I do?

  • Did a bit more investigation and here's an interesting bit. In Wordpress: Settings --> General If Spanish is the language selected in 'Site Language' and my $language variable is set to SPANISH_LOCALE and I try to switch the dropdown to 'English', when I click save settings, it goes back to 'Spanish: goo.gl/Kw9QT5 But notice that the admin menu on the left is still in English. WTF. – csaborio Sep 21 '17 at 14:17
  • Figured it out, no need to use a filter to change the locale, simply use switch_to_locale() instead: if ($language == 'ES') { switch_to_locale('es_ES'); } else if ($language='EN') { switch_to_locale('en_EN'); } – csaborio Sep 21 '17 at 14:32
  • 1
    Possible duplicate of Change locale manually at runtime? – T.Todua Nov 4 '18 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.