Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for some missing MessageFormat types in our fallback code #8105

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading