Skip to content

    Frontend System Design

    Message Formatting, Plurals and the Intl APIs

    Turn one sentence into many languages: plural categories, select, ordinals, and the Intl APIs for numbers, dates, lists and relative time with fixed zones.

    By Published 12 min read

    • frontend-system-design
    • internationalization
    • intl
    • plurals
    • message-format
    Share

    Standard lessonintermediateTypeScript labFrontend System Design

    Before this: Internationalization, Localization and RTL

    What you'll learn: By the end of this lesson you will be able to explain why a translated sentence must be one message and not pieces joined in code, choose the right branch with plural categories (including languages with more than two forms), write plural, select and ordinal messages, format numbers, money, dates, lists and relative times with the Intl APIs, pin the time zone, and know which parts of the output depend on the runtime's locale data.

    Think of a form letter with blanks: "Dear ___, you have ___ items." A mail-merge program fills the blanks, and that works when every recipient reads English. Now the same letter goes to Russia. The word for "items" changes with the number in three ways, the date is written in a different order and the thousands separator is a different character. A clerk who can fill blanks but cannot change the words around them will print wrong letters with perfect confidence.

    A front end that ships to several languages needs a better letter: one whole sentence per locale, with the variables inside it and the language's own rules choosing the words. This lesson follows Internationalization, Localization and RTL, which chose the locale and declared direction. Here the locale is known, and the job is to build the text.

    The running example is a cart and order page: "Ana added 3 items", "arrives tomorrow", "ordered on 1 Dec", prices in two currencies. The lab's catalogs and amounts are made up for the lesson.

    The core idea

    1. A sentence is one unit. Translators receive whole messages with placeholders, so they can reorder words. Code that joins fragments takes that away.
    2. The language decides how many forms a noun has. A plural message names categories, and the locale's rules say which category a number belongs to.
    3. The runtime already knows the formats. Intl.NumberFormat, DateTimeFormat, ListFormat and RelativeTimeFormat hold the locale data, so you do not ship it.
    4. Some inputs come from the environment. The time zone and the locale data version change the output, so pin or record them.

    Three illustrations run through the lesson: a minimal example (a count that has to agree with a noun), a realistic example (the cart line with a name, a count and a price) and a failure example (a date that shows the wrong day).

    Why concatenation fails

    The ICU MessageFormat guide describes the practice that works: translate whole messages with placeholders, so translators can rearrange the pieces, and avoid UI text built from fragments and formatted values. The code below looks harmless in English:

    const text = `${n} item${n === 1 ? "" : "s"}`;

    It encodes three assumptions: the noun follows the number, there are two forms, and the form is chosen by n === 1. Each one is wrong for some language. The lab prints one message per language next to the same shortcut applied to Russian.

    Figure 1. From a locale and a number to a sentence

    Message formatting: a message pattern, values and a locale go into a formatter that uses plural rules and number formatting to produce textMessage to Formatter (parsed once); Values to Formatter (per render); Formatter to Intl (locale, number); Intl to Formatter (category, digits); Formatter to Text (string)Message: pattern per localeMessagepattern perlocaleValues: name, count, priceValuesname, count,priceFormatter: walks the patternFormatterwalks the patternIntl: plural, number, dateIntlplural, number,dateText: for the localeTextfor the localeparsed onceper renderlocale, numbercategory, digitsstring

    How to read this: the pattern is data that translators own. The formatter does not know any language: it asks Intl which plural category a number falls in and how to write it, then picks a branch.

    Plural categories

    Intl.PluralRules returns a tag for a number: zero, one, two, few, many or other. MDN says the tag depends on the language, and shows that English has two cardinal forms (one and other) while Arabic has six. CLDR, which supplies the rules, calls the names mnemonics: "one" is a category for any number that behaves like 1 in that language, and other is required because it is also the form used when a language has a single form. The tag only selects a form. As MDN puts it, creating the right sentence for each form depends on the language, and even in English it is not always as simple as adding "s".

    The lab prints the categories for sample numbers. Some results surprise people who learned plurals from English.

    Expected output, captured from a real run (Node 26.13.1, 2026-12-01; the Node run reported ICU 78.3 and CLDR 48.0; scenarios 1 and 2):

    === 1. concatenation with an English rule, against a plural message per language
    n=0  en: 0 items | de: 0 Artikel | ru: 0 товаров | ru with the English rule: 0 товаров
    n=1  en: 1 item | de: 1 Artikel | ru: 1 товар | ru with the English rule: 1 товар
    n=2  en: 2 items | de: 2 Artikel | ru: 2 товара | ru with the English rule: 2 товаров
    n=5  en: 5 items | de: 5 Artikel | ru: 5 товаров | ru with the English rule: 5 товаров
    n=21 en: 21 items | de: 21 Artikel | ru: 21 товар | ru with the English rule: 21 товаров
    n=22 en: 22 items | de: 22 Artikel | ru: 22 товара | ru with the English rule: 22 товаров
    
    === 2. cardinal plural categories by locale (Intl.PluralRules)
    numbers:  0  1  2  3  5  11  21  100  1.5
    en  one,other                      other one other other other other other other other
    fr  one,many,other                 one one other other other other other other one
    ru  one,few,many,other             many one few few many many one many other
    ar  zero,one,two,few,many,other    zero one two few few many many other other
    ja  other                          other other other other other other other other other

    Read the Russian row: 21 is one, 22 is few, 11 is many, and 1.5 is other. The English shortcut gets 0, 1 and 5 right by coincidence and fails on 2, 21 and 22. In the French row, 0 is one, which differs from English. The Japanese row has one category, so a Japanese message needs only other. Which numbers fall in which category is locale data that the runtime ships, and these outputs belong to the version named above.

    Writing messages

    The lab implements a small subset of the ICU syntax. A plural argument picks a branch by the locale's plural rules; a select argument picks one by keyword, such as a gender value; # inside a plural branch is the number, formatted for the locale; =0 is an exact match that takes precedence over a category. Every plural needs an other branch, so the lab refuses a message without one when it is created, not at the moment a user hits the missing case.

    {who, select, she {She} he {He} other {They}} added
    {count, plural, =0 {nothing} one {# item} other {# items}} to the cart

    The ICU guide advises making select the outermost structure with plural inside it, and writing a full sentence in each branch. Ordinals have their own rules: MDN lists the English ordinal categories as one ("st"), two ("nd"), few ("rd") and other ("th"), and the lab's selectordinal produces 1st, 2nd, 3rd, 4th, 11th and 21st through them. The catalog stores one pattern per locale, and a translator adds the categories their language needs: the lab's German message has one and other, its Russian one has four.

    Minimal example: a count that agrees with its noun

    A ru message with one, few, many and other branches turns 1, 2, 5, 21 and 22 into "1 товар", "2 товара", "5 товаров", "21 товар" and "22 товара". No code in the component knows Russian: it passes n.

    Numbers, dates, lists and relative time

    The Intl namespace has a constructor per job, and each takes a locale list and options. If the locale argument is omitted, the implementation's default is used; a locale that is well formed but not recognized is ignored in favour of the next one. This is the same list behaviour as in the previous lesson.

    Intl.NumberFormat changes grouping, decimal separator, digits and currency placement. MDN's examples show de-DE as 123.456,789, en-IN with lakh grouping as 1,23,456.789, and a euro amount in de-DE as 123.456,79 €. The spaces in such results are not always ordinary spaces, so the lab prints no-break spaces as [NBSP] and narrow ones as [NNBSP]; a test that compares strings must account for them.

    Expected output, captured from a real run (Node 26.13.1, 2026-12-01; scenario 4):

    === 4. numbers and money by locale (invisible spaces shown as markers)
    en-US  number=1,234,567.891  EUR=€1,234.50  percent=26%
    de-DE  number=1.234.567,891  EUR=1.234,50[NBSP]€  percent=26[NBSP]%
    fr-FR  number=1[NNBSP]234[NNBSP]567,891  EUR=1[NNBSP]234,50[NBSP]€  percent=26[NBSP]%
    en-IN  number=12,34,567.891  EUR=€1,234.50  percent=26%
    en-US USD=$1,234.50

    Intl.DateTimeFormat orders and names the parts of a date, with presets for the date (dateStyle of full, long, medium or short). It also decides which calendar day it is, and that depends on the time zone. When the option is omitted, MDN says, the user's default time zone is used. MDN also warns that its own example results assume UTC and "may vary", and that output without arguments depends on the implementation, the default locale and the default time zone.

    Intl.ListFormat joins items with the locale's conjunction or disjunction (type of conjunction, disjunction or unit). MDN's examples show en writing an Oxford comma and en-GB omitting it, and de using "oder" for "or". Intl.RelativeTimeFormat writes "in 3 days" or "1 day ago", and with numeric: "auto" uses phrases such as "yesterday" where the language has them. In the lab's scenario 7, auto gives "yesterday, tomorrow, in 3 days" for en and "gestern, morgen, in 3 Tagen" for de.

    Realistic example: the cart line

    The cart shows "Ana added 3 items", the line total in the customer's currency, and "arrives tomorrow". The message holds the sentence, Intl.NumberFormat writes the price, and RelativeTimeFormat writes the day. Because the calendar day depends on a zone, the zone is an input, not something the formatter guesses.

    Expected output, captured from a real run (Node 26.13.1, 2026-12-01; scenario 6, with scenario 7 below it):

    === 6. the same instant in three time zones (en-GB): the calendar date changes
    UTC                  1 Dec 2026 15:30
    Asia/Tokyo           2 Dec 2026 00:30
    America/Los_Angeles  1 Dec 2026 07:30
    
    === 7. lists and relative time
    en     Ana, Ben, and Cy  |  Ana, Ben, or Cy
    en-GB  Ana, Ben and Cy  |  Ana, Ben or Cy
    de     Ana, Ben und Cy  |  Ana, Ben oder Cy

    Failure example: the order that was placed "tomorrow"

    One instant, 15:30 UTC on 1 December, is already 2 December in Tokyo. A server that formats the order date in its own zone (often UTC) and a browser that formats it in the visitor's zone show different days for the same order. If the server renders the page, the two strings also differ at hydration, which the hydration mismatches lesson explains. Pin the zone on both sides, or render the date only where the zone is known.

    Figure 2. The same instant, formatted with the server's zone and the visitor's zone

    sequenceDiagram accTitle: A date that changes day between server and browser accDescr: The server renders an order placed at 15:30 UTC on 1 December using its own time zone and sends the text "1 Dec 2026". The browser of a visitor in Tokyo formats the same instant in its own zone and gets "2 Dec 2026". The two strings differ, so the page flickers or reports a mismatch. participant S as Server (UTC) participant B as Browser (Tokyo) S->>B: HTML with "1 Dec 2026" B->>B: format the same instant in its own zone Note over B: "2 Dec 2026" is not "1 Dec 2026"

    How to read this: nothing is wrong with either formatter. They are given different zones. The fix is to make the zone an explicit input to both.

    Figure 3. Which tool produces which part of the text

    flowchart TD accTitle: Choosing how to produce text accDescr: Start from what the text depends on. A count that changes the words goes to a plural message. A fixed keyword such as a gender goes to select. A bare number or money goes to NumberFormat. A date or time goes to DateTimeFormat with an explicit time zone. A list of items goes to ListFormat. A distance from now goes to RelativeTimeFormat. A{What does the text depend on?} -->|a count changes the words| B[plural message] A -->|a fixed keyword| C[select message] A -->|a number or money| D[NumberFormat] A -->|a date or time| E[DateTimeFormat with a time zone] A -->|a list of items| F[ListFormat] A -->|distance from now| G[RelativeTimeFormat]

    How to read this: the question is what varies. Words that change with a count are a message; digits and symbols are an Intl formatter. A date always needs a zone.

    Where the output comes from

    All of these APIs read locale data inside the runtime, so a different Node or browser version can print a different string for the same call. That is why the lab pins every time zone, prints invisible spaces as markers, restricts itself to common locales and records the Node version, and why tests for Intl text should compare parts or a version-pinned expectation. formatToParts() returns the pieces (MDN's ListFormat example returns element and literal parts) for the cases where you need to style one part.

    A message formatted a thousand times should not build a thousand formatters. The lab's FormatterCache builds one per kind, locale and options: for 1000 calls of one Russian message it built 2 formatters, against 2000 with a new cache per call. That is a count from the lab's model and says nothing about time.

    Trade-offs

    Choice Gain Cost
    Whole messages with plural and select Correct grammar, reorderable by translators A syntax to learn and a parser to carry
    Concatenate in code Simple for one language Wrong where plural rules or word order differ
    Intl built-ins No locale data shipped Output varies with runtime version
    Ship your own locale data Same output everywhere Bundle size and a data update process

    Build it: plurals, formats and time zones

    Goal. Run a small message formatter and the Intl APIs through eight scenarios and compare them with the rules above.

    Setup. The lab is in the course's example workspace (Node 26.13.1, pnpm). It is pure logic: no DOM, no browser.

    cd examples/frontend
    pnpm install
    cd labs/intl-message-formatting
    pnpm test           # 16 tests
    node src/demo.ts

    Steps.

    1. Run the tests, then the demo, and compare it with the outputs above.
    2. In scenario 2, find the French number 0 and the Russian number 21.
    3. In scenario 8 (see the golden file), read the three errors: a plural with no other, an unterminated message and a currency with no code.

    Scenario 3 prints ordinals and the nested select: "She added nothing to the cart", "He added 1 item to the cart" and "They added 1,234 items to the cart". Scenario 5 formats one instant in five locales with the zone pinned to UTC. The complete output is in examples/frontend/golden/intl-message-formatting.txt.

    What the lab does not show. Only a subset of ICU syntax; no apostrophe quoting, offsets or skeletons; no browser, no other Node or ICU version, no translation workflow, and no timing. The Russian and German strings are fixtures, not reviewed translations.

    Break it. In test/message.test.ts, the Russian test expects "21 товар". Change the one branch of the RU constant to # товара and the test fails on 1 and 21. Change it back.

    In practice: an order page whose date and plural were wrong

    NOTE

    This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.

    The situation

    A storefront has launched in Russian. Support reports "2 товаров" on the cart, and customers in Tokyo say the order confirmation shows the day before the day they ordered.

    Starting architecture

    Figure 4. Starting point: English plural logic in code, and a date formatted on the server

    Starting architecture: components join text with an English plural rule, and the server formats dates in its own zoneComponents to Catalog (word lookups); Components to Customer (wrong plural); Server to Customer (wrong day)Components: join stringsComponentsjoin stringsServer: formats in UTCServerformats in UTCCatalog: single wordsCatalogsingle wordsCustomer: Tokyo, RussianCustomerTokyo, Russianword lookupswrong pluralwrong day

    How to read this: the two red dashed edges are the defects: text built from single words, and a date whose zone belongs to the server.

    Options considered

    Option What it fixes What it costs
    Add a Russian special case in code The cart line Every new language needs code
    Whole messages with plural categories All plural text, in any language Migrate every joined string
    Pass the customer's zone and format with it The date A zone has to be known and stored

    The decision

    Field Decision record
    Context Joined strings and a server-zone date are wrong outside English and UTC.
    Decision Every count-dependent sentence becomes a plural message per locale, translators supply the categories each language needs, and a lint rule rejects template strings that add "s". Dates are formatted with an explicit time zone: the customer's if known, else the store's, and the zone is shown on confirmations. Formatters are cached.
    Consequences, good Correct forms in every language, and the same day on server and browser.
    Consequences, bad Messages need review by a speaker; the syntax needs documentation and tests.
    Revisit when A library with a newer message syntax is worth adopting.

    Rollout and verification

    1. Migrate the highest-traffic messages first, with a test per category for each locale (for Russian, 1, 2, 5, 21, 22 and 1.5).
    2. Add a date test that sets the zone, using an instant near midnight.

    The 2 a.m. failure

    A report arrives that the order page shows a different date after a deployment (illustrative). Nothing in the catalog changed. The container image moved to a new Node release whose locale data formats the medium date differently for one locale, and an exact-string test on the page was never run in that image.

    First three checks: (1) compare process.versions for ICU and CLDR in the old and new images; (2) format the same instant with the same locale and zone in both and compare the parts; (3) check whether the test asserts a string that belongs to a data version. Mitigation: relax the assertion to parts or a pinned zone and locale. Follow-up: log the runtime's locale data version beside the build id.

    Architect's notes

    • Store whole sentences. A translator should see the whole message.
    • Test every category for every locale you ship. The numbers that fall in few and many are not obvious.
    • Make the time zone an argument. Never rely on the runtime's default for content that goes into a page.

    Common mistakes

    • n === 1 ? "" : "s". It is the English rule, not the rule.
    • A plural message with no other branch. Every language needs one, and the lab refuses it.
    • Formatting a date without a zone on the server, then again on the client.
    • Assuming Intl output is identical in every runtime. MDN warns that results may vary.

    Key takeaways

    • A translated sentence is one message with placeholders and branches.
    • Plural categories are zero, one, two, few, many and other; their numbers are language data, and other always exists.
    • Use select for fixed keywords and selectordinal for ordinals; put full sentences in branches.
    • Intl formats numbers, money, dates, lists and relative time from the runtime's locale data.
    • The calendar day depends on the time zone: pass it explicitly.
    • Output depends on the runtime's data version: pin locales, zones and the version when you test.

    Practice exercises

    Exercise 1 — Starter (45 min): Add an Arabic plural message to the lab and a test with one number for each of its six categories.

    Exercise 2 — Intermediate (2 hours): Support {n, plural, offset:1 ...} and test it with a "you and N others" message.

    Exercise 3 — Advanced (half day): Write a lint rule that finds template strings of the form ${n} word${n === 1 ? "" : "s"} and reports them with the replacement message.

    In this course:

    • Hydration Mismatches, why a server and a browser must produce the same text.
    • Error, Loading and Empty States, message text that changes with a count.
    • The previous lesson, Internationalization, Localization and RTL, covers locale negotiation, lang, dir and logical CSS.

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments