3

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?

I'm working on Ubuntu 15.10 in case if that matters

---------EDIT---------

For anyone reading this, option 2 has been implemented and released as version 1.0.12

Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57

1 Answers1

0

You can always create a key in the lang files to tell Carbon how to write the format.

Something like: $event->start_time->formatLocalized(trans('general.date_format'))

mcamara
  • 55
  • 1
  • 7
  • But this solution would change between 25.11.2015 and 11/25/2015, but not month or day name. But still a good solution for that, thanks – Bojan Kogoj Nov 25 '15 at 15:29