【问题标题】:Translate a PHP monthbar to German将 PHP 月条翻译成德语
【发布时间】:2019-09-05 10:19:36
【问题描述】:

我为 WordpRess 部落事件插件构建了一个“月栏”。该栏显示未来 12 个月的名称以及存档页面的链接。我在月份少于 31/31 天时遇到了一些问题,但我可以在社区的帮助下解决这个问题。 我的问题:我回来的月份是英语,我找不到将这些月份名称翻译成德语的方法

我试过了:

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

还有整件事:

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>

我得到了什么: 9 月 10 月 11 月 1 月 2 月 3 月 4 月 5 月 6 月 7 月 8 月

我想要什么:SEP OKT NOV DEZ...

【问题讨论】:

    标签: php


    【解决方案1】:

    您可以查看WP_Locale::get_month,您可以通过以下方式在代码中使用它:

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

    【讨论】:

    • 这看起来很有前途@royarisse!它给了我整个月份的名字。是否有格式化字符串的选项?喜欢$monthName = $wp_locale-&gt;get_month('m', $i);
    • 你好@royarisse。我试过这个:$MonthName = $wp_locale-&gt;get_month($i);$monthName = $wp_locale-&gt;get_month_abbrev($MonthName); 这给了我:SEP OKT NOV DEZ 但不是 JAN FEB...
    • 我尝试了一种新(旧)方法并将其写下来。你说这行得通吗?
    【解决方案2】:

    我回到了 2 个月前使用的方法。我用不同的开始日期对其进行了测试,效果很好。我不是 100% 确定,如果月份少于 31 或 30 天,可能仍然存在这个问题。我试图解决这个问题:

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

    我为索引使用了不同的数字,例如“-2”,在这里没有发现任何问题。

    部分代码如下:

    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>";
                }
            }
    

    (只是为了完成这个变量 $url 是在循环外构建的)

    // 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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多