You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exposes the built-in localization system in the Starlight plugin `config:setup` hook.
6
+
7
+
⚠️ **BREAKING CHANGE:**
8
+
9
+
This addition changes how Starlight plugins add or update translation strings used in Starlight’s localization APIs.
10
+
Plugins previously using the [`injectTranslations()`](https://starlight.astro.build/reference/plugins/#injecttranslations) callback function from the plugin [`config:setup`](https://starlight.astro.build/reference/plugins/#configsetup) hook should now use the same function available in the [`i18n:setup`](https://starlight.astro.build/reference/plugins/#i18nsetup) hook.
Adds a new [`useTranslations()`](https://starlight.astro.build/reference/plugins/#usetranslations) callback function to the Starlight plugin `config:setup` hook to generate a utility function to access UI strings for a given language.
Deprecates the Starlight plugin `setup` hook in favor of the new `config:setup` hook which provides the same functionality.
6
+
7
+
⚠️ **BREAKING CHANGE:**
8
+
9
+
The Starlight plugin `setup` hook is now deprecated and will be removed in a future release. Please update your plugins to use the new `config:setup` hook instead.
Adds a new [`absolutePathToLang()`](https://starlight.astro.build/reference/plugins/#absolutepathtolang) callback function to the Starlight plugin `config:setup` to get the language for a given absolute file path.
In the context of a Starlight plugin, you can use the [`useTranslations()`](/reference/plugins/#usetranslations) helper to access this API for a specific language.
303
+
See the [plugins reference](/reference/plugins/) for more information.
304
+
302
305
### Rendering a UI string
303
306
304
307
Render UI strings using the `locals.t()` function.
function configSetup(options:HookParameters['config:setup']) {
59
+
options.useTranslations('en');
60
+
}
61
+
```
62
+
63
+
### `i18n:setup`
64
+
65
+
Plugin internationalization setup function called when Starlight is initialized.
66
+
The `i18n:setup` hook can be used to inject translation strings so a plugin can support different locales.
67
+
These translations will be available via [`useTranslations()`](#usetranslations) in the `config:setup` hook and in UI components via [`Astro.locals.t()`](/guides/i18n/#using-ui-translations).
68
+
69
+
The `i18n:setup` hook is called with the following options:
A callback function to add or update translation strings used in Starlight’s [localization APIs](/guides/i18n/#using-ui-translations).
76
+
77
+
In the following example, a plugin injects translations for a custom UI string named `myPlugin.doThing` for the `en` and `fr` locales:
78
+
79
+
```ts {6-13} /(injectTranslations)[^(]/
80
+
// plugin.ts
81
+
exportdefault {
82
+
name: 'plugin-with-translations',
83
+
hooks: {
84
+
'i18n:setup'({ injectTranslations }) {
85
+
injectTranslations({
86
+
en: {
87
+
'myPlugin.doThing': 'Do the thing',
88
+
},
89
+
fr: {
90
+
'myPlugin.doThing': 'Faire le truc',
91
+
},
92
+
});
93
+
},
94
+
},
95
+
};
96
+
```
97
+
98
+
To use the injected translations in your plugin UI, follow the [“Using UI translations” guide](/guides/i18n/#using-ui-translations).
99
+
If you need to use UI strings in the context of the [`config:setup`](#configsetup) hook of your plugin, you can use the [`useTranslations()`](#usetranslations) callback.
100
+
101
+
Types for a plugin’s injected translation strings are generated automatically in a user’s project, but are not yet available when working in your plugin’s codebase.
102
+
To type the `locals.t` object in the context of your plugin, declare the following global namespaces in a TypeScript declaration file:
Plugin setup function called when Starlight is initialized (during the [`astro:config:setup`](https://docs.astro.build/en/reference/integrations-reference/#astroconfigsetup) integration hook).
49
-
The `setup` hook can be used to update the Starlight configuration or add Astro integrations.
142
+
Plugin configuration setup function called when Starlight is initialized (during the [`astro:config:setup`](https://docs.astro.build/en/reference/integrations-reference/#astroconfigsetup) integration hook).
143
+
The `config:setup` hook can be used to update the Starlight configuration or add Astro integrations.
50
144
51
145
This hook is called with the following options:
52
146
@@ -73,7 +167,7 @@ In the following example, a new [`social`](/reference/configuration/#social) med
73
167
exportdefault {
74
168
name: 'add-twitter-plugin',
75
169
hooks: {
76
-
setup({ config, updateConfig }) {
170
+
'config:setup'({ config, updateConfig }) {
77
171
updateConfig({
78
172
social: {
79
173
...config.social,
@@ -100,7 +194,7 @@ import react from '@astrojs/react';
A callback function to add or update translation strings used in Starlight’s [localization APIs](/guides/i18n/#using-ui-translations).
262
+
**type:**`(lang: string) => I18nT`
171
263
172
-
In the following example, a plugin injects translations for a custom UI string named `myPlugin.doThing` for the `en` and `fr` locales:
264
+
Call `useTranslations()` with a BCP-47 language tag to generate a utility function that provides access to UI strings for that language.
265
+
`useTranslations()` returns an equivalent of the `Astro.locals.t()` API that is available in Astro components.
266
+
To learn more about the available APIs, see the [“Using UI translations”](/guides/i18n/#using-ui-translations) guide.
173
267
174
-
```ts {6-13} /(injectTranslations)[^(]/
268
+
```ts {6}
175
269
// plugin.ts
176
270
exportdefault {
177
-
name: 'plugin-with-translations',
271
+
name: 'plugin-use-translations',
178
272
hooks: {
179
-
setup({ injectTranslations }) {
180
-
injectTranslations({
181
-
en: {
182
-
'myPlugin.doThing': 'Do the thing',
183
-
},
184
-
fr: {
185
-
'myPlugin.doThing': 'Faire le truc',
186
-
},
187
-
});
273
+
'config:setup'({ useTranslations, logger }) {
274
+
const t =useTranslations('zh-CN');
275
+
logger.info(t('builtWithStarlight.label'));
188
276
},
189
277
},
190
278
};
191
279
```
192
280
193
-
To use the injected translations in your plugin UI, follow the [“Using UI translations” guide](/guides/i18n/#using-ui-translations).
281
+
The example above will log a message that includes a built-in UI string for the Simplified Chinese language:
194
282
195
-
Types for a plugin’s injected translation strings are generated automatically in a user’s project, but are not yet available when working in your plugin’s codebase.
196
-
To type the `locals.t` object in the context of your plugin, declare the following global namespaces in a TypeScript declaration file:
// Define the `locals.t` object in the context of a plugin.
203
-
interfaceLocalsextendsStarlightLocals {}
204
-
}
287
+
#### `absolutePathToLang`
205
288
206
-
declarenamespaceStarlightApp {
207
-
// Define the additional plugin translations in the `I18n` interface.
208
-
interfaceI18n {
209
-
'myPlugin.doThing':string;
210
-
}
211
-
}
212
-
```
289
+
**type:**`(path: string) => string`
213
290
214
-
You can also infer the types for the `StarlightApp.I18n` interface from a source file if you have an object containing your translations.
291
+
Call `absolutePathToLang()` with an absolute file path to get the language for that file.
215
292
216
-
For example, given the following source file:
293
+
This can be particularly useful when adding [remark or rehype plugins](https://docs.astro.build/en/guides/markdown-content/#markdown-plugins) to process Markdown or MDX files.
294
+
The [virtual file format](https://github.com/vfile/vfile) used by these plugins includes the [absolute path](https://github.com/vfile/vfile#filepath) of the file being processed, which can be used with `absolutePathToLang()` to determine the language of the file.
295
+
The returned language can be used with the [`useTranslations()`](#usetranslations) helper to get UI strings for that language.
217
296
218
-
```ts title="ui-strings.ts"
219
-
exportconst UIStrings = {
220
-
en: { 'myPlugin.doThing': 'Do the thing' },
221
-
fr: { 'myPlugin.doThing': 'Faire le truc' },
297
+
For example, given the following Starlight configuration:
298
+
299
+
```js
300
+
starlight({
301
+
title:'My Docs',
302
+
defaultLocale:'en',
303
+
locales: {
304
+
// English docs in `src/content/docs/en/`
305
+
en: { label:'English' },
306
+
// French docs in `src/content/docs/fr/`
307
+
fr: { label:'Français', lang:'fr' },
308
+
},
309
+
});
310
+
```
311
+
312
+
A plugin can determine the language of a file using its absolute path:
0 commit comments