Per-locale bundles
See original GitHub issueDiscussion for RFC 003: https://lingui.js.org/rfc/003_per_locale_bundles.html
Introduction from RFC
Per-locale bundles are another build time optimization to reduce size of internationalized JavaScript applications.
Consider this example - we have an internationalized app which loads translation from external file:
// src/Notifications.js
import * as React from "react"
import { setupI18n } from "@lingui/core"
import { I18nProvider } from "@lingui/react"
import { Trans, Plural, date } from "@lingui/macro"
const i18n = setupI18n()
i18n.load("cs", import("./locale/cs/messages.json"))
i18n.activate("cs")
const Notifications = ({ now, count }) => (
<I18nProvider i18n={i18n}>
<h1><Trans>Notifications</Trans></h1>
<p><Trans>Today is {date(now)}</Trans></p>
<p>
<Plural
value={count}
one={<>You have <strong>#</strong> unread notification</>}
other={<>You have <strong>#</strong> unread notifications</>}
/>
</p>
</I18nProvider>
)
If we generate the production bundle in Engish locale, it will roughly look like this - <Trans> components are removed and formatting components (<Plural> and date()) are replaced with runtime versions:
// build/Notifications.en.js
import * as React from "react"
import { Plural, date } from "@lingui/react"
const Notifications = ({ now, count }) => (
<div>
<h1>Notifications</h1>
<p>Today is {date(now)}</p>
<p>
<Plural
value={count}
one={<>You have <strong>#</strong> unread notification</>}
other={<>You have <strong>#</strong> unread notifications</>}
/>
</p>
</div>
)
So far the code looks very similar to the original one except the loading of message catalogs is removed completely.
Let’s take a look on other than source locale, for example Czech. The message catalog might look similar to this:
msgid "Notifications"
msgstr "Upozornění"
msgid "Today is {now, date}"
msgstr "Dnes je {now, date}"
msgid ""
"{count, plural, "
"one {You have <0>#</0> unread notification} "
"other {You have <0>#</0> unread notification}}"
msgstr ""
"{count, plural, "
"one {Máte <0>#</0> nepřečtenou zprávu} "
"few {Máte <0>#</0> nepřečtené zprávy} "
"other {Máte <0>#</0> nepřečtených zpráv}}"
If we generate the production bundle for Czech locale, it will look roughly like this - translations are applied at build time. Also, <Plural> has all locale specific plural rules:
// build/Notifications.cs.js
import * as React from "react"
import { Plural, date } from "@lingui/react"
const Notifications = ({ now, count }) => (
<div>
<h1>Upozornění</h1>
<p>Dnes je {date(now)}</p>
<p>
<Plural
value={count}
one={<>Máte <strong>#</strong> nepřečtenou zprávu</>}
few={<>Máte <strong>#</strong> nepřečtené zprávy</>}
other={<>Máte <strong>#</strong> nepřečtené zprávy</>}
/>
</p>
</div>
)
Per-locale bundles has zero footprint of internatinalization library - the code looks exactly the same have it would look like when no internationalization was used at all. The remaining runtime layer are utilities for formatting like plurals, dates and number formatting. There’s also no extra request to fetch locale files and no runtime parsing.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:13
- Comments:47 (42 by maintainers)
per-locale bundle
locale agnostic bundle
There’re two parts of extra data:
Now the question is what is worse.
Just a heads up, Module Federation will likely enable per-locale bundling capabilities. Youd have to use the lower-level API, but I think it should be able to do this