1

How do I customize the translation of the Month name strings in WordPress?

example

"January" be "Jan11" in all of website

  • 1
    Elaborate your question with code where you want this change. – Qaisar Feroz Mar 13 at 9:22
  • Hello Qaisar, There is no code ,i need to change the all of string names of months on all of the website – Muhammed Nasser Mar 13 at 9:27
  • Is it wordpress site? – Qaisar Feroz Mar 13 at 9:28
  • yes it a wordpress site – Muhammed Nasser Mar 13 at 9:34
  • 1
    The PHP code in Wordpress displays the name of months as specified in the locale of the server it runs on. A locale specifies date and time preferences, number formatting and lots more. It is usually associated with a language. If you would like to change e.g. English month names to German, you would change the language of the WP installation to German (in Settings/General). You can't just replace any month name by another. For this to work, the month names would need to go through a .mo/.po translation using the __ (double underscore) function. – Jos Mar 13 at 10:09
2

Add the following to your existing functions.php file:

function ra_change_translate_text( $translated_text ) {
    if ( $translated_text == 'January' ) {
        $translated_text = 'Jan11';
    }
    return $translated_text;
}
add_filter( 'gettext', 'ra_change_translate_text', 20 );

If you want to change multiple strings, use this function instead:

function ra_change_translate_text_multiple( $translated ) {
    $text = array(
        'January' => 'Jan11',
        'February' => 'Feb22',
        'March' => 'Mar33',
    );
    $translated = str_ireplace(  array_keys($text),  $text,  $translated );
    return $translated;
}
add_filter( 'gettext', 'ra_change_translate_text_multiple', 20 );

You can add as many strings as you want, for example:

'April' => 'Apr44',

And the new translation can also be in a foreign language. Here's an example with Arabic:

'May' => 'مايو',

Last notes: If you use this function, keep in mind this will change the string translation everywhere in your WordPress site: front and back end. January will become Jan11 everywhere. Try it and see.

If that is acceptable to you then you're good to go. Otherwise, if you were looking to change the strings only on the front end (for your visitors to see), then consider making the changes to your theme or WordPress website language settings (which can be different from the admin language) - as suggested in some of the comments above.

Also, for reference, I learned about this function here: https://ronangelo.com/change-or-translate-text-on-a-wordpress-theme/. It is a very good post and if you want further information on translating text or themes in WordPress I recommend reading it.

Update to the original answer:

This function can only be used with words that are translatable - meaning, those wrapped in these functions ( __(), _e() )...

However, your comment and the website you linked to have given me a clearer idea of what you are trying to achieve. Simply change the locale of the PHP used to show that date to ar-LB.UTF-8. Lebanon is one of the Arabic locales that uses أذار for March, whereas other locales use مارس. The php locale will then render the date as you want it with no need for digging into translations and strings.

So, in your WordPress theme, use this code where you want to display the date instead of the code you are already using:

<?php 
   setlocale(LC_ALL, 'ar_LB.UTF-8');
   echo strftime("%e %B %Y");
?>

In the source code of the page you linked to, the date is displayed within these lines of code:

enter image description here

Depending on how you organized your theme, that bit could be in header.php or maybe even a template part. Find it and you will see that the old PHP code to echo the date will be there between the tags. Paste the snippet I gave you in its place.

Do keep in mind that if this theme is not your own unique one and you're not using a child theme you might lose that modification if the parent theme is updated.

I hope this helps. Please let me know if you need further assistance.

  • I have updated my answer, please try the solution offered there and let us know how it works in case someone else is trying to achieve the same result. – jsmod Mar 15 at 9:29
  • Many thanks for help , but where i should to paste the code which change the local date functions.php of the theme or server ? – Muhammed Nasser Mar 15 at 9:36
  • Paste the whole snippet together wherever you want to display the date in your theme. So, for example, if you are displaying the date in your header.php file, paste it there. I will update the answer now with a screenshot to assist you. – jsmod Mar 15 at 9:43
  • Thanks jsmod for fast replying and ur helps This is a plugin (which show the date) and this plugin appears the date based on wordpress this is the message on the plugin "Todays date (based on your WordPress General Settings)" the plugin called "WP Cloudy" – Muhammed Nasser Mar 15 at 9:53
  • Glad to be of assistance. Ok, then this plugin is using the language locale you picked in Settings > General > Site Language. WordPress has two Arabic locales, plain ar and Moroccan Arabic ar_MA. Both use مارس for March. So, you either have to find a Lebanese Arabic translation file for WordPress, OR (the simpler option) disable the plugin and just add that snippet to your theme as described above. I have used that snippet and another to add Hijri date to an Arabic website without problems, it is very convenient and lessens reliance on plugins for simple tasks. – jsmod Mar 15 at 10:03
0

The right approach is as suggested by @Jos in a comment to your question , or you can write a function where you can replace the date string extracted from WordPress functions like get_the_date() for desired output.

Here is an example function that may help.

function my_custom_date($date){
    $new_date = getdate(strtotime($date));

    // Array that holds customized month's name
    $my_months=[
            'January'   => '1st Month',
            'February'  => '2nd Month',
            'March'     => '3rd Month',
            'April'     => '4th Month',
            'May'       => '5th Month',
            'June'      => '6th Month',
            'July'      => '7th Month',
            'August'    => '8th Month',
            'September' => '9th Month',
            'October'   => '10th Month',
            'November'  => '11th Month',
            'December'  => '12th Month',
        ];



        $new_date['month']= $my_months[$new_date['month']];

        return $new_date['month'].' '.$new_date['mday']. ', '. $new_date['year'];

    }

Then you can use this function to display dates in your site. For example,

echo my_custom_date( get_the_date() ); // Post publish date, within a loop

echo my_custom_date( "May 10, 2017" ); // Outputs: 5th Month 10, 2017

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.