Starlight localization API proposal #1496
Replies: 3 comments 10 replies
-
I am very much in favor of this proposal. I think it's a great idea which really addresses a real need. To share a few thoughts:
Show original
|
Beta Was this translation helpful? Give feedback.
-
Sharing some really quick thoughts
|
Beta Was this translation helpful? Give feedback.
-
Since Starlight See the “Using UI translations” guide to learn more about how to access built-in UI labels or your own custom strings in your project. Plugin authors can also use the new |
Beta Was this translation helpful? Give feedback.
-
This is an initial proposal sketching out a problem space and goals, but not yet landing on a definite solution. Would love feedback on the concept, whether it’s valuable, what would be important to consider, concerns, etc.
Summary
Provide a full localization API out-of-the-box with Starlight, exposing the same tools used to localize Starlight’s built-in UI to developers building pages and components for their own sites.
Background & Motivation
Starlight comes with internationalization (i18n) support for routing and page layout and includes UI translations for a growing number of languages (with new contributions always welcome!)
Internally, Starlight’s UI uses a simple key–value dictionary lookup for the current locale and falls back to a site’s default locale (or, in rare cases, to English if no value is defined for both the current and default locales).
The dictionary looks something like this (abbreviated):
Our internal API consists of a
useTranslations()
factory, which returns at()
function:It would be useful to users to have access to something like
useTranslations()
for their own components, especially now that extending Starlight’s default i18n dictionary is supported.However, exposing our current helper function comes with some issues:
Starlight’s localization system is intentionally very basic and does not support common l10n requirements like variable interpolation, pluralization, and gendered variants. Building these advanced features ourselves seems unwise, but not including them seems like an invitation for feature requests.
We currently require passing in the
locale
but do not expose a helper for quickly getting the current locale, which is not the most user friendly API. Two imports and three function calls would be needed to get a string:We skirted around this clunkiness by exposing a
labels
object in overridden component props that already has the localized result oft()
for all known keys, but this is static and not available in other components.Goals
Example
In order to access localized strings, users need access to some kind of function that returns these. At its most basic this could look like the not very friendly examples above. Other options could be more powerful, but also more restrictive in where they work. Some examples:
An imported helper:
This theoretically works just about anywhere, including on the client, although the client bundle would probably not be optimized at all if this were used in UI components. However, users need to detect the current locale somehow in order to get a
t()
function, and outside of.astro
files, it is unclear how they would do that.A pre-initialized function attached to
Astro.locals
by a middleware:This would allow Starlight to set the current locale for you, but restricts usage to
.astro
components (and API routes, although those are likely rarely used by Starlight projects).If aiming for simplicity, a pre-initialized function may be preferable, even if
Astro.locals
is still not a broadly familiar API.If we are to support more advanced localization needs than simple static string look-up, we should probably defer to a third-party library. Would love to hear from anyone if they have an opinion about i18n tooling, but i18next appears to be the best bet. If we wanted tree-shakeable code for client-side components, Paraglide is interesting, but may be less flexible if we are happy with our existing content collection method of storing/loading user dictionaries, and does not support all common l10n needs yet, including pluralization.
Beta Was this translation helpful? Give feedback.
All reactions