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
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
+
exportdefaultdefineConfig({
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
+
<Sincev="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
+
exportdefaultdefineConfig({
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
+
<Sincev="4.0.0" />
434
+
</p>
435
+
436
+
Retrieves the configured locale paths for each locale defined in your configuration.
0 commit comments