@dnvgl/i18n is a set of functions to support multiple languages/cultures in a browser or Node.js. There is no dependency on other libraries (exception: TypeScript when using definitions). It's built on the ECMAScript native solutions like for example Intl API.
The purpose of this library is to provide all needed functionalities to support multiple languages/cultures in the web application. All functions have been designed to be simple and easy to use, that is why some native API are wrapped. This library was in use two years (commercial use by DNV company) before its open-source release (2021-04-13).
The biggest advantage about the library is that it supports any language, using the culture of the browser.
- Chrome (+ mobile): last 2 versions
- Firefox (+ mobile): last 2 versions
- Safari (+ mobile): v14* or newer
- Edge: last 2 versions
There is a build-in function to check it: isBrowserCompatible().
* - formatCountry() and formatCurrency() require Safari 14.1 (desktop) or 14.5 (mobile)
- v14 or newer
Using npm:
npm install @dnvgl/i18nUsing yarn:
yarn add @dnvgl/i18nImport functions or types from @dnvgl/i18n, example:
import { sort, formatNumber } from '@dnvgl/i18n';
const sortedItems = sort(["Z", "a", "A", "z", "ś"], "asc", "pl-PL"); // returns ["a", "A", "ś", "z", "Z"]
const numberFormattedInSpecificCulture = formatNumber(12486.4529, { maxPrecision: 2 }, "de-DE"); // returns "12.486,45"
const numberFormattedInLocalCulture = formatNumber(12486.4529); // returns "12 486,4529" using local browser culture (in this case 'pl-PL'); any culture can be supported out of the boxThe library ships as spec-compliant, pure ESM: relative imports use explicit .js specifiers, so it resolves natively in the browser (<script type="module">), Node.js, and every major bundler (Vite, webpack, Rollup, esbuild) — no extra configuration required. Tree shaking works out of the box ("sideEffects": false), so only the functions you actually import end up in your bundle.
The library is fully isomorphic and side-effect free, so it is safe to use in server-side rendered apps (Next.js, Remix, SvelteKit, Nuxt, Astro, etc.). It relies solely on the standard Intl API and ships no browser-only globals (window, navigator), so the exact same code runs on the server and the client without guards.
Library provides a built-in ts definition.
See the CONTRIBUTING.md file.
See full details in the DOCUMENTATION.md file.
formatInteger(), formatNumber(), formatNumberToFixed(), parseNumber(), transformToInputNumericString(), roundUsingHalfAwayFromZero(), getDecimalSeparator(), getMinusSign(), getThousandsSeparator(), createNumberFormat()
formatDate(), formatDatePart(), formatDateToISO(), formatRelativeTime(), formatMonth(), formatTime(), getDateFnsFormat(), getDayJsFormat(), getMomentFormat(), getWeekendDays()
compareBooleans(), compareDates(), compareNumbers(), compareNumbersAlike(), compareStrings(), compareStringsFactory(), sort(), sortBy(), sortByInplace(), sortInplace()
capitalizeFirstLetter(), formatString(), lowercaseFirstLetter(), normalizeForSearching(), plural()
findIso3166Country(), formatCountry(), getIso3166Countries(), getStatesOfUsa(), isEuropeanUnionMember(), isValidIso3166Code()
findIso4217Currency(), findIso4217CurrencyForIso3166Country(), formatCurrency(), getIso4217Currencies(), getCurrencySymbol(), isValidIso4217Code()
formatMoney(), formatIban(), getCountryCodeFromBic(), getCountryCodeFromIban(), roundUsingBankersMethod()
getSystemLocaleName(), isBrowserCompatible()
See the CHANGELOG.md file.