Feature: add date formatting option in custom api (#2228)

pull/2230/head
Kirill Kuznetsov 1 year ago committed by GitHub
parent f773e026d5
commit 3c4818a2b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,9 +31,13 @@ widget:
another: key3 another: key3
label: Field 3 label: Field 3
format: percent # optional - defaults to text format: percent # optional - defaults to text
- field: key # needs to be YAML string or object
label: Field 4
format: date # optional - defaults to text
dateStyle: long # optional - defaults to "long". Allowed values: `["full", "long", "medium", "short"]`.
``` ```
Supported formats for the values are `text`, `number`, `float`, `percent`, `bytes` and `bitrate`. Supported formats for the values are `text`, `number`, `float`, `percent`, `bytes`, `bitrate` and `date`.
## Example ## Example

@ -123,6 +123,9 @@ module.exports = {
i18next.services.formatter.add("percent", (value, lng, options) => i18next.services.formatter.add("percent", (value, lng, options) =>
new Intl.NumberFormat(lng, { style: "percent", ...options }).format(parseFloat(value) / 100.0), new Intl.NumberFormat(lng, { style: "percent", ...options }).format(parseFloat(value) / 100.0),
); );
i18next.services.formatter.add("date", (value, lng, options) =>
new Intl.DateTimeFormat(lng, { ...options }).format(new Date(value)),
);
}, },
type: "3rdParty", type: "3rdParty",
}, },

@ -10,7 +10,8 @@
"bibitrate": "{{value, rate(bits: true; binary: true)}}", "bibitrate": "{{value, rate(bits: true; binary: true)}}",
"percent": "{{value, percent}}", "percent": "{{value, percent}}",
"number": "{{value, number}}", "number": "{{value, number}}",
"ms": "{{value, number}}" "ms": "{{value, number}}",
"date": "{{value, date}}"
}, },
"widget": { "widget": {
"missing_type": "Missing Widget Type: {{type}}", "missing_type": "Missing Widget Type: {{type}}",

@ -69,6 +69,9 @@ function formatValue(t, mapping, rawValue) {
case "bitrate": case "bitrate":
value = t("common.bitrate", { value }); value = t("common.bitrate", { value });
break; break;
case "date":
value = t("common.date", { value, dateStyle: mapping?.dateStyle ?? "long" });
break;
case "text": case "text":
default: default:
// nothing // nothing

Loading…
Cancel
Save