Skip to content

Commit 9d88caf

Browse files
ArmandPhilippotHiDeoosarah11918yanthomasdev
authored
feat(astro:i18n): add docs for some missing public utilities (#12264)
Co-authored-by: HiDeoo <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Yan <[email protected]>
1 parent 4e5d7c5 commit 9d88caf

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

src/content/docs/en/reference/modules/astro-i18n.mdx

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ import {
4040
notFound,
4141
middleware,
4242
requestHasLocale,
43+
normalizeTheLocale,
44+
pathHasLocale,
45+
toCodes,
46+
toPaths
4347
} from 'astro:i18n';
4448
```
4549

@@ -337,3 +341,116 @@ export const onRequest = defineMiddleware(async (context, next) => {
337341
return new Response("Not found", { status: 404 });
338342
})
339343
```
344+
345+
### `normalizeTheLocale()`
346+
347+
<p>
348+
349+
**Type:** `(locale: string) => string`
350+
</p>
351+
352+
Replaces underscores (`_`) with hyphens (`-`) in the given locale before returning a lowercase version.
353+
354+
```astro title="src/pages/index.astro"
355+
---
356+
import { normalizeTheLocale } from "astro:i18n";
357+
358+
normalizeTheLocale("it_VT") // returns `it-vt`
359+
// Assuming the current locale is `"pt-PR"`:
360+
normalizeTheLocale(Astro.currentLocale) // returns `pt-pt`
361+
---
362+
```
363+
364+
### `pathHasLocale()`
365+
366+
<p>
367+
368+
**Type:** `(path: string) => boolean`<br />
369+
<Since v="4.6.0" />
370+
</p>
371+
372+
Checks whether the given path contains a configured locale.
373+
374+
This is useful to prevent errors before using an i18n utility that relies on a locale from a URL path.
375+
376+
```js title="astro.config.mjs"
377+
export default defineConfig({
378+
i18n: {
379+
locales: [
380+
{ codes: ["it-VT", "it"], path: "italiano" },
381+
"es"
382+
]
383+
}
384+
})
385+
```
386+
387+
```astro title="src/pages/index.astro"
388+
---
389+
import { pathHasLocale } from "astro:i18n";
390+
391+
pathHasLocale("italiano"); // returns `true`
392+
pathHasLocale("es"); // returns `true`
393+
pathHasLocale('/es/blog/'); // returns `true`
394+
pathHasLocale("it-VT"); // returns `false`
395+
---
396+
```
397+
398+
### `toCodes()`
399+
400+
<p>
401+
402+
**Type:** `(locales: Locales) => string[]`<br />
403+
<Since v="4.0.0" />
404+
</p>
405+
406+
Retrieves the configured locale codes for each locale defined in your configuration. When multiple codes are associated to a locale, only the first one will be added to the array.
407+
408+
```js title="astro.config.mjs"
409+
export default defineConfig({
410+
i18n: {
411+
locales: [
412+
{ codes: ["it-VT", "it"], path: "italiano" },
413+
"es"
414+
]
415+
}
416+
})
417+
```
418+
419+
```astro title="src/pages/index.astro"
420+
---
421+
import { i18n } from "astro:config/client";
422+
import { toCodes } from "astro:i18n";
423+
424+
toCodes(i18n!.locales); // ["it-VT", "es"]
425+
---
426+
```
427+
428+
### `toPaths()`
429+
430+
<p>
431+
432+
**Type:** `(locales: Locales) => string[]`<br />
433+
<Since v="4.0.0" />
434+
</p>
435+
436+
Retrieves the configured locale paths for each locale defined in your configuration.
437+
438+
```js title="astro.config.mjs"
439+
export default defineConfig({
440+
i18n: {
441+
locales: [
442+
{ codes: ["it-VT", "it"], path: "italiano" },
443+
"es"
444+
]
445+
}
446+
})
447+
```
448+
449+
```astro title="src/pages/index.astro"
450+
---
451+
import { i18n } from "astro:config/client";
452+
import { toPaths } from "astro:i18n";
453+
454+
toPaths(i18n!.locales); // ["italiano", "es"]
455+
---
456+
```

0 commit comments

Comments
 (0)