Skip to content

Commit 5eeecc7

Browse files
jedrzejdocs-codebackportbot[bot]
authored andcommitted
fix: add localization to uptime strings
Fixes #592 Signed-off-by: jedrzejdocs-code <jedrzejdocs@gmail.com>
1 parent f41a92a commit 5eeecc7

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

lib/Controller/ApiController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OCP\AppFramework\OCSController;
2323
use OCP\IConfig;
2424
use OCP\IGroupManager;
25+
use OCP\IL10N;
2526
use OCP\IRequest;
2627
use OCP\IUserSession;
2728

@@ -40,6 +41,7 @@ public function __construct(
4041
private DatabaseStatistics $databaseStatistics,
4142
private ShareStatistics $shareStatistics,
4243
private SessionStatistics $sessionStatistics,
44+
private IL10N $l10n,
4345
) {
4446
parent::__construct($appName, $request);
4547
}
@@ -131,19 +133,24 @@ private function getWebserver(): string {
131133
*/
132134
private function formatUptime(int $uptime): string {
133135
if ($uptime === -1) {
134-
return 'Unknown';
136+
return $this->l10n->t('Unknown');
135137
}
136138

137139
try {
138140
$boot = new \DateTime($uptime . ' seconds ago');
139141
} catch (\Exception $e) {
140-
return 'Unknown';
142+
return $this->l10n->t('Unknown');
141143
}
142144

143145
$interval = $boot->diff(new \DateTime());
144-
if ($interval->days > 0) {
145-
return $interval->format('%a days, %h hours, %i minutes, %s seconds');
146+
$days = $interval->days;
147+
$hours = $interval->h;
148+
$minutes = $interval->i;
149+
$seconds = $interval->s;
150+
151+
if ($days > 0) {
152+
return $this->l10n->t('%1$d days, %2$d hours, %3$d minutes, %4$d seconds', [$days, $hours, $minutes, $seconds]);
146153
}
147-
return $interval->format('%h hours, %i minutes, %s seconds');
154+
return $this->l10n->t('%1$d hours, %2$d minutes, %3$d seconds', [$hours, $minutes, $seconds]);
148155
}
149156
}

tests/lib/ApiControllerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\AppFramework\Http;
2222
use OCP\IConfig;
2323
use OCP\IGroupManager;
24+
use OCP\IL10N;
2425
use OCP\IRequest;
2526
use OCP\IUser;
2627
use OCP\IUserSession;
@@ -38,6 +39,7 @@ class ApiControllerTest extends \Test\TestCase {
3839
private DatabaseStatistics&MockObject $databaseStatistics;
3940
private ShareStatistics&MockObject $shareStatistics;
4041
private SessionStatistics&MockObject $sessionStatistics;
42+
private IL10N&MockObject $l10n;
4143

4244
protected function setUp(): void {
4345
parent::setUp();
@@ -53,6 +55,7 @@ protected function setUp(): void {
5355
$this->databaseStatistics = $this->createMock(DatabaseStatistics::class);
5456
$this->shareStatistics = $this->createMock(ShareStatistics::class);
5557
$this->sessionStatistics = $this->createMock(SessionStatistics::class);
58+
$this->l10n = $this->createMock(IL10N::class);
5659
}
5760

5861
private function getController($userSession) {
@@ -69,7 +72,8 @@ private function getController($userSession) {
6972
$this->fpmStatistics,
7073
$this->databaseStatistics,
7174
$this->shareStatistics,
72-
$this->sessionStatistics
75+
$this->sessionStatistics,
76+
$this->l10n
7377
);
7478
}
7579

0 commit comments

Comments
 (0)