How to translate toDayDateTimeString method to other language?
Asked Answered
B

3

7

I want to have show dates translated in "de" and in this format:

 Thu, Mar 8, 2018 - 6:30

I have in "app.php":

'locale' => 'de',

Then in a view Im using:

{{$post->date->toDayDateTimeString()}}

But the result still appears in "en":

 Thu, Mar 8, 2018 6:30 AM

But for example using other carbon method: {$post->date->diffForHumans()}} the result appears in "de", but I dont want that format, I want the toDayDateTimeString() format.

Do you know how to also have this format toDayDateTimeString() translated another language? So is possible to show the date like "Thu, Mar 8, 2018 - 6:30" in another language?

Bartholomew answered 16/3, 2018 at 17:38 Comment(0)
B
4

In order to get the date translated to German, you'll have to set the appropriate locale, before using Carbon.

As a test in your view, this should be enough:

<?php setlocale(LC_TIME, 'de_DE'); ?>

{{ $post->date->formatLocalized('%a, %b %d, %Y %H:%M') }}

For other format options, refer to the strftime() function manual.

You will have to make sure that the locale you want is enabled in your system.

I'm gonna assume you're in a Linux environment, so you'll have to edit the locale.gen file (Arch Linux and Ubuntu have it in /etc/locale.gen, other distros might have it elsewhere), and uncomment the appropriate lines:

# de_DE ISO-8859-1
# de_DE.UTF-8 UTF-8

will have to be changed to

de_DE ISO-8859-1
de_DE.UTF-8 UTF-8

After updating the locale.gen file, you'll need to run locale-gen, in order for those new locales you've just uncommented to be available from PHP.

$ locale-gen

Now confirm that the new locales are available, with:

$ locale -a

Now, go back to your browser and you'll successfully see the date/time translated.

Edited: As we later found out, the toDayDateTimeString() method from the Carbon class, does not work with localisation, so the formatLocalized() has to be used.

Branks answered 24/3, 2018 at 16:43 Comment(10)
Thanks, that "setlocale(LC_TIME, 'de_DE');" is in the AppServiceProvider.php?Bartholomew
It has to be set before you call the toDayDateTimeString() method, so wherever you see fit. I guess the AppServiceProvider could be a good place for it.Branks
Thanks but its not working setting "setlocale(LC_TIME, 'de_DE');" in AppServiceProvider and then use "{{$post->date->toDayDateTimeString()}}". I check in osx the languages and regions setting and there is the language.Bartholomew
Also in "/usr/share/locale/" there are the files.Bartholomew
I have this in the AppServiceprovider: public function boot() { \Carbon\Carbon::setlocale(LC_TIME, 'de_DE'); }.Bartholomew
Make sure you definitely have the de_DE locales available. Use locale -a to list what you have currently available. In regards to the setlocale() function, I mean the native PHP one, not the Carbon method. As a test, call it just before you call the toDayDateTimeString() method. Once it works, move it to a proper place, like AppServiceProvider or other place you see appropriate.Branks
The de_DE are available. Like this "{{$post->date->setlocale(LC_TIME, 'de_DE')->toDayDateTimeString()}}" it appears "Call to a member function toDayDateTimeString() on boolean".Bartholomew
Thanks but I'm not understanding. Are you say to in the file where there is {{$post->date}} have before for example <?php setlocale(LC_TIME, 'de_DE') ?>? Because also dont works.Bartholomew
Like I previously said, you need to call setlocale(LC_TIME, 'de_DE') before you print the date/time. When running locale -a in the terminal, what does it show you? Is de_DE in the list? Again, follow the instructions. I can't make it simplerBranks
Let us continue this discussion in chat.Branks
D
1

You can use setLocale('de') method.

Here is an example

<?php 
\Carbon\Carbon::setLocale('de')      
?>


{{$post->date->toDayDateTimeString()}}

Hope this helps

Dextrad answered 16/3, 2018 at 17:50 Comment(3)
Thanks, but with that it appears "Call to a member function toDayDateTimeString() on boolean".Bartholomew
try <?php \Carbon::setLocale('de') ?>Dextrad
With "<?php \Carbon\Carbon::setLocale('de') ?>" I dont get any error.Bartholomew
S
1

Laravel comes with Carbon package, if you set locale in your app. Carbon will parse the date automatically with the language choosen.

Check this

Carbon::setLocale('de');
echo Carbon::getLocale();                          // de
echo Carbon::now()->addYear()->diffForHumans();    // in 1 Jahr

Carbon::setLocale('en');
echo Carbon::getLocale();                          // en
Scruggs answered 16/3, 2018 at 17:57 Comment(8)
Thanks, but with the toDayDateTimeString() method dont works, dont translates.Bartholomew
That method converts to string, it wont translate the date. For translation you need to setLocale with CarbonScruggs
You could set it globally. class AppServiceProvider extends ServiceProvider { public function boot() { // Localization Carbon \Carbon\Carbon::setLocale(config('app.locale')); } }Scruggs
Thanks, I have that "\Carbon\Carbon::setLocale(config('app.locale'));" in the AppServiceProvider and in "app.php" I have " 'locale' => 'de'," but "{{$post->date->toDayDateTimeString()}}" prints in "en".Bartholomew
can you try Carbon::parse($post->date)->toDateTimeString() ?Scruggs
It appears "2018-03-08 06:30:00".Bartholomew
i think this would work Carbon::parse($post->date)->toDayDateTimeString()Scruggs
Thanks, but still appear in "en".Bartholomew

© 2022 - 2024 — McMap. All rights reserved.