0

Trying to serve all dates in website as another language. But It doesn't make any change.

I set 'locale' => 'tr', in config/app.php file.

My AppServiceProvider.php below:

 public function boot(\Illuminate\Contracts\View\Factory $factory)
    {
        $factory->composer('*', 'App\Http\Views\Composers\AuthenticatedComposer');
        Carbon::setLocale('tr');
    }

And my resource file like:

  public function toArray($request)
    {
        Carbon::setLocale('tr');
        $user = Auth::user();
        setlocale(LC_TIME, 'Turkish');
        return [
            "id" => $this->id,
            "customer" => $this->customer,
            "vehicle" => $this->vehicle,
            "basket" => $this->basket,
            "total" => $this->total,
            "discount" => $this->discount,
            "net_total" => $this->total-$this->discount,
            "status" => $this->status,
            "created_at" => Carbon::parse($this->created_at)->formatLocalized('%A %d %B %Y'),
            "edit_allowed" => $user->power('process', 'edit'),
            "delete_allowed" => $user->power('process', 'delete'),
            "detail_allowed" => $user->power('process', 'detail')
        ];
    }

I expected date to be converted Turkish. But It returns English date like: "Sunday 06 September 2020"

What's wrong with it?

1 Answer 1

0

I think its supposed to be the language code like this: tr-TR

Unless its because the intl extension is not on the server. In that case try installing that "sudo apt-get install php-intl"

3
  • I change to tr-TR and response didn't change again. php-intl is installed, I've seen it in modules. (As I know, If intl is not installed, it would return empty. but it returns english)
    – TCS
    Sep 7, 2020 at 7:14
  • In that case om not sure :( Note though, that when you set "Carbon::setLocale('tr');" and later use it in "created_at" => Carbon::parse($this->created_at)->formatLocalized('%A %d %B %Y'), It wont be the same instance of carbon. You should add the set locale in the "create_at" line to make sure the setting is on the same instance. Or take the return of "Carbon::setLocale('tr')" so you use an instance of carbon you have set the locale for. "created_at" => Carbon::setLocale('tr')->parse($this->created_at)->formatLocalized('%A %d %B %Y') Sep 7, 2020 at 13:53
  • setLocale returns a bool, so we cannot call parse()
    – realtebo
    Mar 26, 2021 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.