Source: https://paraglidejs.com/formatting

# Formatting

Paraglide supports locale-aware formatting in message declarations when you use the **inlang message format plugin**.

Built-in formatter names:

- `plural` (uses `Intl.PluralRules`)
- `number` (uses `Intl.NumberFormat`)
- `datetime` (uses `Intl.DateTimeFormat`)
- `relativetime` (uses `Intl.RelativeTimeFormat`)
- `date` and `time` (use `Intl.DateTimeFormat`, see [ICU MessageFormat 1 styles](#icu-messageformat-1-styles))
- `icu:pound` (ICU MessageFormat 1 `#` in plurals, formats like `number`)

> [!NOTE]
> The formatter names are exactly `plural`, `number`, `datetime`, `relativetime`, `date`, `time`, and `icu:pound`. Unknown formatters are ignored with a warning: the value is interpolated without formatting.

## Number formatting

Use a local declaration to format a numeric input:

```json
{
  "personal_balance": [{
    "declarations": [
      "input amount",
      "local formattedAmount = amount: number"
    ],
    "match": {
      "amount=*": "Your balance is {formattedAmount}."
    }
  }]
}
```

`number` forwards options to `Intl.NumberFormat`:

```json
{
  "balance_fixed_decimals": [{
    "declarations": [
      "input amount",
      "local formattedAmount = amount: number minimumFractionDigits=2 maximumFractionDigits=2"
    ],
    "match": {
      "amount=*": "Balance: {formattedAmount}."
    }
  }],
  "price_in_usd": [{
    "declarations": [
      "input amount",
      "local formattedAmount = amount: number style=currency currency=USD"
    ],
    "match": {
      "amount=*": "Price: {formattedAmount}"
    }
  }]
}
```

## Date and time formatting

Use `datetime` for date/time values:

```json
{
  "purchase_date": [{
    "declarations": [
      "input date",
      "local formattedDate = date: datetime day=2-digit month=2-digit year=numeric"
    ],
    "match": {
      "date=*": "Purchase date: {formattedDate}."
    }
  }]
}
```

`datetime` forwards options to `Intl.DateTimeFormat`:

```json
{
  "event_start": [{
    "declarations": [
      "input date",
      "local formattedDate = date: datetime dateStyle=long timeStyle=short timeZone=UTC"
    ],
    "match": {
      "date=*": "Starts: {formattedDate}"
    }
  }]
}
```

Without options that pick what to show (`dateStyle`, `timeStyle`, or fields like `year`, `month`, `hour`), the formatters use these defaults:

| Formatter | Default | Example (`en`) |
| --- | --- | --- |
| `datetime` | `dateStyle=medium timeStyle=short` | "Oct 9, 2026, 4:30 PM" |
| `date` | `style=medium` | "Oct 9, 2026" |
| `time` | `style=medium` | "4:30:00 PM" |

The `datetime` and `date` defaults are those of MessageFormat 2 (LDML 47). `time` keeps ICU MessageFormat 1's `medium`, because `{d, time}` imports as `time`. MessageFormat 2's `:time` defaults to `short`: write `time style=short` for it. Options that pick nothing, such as `timeZone` or `hour12`, keep the defaults.

> [!TIP]
> Use an explicit `timeZone` if output must be stable across environments.

## Relative time formatting

Use `relativetime` for relative durations such as past updates, upcoming deadlines, and "today"/"tomorrow" labels:

```json
{
  "last_seen": [{
    "declarations": [
      "input duration",
      "local formattedDuration = duration: relativetime unit=day numeric=auto"
    ],
    "match": {
      "duration=*": "Last seen {formattedDuration}."
    }
  }],
  "delivery_window": [{
    "declarations": [
      "input duration",
      "local formattedDuration = duration: relativetime unit=day"
    ],
    "match": {
      "duration=*": "Arrives {formattedDuration}."
    }
  }]
}
```

With an English locale, `last_seen({ duration: -1 })` can render "Last seen yesterday.", and `delivery_window({ duration: 2 })` can render "Arrives in 2 days." Negative values are in the past; positive values are in the future.

`unit` is required by Paraglide. It is not an `Intl.RelativeTimeFormatOptions` property because `Intl.RelativeTimeFormat` accepts the unit as a separate argument. Use a literal unit or a dynamic input with `$inputName`:

```json
{
  "relative_update": [{
    "declarations": [
      "input duration",
      "input unit",
      "local formattedDuration = duration: relativetime unit=$unit style=short"
    ],
    "match": {
      "duration=*,unit=*": "Updated {formattedDuration}."
    }
  }]
}
```

`relative_update({ duration: -3, unit: "hour" })` can render "Updated 3 hr. ago." Invalid dynamic units fail at runtime with the platform `Intl.RelativeTimeFormat` behavior.

Supported units are `year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, and `second`. Plural forms such as `days` are accepted by `Intl.RelativeTimeFormat`, but singular forms are recommended for consistency.

`relativetime` forwards supported `Intl.RelativeTimeFormatOptions` such as `localeMatcher`, `numeric`, and `style`:

```json
{
  "relative_calendar_day": [{
    "declarations": [
      "input duration",
      "local formattedDuration = duration: relativetime unit=day numeric=auto"
    ],
    "match": {
      "duration=*": "{formattedDuration}"
    }
  }],
  "relative_short_update": [{
    "declarations": [
      "input duration",
      "local formattedDuration = duration: relativetime unit=hour style=short"
    ],
    "match": {
      "duration=*": "Updated {formattedDuration}."
    }
  }]
}
```

With `numeric=auto`, `-1 day` can become "yesterday", `0 day` can become "today", and `1 day` can become "tomorrow".

Locale overrides work the same as other message calls:

```ts
import { m } from "./paraglide/messages.js";

m.last_seen({ duration: -1 }, { locale: "de" });
```

Paraglide formats the `(value, unit)` pair you pass. Automatic threshold selection is caller logic, so compute both values before calling the message:

```ts
import { m } from "./paraglide/messages.js";

function toRelativeDuration(minutes: number) {
  const absoluteMinutes = Math.abs(minutes);

  if (absoluteMinutes >= 60 * 24) {
    return {
      duration: Math.round(minutes / (60 * 24)),
      unit: "day",
    };
  }

  if (absoluteMinutes >= 60) {
    return {
      duration: Math.round(minutes / 60),
      unit: "hour",
    };
  }

  return {
    duration: minutes,
    unit: "minute",
  };
}

const relative = toRelativeDuration(-125);
m.relative_update(relative);
```

Paraglide uses the platform `Intl.RelativeTimeFormat`. Older runtimes need a polyfill.

## ICU MessageFormat 1 styles

The ICU MessageFormat 1 plugin imports ICU arguments with their ICU names and styles. Paraglide formats them like ICU:

| ICU | Imported as | Formats as |
| --- | --- | --- |
| `{n, number}` | `{n: number}` | `number` |
| `{n, number, percent}` | `{n: number style=percent}` | `number style=percent` |
| `{n, number, integer}` | `{n: number style=integer}` | `number maximumFractionDigits=0` |
| `{d, date, short}` | `{d: date style=short}` | `datetime dateStyle=short` (also `medium`, `long`, `full`) |
| `{d, date}` | `{d: date}` | `datetime dateStyle=medium` |
| `{d, time, short}` | `{d: time style=short}` | `datetime timeStyle=short` (also `medium`, `long`, `full`) |
| `{d, time}` | `{d: time}` | `datetime timeStyle=medium` |
| `#` in `{n, plural, offset:1 …}` | `{n: icu:pound offset=1}` | `number` of `n - 1` |

`{n, number, integer}` rounds halves away from zero like `Intl.NumberFormat` (`2.5` renders "3"), while ICU rounds them to the nearest even integer (`2.5` renders "2").

A style without an `Intl` equivalent, like an ICU skeleton (`{n, number, ::currency/EUR}`, `{d, date, ::yyyyMMdd}`) or `currency` without a `currency` option, is ignored with a warning: the value is formatted without it, so `{d, date, ::yyyyMMdd}` formats like `{d, date}`.

## Formatting across locales

Formatting runs when a message function is called. On a page, messages use the current document's locale. By default, changing the user's locale starts a new document navigation, so formatting runs again as the new document renders. The rare browser-only `setLocale(locale, { reload: false })` escape hatch stays on the current document instead; it does not automatically re-run formatting calls, so the application must update its locale-dependent UI. See [the warning in Basics](https://paraglidejs.com/basics#advanced-stay-on-the-current-document).

To format a value for a locale other than the current document—for example, in a preview or server response—pass that locale to the message call:

```ts
import { m } from "./paraglide/messages.js";

m.personal_balance({ amount: 1000.57 }, { locale: "en" }); // "Your balance is 1,000.57."
m.personal_balance({ amount: 1000.57 }, { locale: "de" }); // "Ihr Kontostand ist 1.000,57."
```

For a locale picker, use `setLocale()` and let it navigate to the locale's document. Do not use `reload: false` for a preview: the per-message `locale` option above is designed for that. See [Basics](https://paraglidejs.com/basics#getting-and-setting-the-locale).

## Common mistakes

- Using `numberFormat`, `dateFormat`, or `relativeTimeFormat`:
  use `number`, `datetime`, or `relativetime`.
- Passing already formatted strings:
  pass raw values (`number` / `Date` / parseable date string) and let Paraglide format per locale.
- Expecting timezone-independent output without a timezone:
  set `timeZone=UTC` (or your target zone) in `datetime` options.
- Omitting `unit` for `relativetime`:
  provide a literal unit such as `unit=day` or a dynamic unit such as `unit=$unit`.
- Expecting `relativetime` to choose units automatically:
  compute the duration and unit in your app before calling the message.

## See also

- [Variants](https://paraglidejs.com/variants)
- [Translation File Formats](https://paraglidejs.com/file-formats)
- [Formatting example](https://github.com/opral/paraglide-js/tree/main/examples/formatting)
