Skip to content

Commit

Permalink
Merge pull request #8105 from Sesquipedalian/messageformat_ordinal_da…
Browse files Browse the repository at this point in the history
…te_time

Adds support for some missing MessageFormat types in our fallback code
  • Loading branch information
Sesquipedalian authored Feb 16, 2024
2 parents aaff400 + 7844aa5 commit 78dec26
Showing 1 changed file with 82 additions and 4 deletions.
86 changes: 82 additions & 4 deletions Sources/Localization/MessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

use SMF\Config;
use SMF\Lang;
use SMF\Time;
use SMF\User;
use SMF\Utils;

/**
Expand Down Expand Up @@ -179,10 +181,86 @@ public static function formatMessage(string $message, array $args = []): string

break;

// @todo
// case 'date':
// case 'time':
// break;
case 'ordinal':
$final .= self::formatMessage(Lang::$txt['ordinal'], [$args[$arg_name]]);
break;

case 'date':
if ($args[$arg_name] instanceof \DateTimeInterface) {
$args[$arg_name] = Time::createFromInterface($args[$arg_name]);
} elseif (is_numeric($args[$arg_name])) {
$args[$arg_name] = new Time('@' . $args[$arg_name], User::getTimezone());
} elseif (is_string($args[$arg_name])) {
$args[$arg_name] = date_create($args[$arg_name]);

if ($args[$arg_name] === false) {
$args[$arg_name] = new Time();
} else {
$args[$arg_name] = Time::createFromInterface($args[$arg_name]);
}
} else {
$args[$arg_name] = new Time();
}

// Trying to produce the same output as \IntlDateFormatter
// would require a lot of complex code, so we're just going
// for simple fallbacks here.
switch ($rest) {
case 'full':
case 'long':
$fmt = Time::getDateFormat();
$relative = true;
break;

case 'short':
$fmt = 'Y-m-d';
$relative = false;

case 'medium':
default:
$fmt = Time::getShortDateFormat();
$relative = true;
break;
}

$final .= $args[$arg_name]->format($fmt, $relative);
break;

case 'time':
if ($args[$arg_name] instanceof \DateTimeInterface) {
$args[$arg_name] = Time::createFromInterface($args[$arg_name]);
} elseif (is_numeric($args[$arg_name])) {
$args[$arg_name] = new Time('@' . $args[$arg_name], User::getTimezone());
} elseif (is_string($args[$arg_name])) {
$args[$arg_name] = date_create($args[$arg_name]);

if ($args[$arg_name] === false) {
$args[$arg_name] = new Time();
} else {
$args[$arg_name] = Time::createFromInterface($args[$arg_name]);
}
} else {
$args[$arg_name] = new Time();
}

// Trying to produce the same output as \IntlDateFormatter
// would require a lot of complex code, so we're just going
// for simple fallbacks here.
switch ($rest) {
case 'full':
case 'long':
$fmt = Time::getTimeFormat();
break;

case 'short':
case 'medium':
default:
$fmt = Time::getShortTimeFormat();
break;
}

$final .= $args[$arg_name]->format($fmt);
break;

// @todo
// case 'duration':
Expand Down

0 comments on commit 78dec26

Please sign in to comment.