Skip to content

Commit

Permalink
Fix localization issues for dates with translated day, month names
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Aug 15, 2024
1 parent 9e0028a commit 9397ee7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/Core/Middleware/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle(IncomingRequest $request, Closure $next): Response

CarbonImmutable::mixin(new CarbonMacros(
$this->config->defaultTimezone,
session("companysettings.language"),
str_replace("-", "_", session("companysettings.language")),
$this->language->__("language.dateformat"),
$this->language->__("language.timeformat")
));
Expand All @@ -58,7 +58,7 @@ public function handle(IncomingRequest $request, Closure $next): Response
// Set macros for CabonImmutable date handling
CarbonImmutable::mixin(new CarbonMacros(
session("usersettings.timezone"),
session("usersettings.language"),
str_replace("-", "_", session("usersettings.language")),
session("usersettings.date_format"),
session("usersettings.time_format")
));
Expand Down
8 changes: 4 additions & 4 deletions app/Core/Support/CarbonMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function formatDateForUser(): \Closure
$mixin = $this;
return function () use ($mixin): String {
return self::this()
->setTimezone($mixin->userTimezone)
->locale($mixin->userLanguage)
->format($mixin->userDateFormat);
->setTimezone($mixin->userTimezone)
->translatedFormat($mixin->userDateFormat);
};
}

Expand All @@ -76,7 +76,7 @@ public function formatTimeForUser(): \Closure
return self::this()
->setTimezone($mixin->userTimezone)
->locale($mixin->userLanguage)
->format($mixin->userTimeFormat);
->translatedFormat($mixin->userTimeFormat);
};
}

Expand All @@ -94,7 +94,7 @@ public function format24HTimeForUser(): \Closure
return self::this()
->setTimezone($mixin->userTimezone)
->locale($mixin->userLanguage)
->format("H:i");
->translatedFormat("H:i");
};
}

Expand Down
11 changes: 6 additions & 5 deletions app/Core/Support/DateTimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function __construct($time = null, $tz = null)

// Session is set in middleware, unlikely to not be set but just in case set defaults.
$this->userTimezone = session("usersettings.timezone") ?? $this->config->defaultTimezone;
$this->userLanguage = session("usersettings.language") ?? $this->config->language;
$this->userLanguage = str_replace("-", "_", (session("usersettings.language") ?? $this->config->language));

$this->userDateFormat = session("usersettings.date_format") ?? $this->language->__("language.dateformat");
$this->userTimeFormat = session("usersettings.time_format") ?? $this->language->__("language.timeformat");
}
Expand All @@ -77,15 +78,15 @@ public function parseUserDateTime(string $userDate, string $userTime = ""): Carb
}

if ($userTime == "start") {
$this->datetime = CarbonImmutable::createFromFormat("!" . $this->userDateFormat, trim($userDate), $this->userTimezone)
$this->datetime = CarbonImmutable::createFromLocaleFormat("!" . $this->userDateFormat, substr($this->userLanguage, 0, 2), trim($userDate), $this->userTimezone)
->startOfDay();
} elseif ($userTime == "end") {
$this->datetime = CarbonImmutable::createFromFormat("!" . $this->userDateFormat, trim($userDate), $this->userTimezone)
$this->datetime = CarbonImmutable::createFromLocaleFormat("!" . $this->userDateFormat, substr($this->userLanguage, 0, 2), trim($userDate), $this->userTimezone)
->endOfDay();
} elseif ($userTime == "") {
$this->datetime = CarbonImmutable::createFromFormat("!" . $this->userDateFormat, trim($userDate), $this->userTimezone);
$this->datetime = CarbonImmutable::createFromLocaleFormat("!" . $this->userDateFormat, substr($this->userLanguage, 0, 2), trim($userDate), $this->userTimezone);
} else {
$this->datetime = CarbonImmutable::createFromFormat("!" . $this->userDateFormat . " " . $this->userTimeFormat, trim($userDate . " " . $userTime), $this->userTimezone);
$this->datetime = CarbonImmutable::createFromLocaleFormat("!" . $this->userDateFormat . " " . $this->userTimeFormat, substr($this->userLanguage, 0, 2), trim($userDate . " " . $userTime), $this->userTimezone);
}

return $this->datetime;
Expand Down

0 comments on commit 9397ee7

Please sign in to comment.