Are there a robust and crutch-less way to print Date
inside the ICU-message string, using ISO-8601 format - YYYY-MM-DDThh:mm:ss
?
Subquestion 1: are other custom format string supported?
Subquestion 2: why Intl.DateTimeFormat doesn't support custom formatting?
Details and observations
I want to use ICU-messages for i18n features for the javascript application. There are at least two packages on NPM, allowing to format message-strings:
Each has some kind of formatMessageFunction
, accepting the raw ICU-message, the name of locale and data for applying. But both uses Intl.DateTimeFormat (looks, like it is the industry standard):
and it can't format dates using custom format.
Despite the described limitations of mentioned implementations, the ICU describes a way of custom formatting for dates
Example
My ICU-string:
var icu_string = `Hello,
<strong>{username}</strong>,
it is
<time datetime="{ts, date, ???}">{ts, date}</time>
`;
Sample code:
var IntlMessageFormat = require('intl-messageformat'); // no matter, which NPM package to use
var formatter = new IntlMessageFormat(icu_string, 'en');
var output = formatter.format({
username: 'Barack Obama',
ts: new Date()
});
console.log(output);
The desirable output is Hello, <strong>Barack Obama</strong>, it is
<time datetime="2016-10-25T01:01:15Z">Oct 25, 2016</time>
You could test current behavior here: format-message.github.io
Similar questions
- question about ISO and ICU. But I don't want to play with
locale
parameter (the code will use the selected locale for translation).