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

feat(utilities/date-and-time/relative): add localization for "just now" #17878

1 change: 0 additions & 1 deletion packages/utilities-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"build": "yarn build:es & yarn build:cjs & yarn build:types",
"watch": "yarn build:es --watch & yarn build:cjs --watch",
"clean": "rimraf es lib",
"test": "echo yay && exit 0",
"postinstall": "ibmtelemetry --config=telemetry.yml"
},
"peerDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"build": "yarn build:es & yarn build:cjs & yarn build:types",
"watch": "yarn build:es --watch & yarn build:cjs --watch",
"clean": "rimraf es lib",
"test": "echo yay && exit 0",
"postinstall": "ibmtelemetry --config=telemetry.yml"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions packages/utilities/src/dateTimeFormat/relative-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ const tests = [
describe(locale, () => {
styles.forEach((style) => {
describe(style, () => {
const nowFormatted = new Intl.RelativeTimeFormat(locale, {
style,
numeric: 'auto',
}).format(0, 'seconds');
test(`-30 seconds → ${nowFormatted}`, () => {
const actualOutput = relative.format(Date.now() - 1000 * 30, {
locale,
});

expect(actualOutput).toBe(nowFormatted);
});

const rtf = new Intl.RelativeTimeFormat(locale, { style });

tests.forEach(([unit, unitsPassed, secondsInUnit]) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/utilities/src/dateTimeFormat/relative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ export function format(
const years = Math.floor(days / 365);

if (Math.abs(seconds) < 60) {
// TODO: localize
return 'just now';
return new Intl.RelativeTimeFormat(options?.locale, {
numeric: 'auto',
style: options?.style ?? 'long',
}).format(0, 'seconds');
}

if (Math.abs(minutes) < 60) {
Expand Down
Loading