# Languages and formats

An app in more than one language: its own strings in each (`words`), said
with `t("key", {…})` and checked by the compiler against the keys; numbers,
money, shares, lists and counts in the language's own way (`fmt`); and the
reader's language (`screen.lang`). Dates and times are `time.*`
([data.md](data.md)). Back to the [map](README.md).

```mote
// docs: whole
app "Shop"

words "en"
  hello = "Hello, {name}!"
  cart.items = {zero: "Your cart is empty", one: "{n} item in your cart", other: "{n} items in your cart"}
  total = "Total: {sum}"

words "de"
  hello = "Hallo, {name}!"
  cart.items = {zero: "Dein Warenkorb ist leer", one: "{n} Artikel im Warenkorb", other: "{n} Artikel im Warenkorb"}
  total = "Summe: {sum}"

state count = 0

column gap=8 padding=16
  text t("hello", {name: "Ada"})
  text t("cart.items", {n: count})
  text t("total", {sum: fmt.currency(1234.5, "EUR")})
  row gap=8
    button "Add" press={count += 1}
    button "Deutsch" press={words.choose("de")}
    button "English" press={words.choose("en")}
  text `Shown in ${words.lang}; you read ${screen.lang}`
```

## Mental model

- **The first `words` is the language the app is written in.** Every key
  is said there; a later language says the same keys in its own words.
  A key it does not say yet is shown in the first's words, and `mote
  check` warns of it (`H251`), so a translation can ship half done.
- **`t("key", {…})` is checked when it is compiled**: the key is written
  out and is one the first language has (`H248`, with the nearest key
  offered), and the values are exactly the `{name}`s of its text, no more
  and no fewer.
- **One language is shown at a time**, the same for the whole page:
  `words.lang`. As the page opens it is the one the reader chose before
  (`words.choose`, kept in this browser), else the first of the browser's
  languages the app has (a browser that asks for `de-AT` gets `de`), else
  the first. Choosing another redraws every `t(…)` at once.
- **The page says its language**: the root element's `lang` (`<html
  lang="de">`) follows `words.lang`, for screen readers, a browser's
  translator, hyphenation and spell-checking, unless `root lang=` says
  another ([markup.md](markup.md#values-of-the-whole-page) has the
  order, and `app "X" lang=` for a page with no words).
- **`fmt` and `time` follow it.** Where they are not told a language,
  they write in the one the words are shown in (in an app with no `words`:
  the reader's own; on the back end: English).

## The forms

```text
words "en"                             the app's strings in a language (a language tag: "de", "pt-BR", "zh-Hant")
  hello = "Hello, {name}!"             a key (names with `.` between them) and its text; `{name}` is a value it is given
  cart.items = {one: "{n} item", other: "{n} items"}   forms for a count: `n`
words "de" from "./de.words"           the same lines, in a file beside this one

t("hello", {name: who})                the words of a key, in the language shown
t("cart.items", {n: count})            the form for that count, `{n}` written as the language writes numbers
words.lang                             the language shown now, reactive ("de")
words.langs                            those the app has, the first first (["en", "de"])
words.choose("de")                     show another from now on, kept for the next visit (the nearest the app has)
screen.lang                            the reader's language, as the browser says it ("de-AT"), reactive
screen.langs                           all the reader's languages, the most wanted first
```

**Counts.** A key whose text is a record has a form for each kind of count
the language has, by the browser's own rules (`Intl.PluralRules`): `one`,
`two`, `few`, `many`, `other` (English has `one` and `other`; Polish has
`one`, `few`, `many`; Arabic all six). `other` is needed. `zero`, where it is
given, is for none at all in any language ("Your cart is empty"). The
count is the value `n`, and `{n}` in a form is it written in the
language's way (`1.234` in German).

**A value** is any text or number. A number is written in the language's
way; for money or a share, write it with `fmt` first, as `total` above.

**A file of words** (`words "de" from "./de.words"`) holds the lines that
would stand under the `words`, unindented, with `//` comments, for a
translator to work on alone. A mistake in it is said at its own line.

### fmt

```text
fmt.number(1234567.891, {digits: 2})          "1,234,567.89"     "1.234.567,89" in German
fmt.number(1234567, {compact: true})          "1.2M"             "1,2 Mio."
fmt.currency(1234.5, "EUR")                   "€1,234.50"        "1.234,50 €"
fmt.percent(0.256, {digits: 1})               "25.6%"            "25,6 %"
fmt.list(["Ada", "Grace", "Linus"])           "Ada, Grace, and Linus"    "Ada, Grace und Linus"
fmt.list(["tea", "coffee"], {type: "or"})     "tea or coffee"
fmt.plural(n, {one: "{n} file", other: "{n} files"})    the form for that count, as in `words`
```

Every one takes `{lang: "de"}` among its options, for a language other than
the one shown. `digits` is how many places after the point at most;
`exact` is always that many (`fmt.number(2, {exact: 2})` is "2.00");
`grouping: false` leaves out the thousands' marks. `fmt.currency` takes the
currency's three letters (`H252` for any other) and `sign: "code"` ("EUR
1,234.50") or `sign: "name"` ("1,234.50 euros"). `fmt.list` takes `type:
"and"` (unsaid), `"or"` or `"unit"` ("3 ft, 7 in"). Everything here is the
browser's (or Bun's) `Intl`: nothing is downloaded, and a language the
host does not know is written as the default.

Dates and times are not here: `time.date(at, {style: "long"})`, `time.clock`
and `time.words(at)` ("in 20 minutes", "yesterday") already take `lang`, and
follow `words.lang` where it is unsaid.

## Pitfalls

- **`t` takes the key written out**, not a value (`t(key)` is `H248`): that
  is what lets the compiler check it. To choose among keys, choose among
  calls: `ok ? t("saved") : t("failed")`.
- **Sentences, not pieces.** Give a whole sentence its own key with its
  values in it (`"{name} invited you to {team}"`); words joined together
  read wrong in a language whose order differs.
- **`t` is the page's.** A `server fn` has no reader and no language
  (`H248`): an email sent from the back end is written in the language the
  page passes in, with its words as values.
- **The words are inside the page**, every language: a few kilobytes for a
  few hundred strings. They are not loaded one language at a time.
- **Text in markup is not translated by itself**: `text "Save"` is
  English in every language. Write `text t("save")`.

## In the source

| File | What |
|---|---|
| `compiler/src/parse/words.rs` | `words "…"`, its lines, `from`, a `.words` file. |
| `compiler/src/words.rs` | Each language checked against the first; the table the page is given (`WDI`). |
| `compiler/src/types/words.rs` | `t(…)` checked against the keys; `fmt.…` and its options. |
| `runtime/page/words.mote` | Part `WD`: the language chosen, kept and followed, `<html lang>`, `t`; part `SCL`: `screen.lang`. |
| `runtime/shared/fmt.mote` | Parts `LNG` (the language the words are shown in, for `fmt` and `time`) and `FM` (`fmt.…`). |
| `tests/words/`, `tests/words.real.mjs` | The browser run: English, German from a file, a French that says only some, a reader who asks for `de-AT`. |
