npm.io
0.0.51 • Published 1 week ago

be-intl

Licence
MIT
Version
0.0.51
Deps
4
Size
17 kB
Vulns
0
Weekly
0

be-intl ()

Format numbers, dates automatically and semantically.

Playwright Tests How big is this package in your project? NPM version

<data value=123456.789 lang="de-DE" be-intl='{ "style": "currency", "currency": "EUR" }'></data>

emits

<data value=123456.789 lang="de-DE" be-intl='{ "style": "currency", "currency": "EUR" }'>123.456,79 €</data>

The output element provides identical support.

<time lang="ar-EG" datetime=2011-11-18T14:54:39.929Z be-intl='{ "weekday": "long", "year": "numeric", "month": "long", "day": "numeric" }'></time>

emits

<time lang="ar-EG" datetime="2011-11-18T14:54:39.929Z" be-intl="{ &quot;weekday&quot;: &quot;long&quot;, &quot;year&quot;: &quot;numeric&quot;, &quot;month&quot;: &quot;long&quot;, &quot;day&quot;: &quot;numeric&quot; }">الجمعة، ١٨ نوفمبر ٢٠١١</time>

We can also employ more semantic syntax:

<data value=123456.789 lang="de-DE" be-intl-style=currency be-intl-currency=EUR></data>

Locale resolution

The examples above put lang right on the formatted element, but that isn't required. be-intl uses the element's effective language, resolved the same way the browser's :lang() selector works:

  1. the nearest ancestor with a lang (or xml:lang) attribute — crossing shadow-root boundaries via the host element;
  2. otherwise <html lang>;
  3. otherwise the browser's own navigator.language.

So in practice you set lang once, high up:

<html lang="de-DE">
  ...
  <data value=123456.789 be-intl-style=currency be-intl-currency=EUR></data>
  <!-- emits 123.456,79 € -->

Changing the formatted element's own lang after it has been enhanced re-formats it only if you opt in with be-intl-observe-lang (🌐-observe-lang); changes to an ancestor's lang after enhancement are not tracked.

Announcing updates to assistive technology

be-intl writes the formatted string into textContent on first render and again whenever the bound value or the effective locale changes. By default it leaves ARIA untouched — most formatted <data> / <time> elements are static readouts, and turning every one into a live region (especially for a locale switch that only changes presentation, not the value) is usually just screen-reader noise.

When a particular value is something the user is watching change, opt that element in with be-intl-announce (🌐-announce):

<output be-intl be-intl-announce></output>
<data be-intl be-intl-announce value="0" id="unread"></data>

With the attribute set, be-intlafter the element's first render, so the initial value isn't spoken on load — marks it as a polite live region:

  • aria-live="polite" (skipped for <output>, which is already an implicit polite live region), and
  • aria-atomic="true", so multi-token output such as a formatted date is announced as one phrase rather than word-by-word.

Later re-formats then mutate an already-registered region and are announced. assertive is intentionally not offered — interrupting the user to read out a reformatted number is almost never the right call.

Alternative names

The semantic example above involves a lot of keyboard tapping of the letters "be-intl". To avoid blisters on your itty bitty fingers, we provide an alternative base attribute you can use:

<time lang="ar-EG" datetime="2011-11-18T14:54:39.929Z"
🌐-weekday=long 🌐-year=numeric 🌐-month=long 🌐-day=numeric></time>

Viewing Demos Locally

  1. Install git
  2. Fork/clone this repo
  3. Install node.js
  4. Open command window to folder where you cloned this repo
  5. git submodule add https://github.com/bahrus/types.git types

  6. git submodule update --init --recursive

  7. npm install

  8. npm run build

  9. npm run serve

  10. Open http://localhost:8000/demo/ in a modern browser

Running Tests

> npm run test

Keywords