Tools
Tools and utilities for Intl-T
Intl-t provides a set of tools to help you with your translations. You can use each of them independently from intl-t/utils or @intl-t/utils.
import "intl-t/utils";Inject
Inject variables into content, with built-in support for the ICU message format.
import { inject } from "intl-t/utils";
const str = inject("Hello, {user}!", { user: "Ivan" }); // "Hello, Ivan!"
// TypeScript Support
typeof str; // `Hello, ${string}`
// Full support for ICU message format
// Extended keeping syntax and performance
inject("One plus one equals {(1+1), =2 {two (#)} other {# (#)}}"); // "One plus one equals two (2)"
inject("{a} plus {b} {(a+b), (typeof # != 'number') {is not a number. #} <0 {is negative. #} other {equals {(a+b)}. {a}+{b}=#}}");
// nested injectionsSecurity: message templates are trusted code
The extended syntax above ({(a+b)}, comparison guards, nested conditions) is evaluated. Treat
message templates as code, not data — the same trust you give the source files that import them.
Do not feed attacker-controlled strings (CMS entries, user-submitted or third-party translation
files) in as templates; a crafted template can run arbitrary expressions.
Interpolated values (inject(template, values)) are data and safe to take from users. They are
inserted literally: braces, #, quotes or markup inside a value are never parsed as placeholders,
options, expressions or React tags. To write one of those characters literally in a template,
escape it with a backtick (`{, `#, `<).
Expression evaluation can be turned off where it isn't needed (for example on a server rendering
untrusted content). Set the INTL_T_DISABLED_EVAL environment variable, or pass enabledEval: false
in the format options. It is also disabled automatically under runtimes that forbid code generation
from strings (strict CSP, some edge runtimes), where the tree degrades gracefully to plain lookups.
Variable injection is also hardened against denial-of-service via malformed templates: the parser
runs in linear time and caps any single {…} fragment at MAX_FRAGMENT_LENGTH characters.
React Injection
To use the React Chunk Injection function, import it from intl-t/react.
import { injectReactChunk } from "intl-t/react";Match
Function to match the best locale from the available ones.
import { match } from "intl-t/utils";
const availableLocales = navigator.languages.split(","); // ["es", "en"]; // can be string | string[]
const allowedLocales = ["en-US", "es-MX", "fr-FR", "zh-Hant"];
const defaultLocale = "en-US";
const locale = match(availableLocales, allowedLocales, defaultLocale); // "es-MX"It finds the best locale by comparing the available locales with the allowed locales. Try it yourself.
Negotiator
Simple function to extract the locale from HTTP headers.
import { negotiator } from "intl-t/utils";
negotiator({ headers });Formatters
Formatters are used internally by the inject function, but they can also be used directly.
import { format } from "intl-t/utils";// format params
format.list(value: string[], options?: Intl.ListFormatOptions);
format.number(value: number = 0, options?: Intl.NumberFormatOptions);
format.currency(value: number = 0, options: Intl.NumberFormatOptions = {});
format.date(value: Date = new Date(), options?: Intl.DateTimeFormatOptions);
format.relative(value: Date | number = 0, options: Intl.RelativeTimeFormatOptions & Record<string, any> = {}); // relative time inferred from value
format.time(value: Date = new Date(), options?: Intl.DateTimeFormatOptions);
format.price(value: number = 0, options: Intl.NumberFormatOptions = {}); // uses USD by defaultResolvers
Resolver functions are best used via createNavigation, but you can also import them directly from intl-t/utils without bound values and types.