mercredi 25 novembre 2015

Carbon with Laravellocalization not using correct locale

I am using mcamara/laravel-localization on my project, and I'm having an issue with Carbon not recognizing locale. When I use

$event->start_time->formatLocalized('%A %d %B %Y')

it always displays English format, even though I get 'de' when I try

App::getLocale()

Obviously this is because carbon uses locale from operating system, and my OS has 'de_DE.utf8', so unless I do

setlocale(LC_TIME, 'de_DE.utf8')

it doesn't work. I obviously can't use that this way because I use multiple languages.

  • Option 1 (the bad and easy, but works):

Define the following in routes.php (or somewhere else)

if(App::getLocale()=="de")
    setlocale(LC_TIME, 'de_DE.utf8');
else if(App::getLocale()=="hr")
    setlocale(LC_TIME, 'hr_HR.utf8');
else 
    setlocale(LC_TIME, 'en_EN.utf8');

  • Option 2 (the long and possibly better solution? Feels the correct one)

Since I have this defined in routes.php

'prefix' => LaravelLocalization::setLocale(),

I could extend setLocale() to set LC_TIME to the de_DE or anything else. But to do that I would need to extend add 'regional' to config/laravellocalization.php from

'de' => ['name' => 'German','script' => 'Latn', 'native' => 'Deutsch'],

to

'de' => ['regional' => 'de_DE.utf8', 'name' => 'German','script' => 'Latn', 'native' => 'Deutsch'],

I would also need to create a method such as this in LaravelLocalization.php (obviously I would extend the class)

public function getCurrentLocaleRegional()
{
    return $this->supportedLocales[ $this->getCurrentLocale() ][ 'regional' ];
}

Now I would like to know, which way is better? Or, is there another (easier) solution to this problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire