简体   繁体   中英

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). 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

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.

Homestead provides an Ubuntu system. So first check which locales are available on your system with locale -a . Maybe the swedish locale is named sv_SE.iso88591 or sv_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(); should work. This would be the PHP-way to generate dates.

Don't forget to restart the server after you've installed the locale's described

sudo nginx restart

The Laravel-way

For the Laravel-way you need to generate the locals yourself. Start by adding a locale file in /app/lang/sv/days.php with:

<?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('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.

至少在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. Add to the composer.json file:

"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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