【问题标题】:Translating PHP weekdays and months翻译 PHP 工作日和月份
【发布时间】:2016-03-31 15:21:38
【问题描述】:

对于将 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 ,但我不明白如何使用它们..

提前谢谢你。

【问题讨论】:

  • 试试谷歌翻译 api。 cloud.google.com/translate
  • PHP 已经支持以不同语言格式化日期。我看到你已经发现了strftime()。它与setlocale() 一起工作。这两个函数的 PHP 文档都提供了使用示例。
  • @axiac,我添加了setlocale(LC_TIME, "fi_FI");,但它没有任何效果。平日继续以英语显示。
  • 作为第二个参数传递给setlocale() 的值取决于运行 PHP 解释器的操作系统。 Windows 使用不同的命名规则。此外,有时不会为所有语言安装区域设置信息。在类 Unix 系统上,它通常可以安装。
  • 检查this question。它的accepted answer 提供了有关如何找出用作 [setlocale()] 的第二个参数的值的信息

标签: php date for-loop momentjs


【解决方案1】:

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

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

【讨论】:

  • 我尝试在strftime() 之前添加setlocale(LC_TIME, "fi_FI");,但没有任何效果。工作日是英文的。
  • @lingo 查看它为您提供的更新,为您提供所需的结果
【解决方案2】:

我做了以下真正的翻译月份和工作日从和到任何语言(安装在服务器上)。

即使认为这个话题早已得到回答,我还是会发布它,因为它可能真的对某些人有所帮助。

setlocale() 被使用,但也被重置;所以它不会弄乱你的其余代码。

这些是您可以使用的功能:

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

【讨论】:

【解决方案3】:

你可以使用IntlDateFormatter,如下:

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

输出:

马南泰

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

马南泰纳

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    相关资源
    最近更新 更多