翻译 PHP 个工作日和月份

Translating PHP weekdays and months

有什么想法可以将 PHP 工作日和月份翻译成当地语言吗?我有打印接下来 5 周工作日的脚本,我希望它们使用其他语言。

$timestamp = strtotime('next Monday');
for ($i = 0; $i < 35; $i++) {
    echo strftime('%A', $timestamp)." ";
    $timestamp = strtotime('+1 day', $timestamp);
}

是否有任何好的集成,例如PHP 和 moment.js?我看过 Use PHP's date() formats in moment.js GitHub fightbulc/moment.php ,但我不明白如何使用这些..

提前致谢。

您可能正在寻找以下内容:

http://php.net/manual/en/function.setlocale.php

http://php.net/manual/en/function.strftime.php

更新

setlocale(LC_TIME, "fi");
echo utf8_encode(strftime('%A'));

结果

perjantaina

这是送给你们的礼物 - the ISO language codes

您可以使用IntlDateFormatter,如下:

$fmt = new IntlDateFormatter('fin', IntlDateFormatter::FULL,
              IntlDateFormatter::NONE, null, null, "cccc"); 
$timestamp = strtotime('next Monday');
echo $fmt->format($timestamp);

输出:

maanantai

根据语法上下文,您可能需要 "eeee" 作为最后一个参数而不是 "cccc":

maanantaina

我做了以下内容,用于真正将月份和工作日与任何语言(安装在服务器上)相互翻译。

即使认为这个话题早就有人回答了,我也会 post 因为它可能真的对某些人有帮助。

setlocale()用了,也reset了;所以它不会弄乱你的其余代码。

这些是您可以使用的函数:

weekdayToEnglish($locale,$weekday,$short)
weekdayFromEnglish($locale,$weekday,$short)
monthToEnglish( $locale,$month,$short)
monthFromEnglish($locale,$month,$short)

$locale 是您在 setlocale() 中使用的语言,取决于您的服务器,主要类似于 'fr_FR';

$weekday$month 要翻译的月份的星期几的名称

(可选)$short 如果 true 给出(本地化的)缩写符号(例如 Thu 代替 Thursday 或 Oct 代替 Octobre)

代码:

function weekdayToEnglish($locale,$weekday,$short=false) {
    return dateElementToEnglish($locale, $weekday, $short ? "%a" : "%A",
                                $short? "D" : "l", 'tm_wday', "Sunday +" ," days") ;
}

function weekdayFromEnglish($locale,$weekday,$short=false) {
    return dateElementFromEnglish($locale, $weekday, $short?"%a":"%A");
}

function monthToEnglish( $locale,$month,$short=false) {
    return dateElementToEnglish($locale, $month, $short ? "%b" : "%B",
                                $short ? "M" : "F", 'tm_mon', "January+" ," months") ;
}

function monthFromEnglish($locale,$month,$short=false) {
    return dateElementFromEnglish($locale, $month, $short ? "%b" : "%B");
}

function dateElementToEnglish($locale, $text, $strfTimeformat, $dateFormat,
                              $dateArrayIndex, $strToTimePrefix, $strToTimeSuffix) {
    $saveLocale = setlocale(LC_TIME,0);setlocale(LC_TIME,$locale);
    $translateToNr = strptime($text, $strfTimeformat)[$dateArrayIndex] ;
    $readDate = strtotime($strToTimePrefix . $translateToNr . $strToTimeSuffix);
    $translation = date($dateFormat, $readDate);
    setlocale(LC_TIME,$saveLocale);
    return $translation;
}

function dateElementFromEnglish($locale,$text,$strfTimeformat) {
    $saveLocale = setlocale(LC_TIME,0);setlocale(LC_TIME,$locale);
    $translation = strftime($strfTimeformat,strtotime($text));
    setlocale(LC_TIME,$saveLocale);
    return $translation;
}

例如:

echo weekdayToEnglish("nl_NL","vrijdag")."<br>";
echo weekdayFromEnglish("fi_FI","Monday")."<br>";
echo weekdayToEnglish("nl_NL","wo", true)."<br>";
echo weekdayFromEnglish("de_DE","Thu", true)."<br>";
echo weekdayFromEnglish("fr_FR",weekdayToEnglish("nl_NL","zaterdag"))."<br>";
echo monthFromEnglish("de_DE", "March")."<br>";
echo monthFromEnglish("fr_FR", "February", true)."<br>";
echo monthToEnglish("fr_FR", "avril")."<br>";
echo monthToEnglish("fi_FI", "joulukuu", true)."<br>";

将产生:

Friday
maanantai
Wed
Do
samedi
März
févr.
April
Dec