Laravel 5 - 如何将本地碳时间设置为德语

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32202017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:14:55  来源:igfitidea点击:

Laravel 5 - How to setlocale Carbon time to German

phplaravellocalizationphp-carbon

提问by Wolfgang Müller

I am using Carbon, and I don't know how to change output to the German Time Format.

我正在使用 Carbon,但不知道如何将输出更改为德国时间格式。

Should you make the change in Controller or in the view?

您应该在控制器中还是在视图中进行更改?

Now I want the DayName as Germanstring. That's my default output:

现在我想要 DayName 作为 Germanstring。这是我的默认输出:

{{ $game->start_at }}

when I change in view to

当我改变观点时

{{ $game->start_at->format('l') }}

I get the DayName but not in German.

我得到了 DayName 但不是德语。

回答by Chathushka

Your answer is here I think. Remember to check the documentation always when you are in trouble :D

我想你的答案就在这里。遇到麻烦时,请记住始终检查文档:D

http://carbon.nesbot.com/docs/#api-localization

http://carbon.nesbot.com/docs/#api-localization

setlocale(LC_TIME, 'German');

$dt = Carbon::now();

echo $dt->formatLocalized('%A %d %B %Y');

回答by Wolfgang Müller

I have found my error, i had

我发现了我的错误,我有

setlocale(LC_TIME, 'de_DE')

the correct syntax is

正确的语法是

setlocale(LC_TIME, 'German');

and I put it to bootstrap/app.php So it′s working.

我把它放到 bootstrap/app.php 所以它的工作。

回答by Mathias

Maybe someone is looking for converting carbon date into readable german Month:

也许有人正在寻找将碳日期转换为可读的德国月份:

if ( ! function_exists( 'convert_to_german_month' ) ) {
    /**
     * Converts given Carbon date into German Month
     * Output example: "Januar"
     *
     * @param \Carbon\Carbon $date
     * @return string
     */
    function convert_to_german_month( \Carbon\Carbon $date ) : string {
        $month_mapping = [
            'January' => 'Januar',
            'February' => 'Februar',
            'March' => 'M?rz',
            'April' => 'April',
            'May' => 'Mai',
            'June' => 'Juni',
            'July' => 'Juli',
            'August' => 'August',
            'September' => 'September',
            'October' => 'Oktober',
            'November' => 'November',
            'December' => 'Dezember'
        ];
        return $month_mapping[strftime('%B', strtotime($date))];
    }
}