0
votes

I built a "month bar" for WordpRess tribe-events plugin. The bar shows the names of the next 12 months with links to the archive pages. I had some problems with month having less than 31/31 dayse but I could fix this with some help from the community here. My problem: the months I get back are in Englsich and I cant find a way to translate those month-names to German

I tried:

setlocale(LC_TIME, 'german', 'deu_deu', 'deu', 'de_DE', 'de');

And the whole thing:

setlocale(LC_TIME, 'german', 'deu_deu', 'deu', 'de_DE', 'de'); 
$currentmonth = date_i18n('M'); //localization 
date_i18n $todaysmonth = date_i18n('n', strtotime('now')); $todaysMonth = date_i18n('M', strtotime('now')); 

echo "<ul id='my-month-bar'>";

    for($i = $todaysmonth; $i <= $todaysmonth+11; $i++)
    {

    $dateObj = DateTime::createFromFormat( 'm', $i );
    $monthName = $dateObj->format('M'); //Ausgabe der Monate in Listew
    $my_date = $dateObj->format('Y-m-01'); //Datum fuer URL

        $current_ym = date_i18n('Y-m',strtotime("+$i months"));
        $m = date_i18n('M',strtotime("+$i months")); //localization date_i18n
        //$title = date_i18n('Y-m' . "-01",strtotime("+$i months"));
        $title = $my_date;
        $y = date_i18n('Y',strtotime("+$i months"));
        //echo "<a href='$url&tribe_bar_date=$title'>$m</a>";            
        $query = $url;
        $bar_date = get_query_var('tribe-bar-date');
        $new_query = add_query_arg( array(
        'tribe-bar-date' => $title,
        ), $query );
        if ( $bar_date == $my_date ){
        echo "<li><a title='Veranstaltungen im $monthName' rel='nofollow' id='my-monthbar-act' href='$new_query'> $monthName </a></li>"; //if is current month
        }else{
        echo "<li><a title='Veranstaltungen im $monthName' rel='nofollow' id='my-monthbar' href='$new_query'> $monthName </a></li>";
        }}
        echo "</ul>";
    ?></div>

What I get: SEP OCT NOV DEC JAN FEB MAR APR MAY JUN JUL AUG

What I want: SEP OKT NOV DEZ...

2

2 Answers

0
votes

You might have a look at WP_Locale::get_month, which you could use in your code by:

global $wp_locale;
$monthName = $wp_locale->get_month($i);
0
votes

I went back to an approach I used 2 months ago. I tested it with different start dates and it worked quite well. Im not 100% sure, there might still be this problem with months having less than 31 or 30 days. I tried to fix this:

$my = date_i18n("M", strtotime( date( 'Y-M-01' )." +$i months"));

I used different numbers for the index, like "-2" and saw no problems here.

Part of the code goes:

for ($i = 0; $i <= 11; $i++)    
            {
            $my = date_i18n("M", strtotime( date( 'Y-M-01' )." +$i months")); //get the months name for list, make sure it set to first day of month, 28 days problem
            $my_date = date("Y-m-d", strtotime( date( 'Y-m-01' )." +$i months"));

            $title = $my_date;
            $query = $url;
            $bar_date = get_query_var('tribe-bar-date');
            $new_query = add_query_arg( array('tribe-bar-date' => $title,), $query );

            //Link color for current month or current selection         
            if ( $bar_date == $my_date ){
            echo "<li><a title='Veranstaltungen im $my' rel='nofollow' id='my-monthbar-act' href='$new_query'> $my </a></li>"; //if is current month 
            }else{
            echo "<li><a title='Veranstaltungen im $my' rel='nofollow' id='my-monthbar' href='$new_query'> $my </a></li>";
            }
        }

(Just to make this complete The variable $url is build outside the loop)

// find current domain:
    $domain = $_SERVER['HTTP_HOST'];
    // path to the current file:
    $path = $_SERVER['SCRIPT_NAME'];
    // QueryString:
    $queryString = $_SERVER['QUERY_STRING'];
    // put it all together:
    //$url = "https://" . $domain . $path . "?" . $queryString;
    // An alternative way is to use REQUEST_URI instead of both
    // SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
    $url = "http://" . $domain . $_SERVER['REQUEST_URI'];
    //Ende aktuelle Domain finden