简体   繁体   English

Laravel与Homestead的strftime无法正常工作

[英]Laravel strftime with Homestead ain't working

I've been having alot of trouble with formating a date with my localization using Laravel and Homestead (I don't know if homestead has any kinda server configurations which make it doesn't work but thats my though). 我在使用Laravel和Homestead进行本地化格式化日期时遇到了很多麻烦(我不知道homestead是否有任何某种服务器配置使其无法工作,但这就是我的意思)。 I guess anyone around the internet have been struggling with the same issue. 我想互联网上的任何人都在为同一问题而苦苦挣扎。 I've been searching for an answer for hours without getting anywhere closer to a solution.. This is my code. 我一直在寻找答案长达数小时之久,而没有找到任何解决方案的地方。这是我的代码。

public function formated_start()
{
    setlocale(LC_ALL, 'sv_SE');
    return strftime('%A %H:%M', strtotime($this->start));
}

I've also tried with laravel specific 我也尝试过使用laravel

public function formated_start()
{
    App::setLocale('sv_SE')
    return strftime('%A %H:%M', strtotime($this->start));
}

Still with the same result nothing changed and the %A which should be the day of week in my language in this case Swedish is still in English. 仍然具有相同的结果,并且没有任何变化,在我的语言中,%A应该是星期几,在这种情况下,瑞典语仍然是英语。

Homestead provides an Ubuntu system. Homestead提供了一个Ubuntu系统。 So first check which locales are available on your system with locale -a . 因此,首先使用locale -a检查系统上可用的locale -a Maybe the swedish locale is named sv_SE.iso88591 or sv_SE.utf8 . 瑞典语的语言环境可能名为sv_SE.iso88591sv_SE.utf8 If it is not installed you can install it with: 如果尚未安装,则可以使用以下命令进行安装:

sudo locale-gen sv_SE sv_SE.utf8

After installing your first solution with setlocale(); setlocale();安装第一个解决方案后setlocale(); should work. 应该管用。 This would be the PHP-way to generate dates. 这将是生成日期的PHP方法

Don't forget to restart the server after you've installed the locale's described 在安装了所描述的语言环境后,不要忘记重启服务器

sudo nginx restart

The Laravel-way Laravel方式

For the Laravel-way you need to generate the locals yourself. 对于Laravel方式,您需要自己生成本地人。 Start by adding a locale file in /app/lang/sv/days.php with: 首先在/app/lang/sv/days.php添加一个语言环境文件, /app/lang/sv/days.php包括:

<?php

return array(
    'Monday' => 'måndag',
    'Tuesday' => 'tisdag',
    'Wednesday' => '..',
    'Thursday' => '..',
    'Friday' => '..',
    'Saturday' => 'lördag',
    'Sunday' => '..',
);

Now you can use the App::setLocale() with Lang::get() : 现在您可以将App::setLocale()Lang::get()

App::setLocale('sv');
echo Lang::get(sprintf('days.%s', date('l')));

This is way more complex but if you are hosting Laravel on a shared hosting environment where you can't generate locales, this could be a possible way. 这是更复杂的方法,但是如果您将Laravel托管在无法生成语言环境的共享托管环境中,则可能是可行的方法。

至少在Windows中,这似乎对我有用。

setlocale(LC_ALL, 'sv_SE', 'Swedish', 'swedish');

If your problem is in general, instead of specifically using strftime you could try with Laravel4-lang , it's installed via composer. 如果您的问题一般,而不是专门使用strftime,您可以尝试使用Laravel4-lang ,它是通过composer安装的。 Add to the composer.json file: 添加到composer.json文件:

"require": { "laravel/framework": "4.2.*", "caouecs/laravel4-lang": "dev-master" },

Then update the package: 然后更新软件包:

composer update caouecs/laravel4-lang

Update 更新

This worked really good too: https://github.com/jenssegers/laravel-date 这也真的很好: https : //github.com/jenssegers/laravel-date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM