Skip to content

Commit df199f7

Browse files
authoredFeb 16, 2022
Added option to enable Carbon's translatedFormat (jamesmills#69)
1 parent ea5a5ca commit df199f7

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed
 

‎src/LaravelTimezoneServiceProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ function ($expression) {
5555
return "<?php echo e(Timezone::convertToLocal($options[0], $options[1])); ?>";
5656
} elseif (count($options) == 3) {
5757
return "<?php echo e(Timezone::convertToLocal($options[0], $options[1], $options[2])); ?>";
58+
} elseif (count($options) == 4) {
59+
return "<?php echo e(Timezone::convertToLocal($options[0], $options[1], $options[2], $options[3])); ?>";
5860
} else {
5961
return 'error';
6062
}

‎src/Timezone.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ class Timezone
1212
* @param bool $format_timezone
1313
* @return string
1414
*/
15-
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string
15+
public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false, $enableTranslation = null) : string
1616
{
1717
if (is_null($date)) {
1818
return __('Empty');
1919
}
2020

2121
$timezone = (auth()->user()->timezone) ?? config('app.timezone');
22-
22+
23+
$enableTranslation = $enableTranslation !== null ? $enableTranslation : config('timezone.enableTranslation');
24+
2325
$date->setTimezone($timezone);
2426

2527
if (is_null($format)) {
26-
return $date->format(config('timezone.format'));
28+
return $enableTranslation ? $date->translatedFormat(config('timezone.format')) : $date->format(config('timezone.format'));
2729
}
2830

29-
$formatted_date_time = $date->format($format);
31+
$formatted_date_time = $enableTranslation ? $date->translatedFormat($format) : $date->format($format);
3032

3133
if ($format_timezone) {
3234
return $formatted_date_time . ' ' . $this->formatTimezone($date);

‎src/config/timezone.php

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@
3939
*/
4040

4141
'format' => 'jS F Y g:i:a',
42+
43+
/*
44+
|--------------------------------------------------------------------------
45+
| Enable translated output
46+
|--------------------------------------------------------------------------
47+
|
48+
| Here you may configure if you would like to use translated output.
49+
|
50+
*/
51+
52+
'enableTranslation' => false,
4253

4354
/*
4455
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)
Please sign in to comment.