2

Hey everyone, i need some help on translation carbon values date, time etc....,

for example using {{ date('M d, Y',strtotime($something->created at)) }} then it will be displayed as —> Apr 03, 2021

my question how can we translate "Apr" to other languages, i have spent more than 3 days on this case searching for a possible way in the last 3 days, but there was no luck :(, if you could help me and the other who need this solution

also i have tried more than one way for this such as writing this on providers.php

$app = $this->app && method_exists($this->app, 'getLocale') ? $this->app : app('translator');
        $locale = $app->getLocale();
        Carbon::setLocale($locale);
        CarbonImmutable::setLocale($locale);
        CarbonPeriod::setLocale($locale);
        CarbonInterval::setLocale($locale);
Mohammed B
  • 31
  • 1
  • 1
  • you can't use date() to translate, you should format it with Carbon object e.g. $something->created_at->locale('ab_cd')->format('dddd MMMM YYYY') – Anurat Chapanond Apr 03 '21 at 03:36
  • try something like `now()->locale($locale)->translatedFormat('M d, Y');`. https://carbon.nesbot.com/docs/#api-localization – porloscerros Ψ Apr 03 '21 at 03:36

2 Answers2

0

Have you tried using formatLocalized method? Google it

Alican Ali
  • 181
  • 2
  • 5
0

You need to use the setlocale function before setting the localized format in Carbon. The problem in your code is that, the way you are setting the locale, it is stateless. It is not being saved.

setlocale(LC_TIME, 'German');                     
echo $date->formatLocalized('%A %d %B %Y');

See https://stackoverflow.com/a/20058254/8607640

Another solution for you could be saving the locale in session and thus making it saved for the session.

Farhan Ibn Wahid
  • 926
  • 1
  • 9
  • 22