Skip to content

Configuration

Configure the integration

You can pass the following options to the integration.

defaultLocale (required)

Type: string

Sets the default locale for your website.

locales (required)

Type: Array<string>

Sets the available locales for your website. Must include the default locale.

strategy

Type: "prefixWithoutDefault" | "prefix"

Default: "prefixWithoutDefault"

Defines how your routes are generated:

  • "prefixWithoutDefault" will not add a prefix for your default locale:

    • Directorysrc/routes/
      • index.astro / and /fr
      • about.astro /about and /fr/about
  • "prefix" will add a prefix for your default locale:

    • Directorysrc/routes/
      • index.astro /en and /fr
      • about.astro /en/about and /fr/about

pages

Type: Record<string, Record<string, string>>

Allows you to define translated paths for your locales. For example:

Integration configuration:

1
i18n({
2
// ...
3
strategy: "prefixWithoutDefault",
4
pages: {
5
"/about": {
6
fr: "/a-propos"
7
},
8
"/blog/[slug]": {
9
fr: "/le-blog/[slug]"
10
}
11
}
12
})

Project structure

  • Directorysrc/routes/
    • index.astro
    • about.astro
    • contact.astro
    • Directoryblog/
      • [slug].astro

URL structure

  • Directory/
  • about
  • contact
  • Directoryblog/
    • a
    • b
  • Directoryfr/
    • a-propos
    • contact
    • Directoryle-blog/
      • a
      • b

localesDir

Type: string

Default: "./src/locales"

A path relative to the root where locales files are located for translations features.

defaultNamespace

Type: string

Default: "common"

Sets the default namespace for locales. Since this package uses i18next under the hood, it allows to split translations data in multiple json files under src/locales/[locale]/. If you’re not using a file called common.json, you need to update this property to have proper types completions when using t.

  • Directorysrc/locales/
    • Directoryen/
      • shared.json Update this option to "shared" if you’re not using the default "common"
      • test.json
    • Directoryfr/
      • shared.json

client

Type: false | ClientConfig

Default: false

Client usage is disabled by default because it sends some JavaScript to the browser. Enabling any of the following features requires importing the <I18nClient/> component.

ClientConfig

UtilityRequired features
tdata, translations
getLocaledata
getLocalesdata
getDefaultLocaledata
getHtmlAttrsdata
setDynamicParamsN/A, server only
getLocalePathdata, paths
switchLocalePathdata, paths
getSwitcherDatadata, paths
getLocalePlaceholderN/A, getStaticPaths only
  • data: boolean, defaults to false
  • paths: boolean, defaults to false
  • translations: boolean, defaults to false

rootRedirect

Type: { status: number; destination: string }

When using strategy: "prefix", you may want to redirect your users from the root to a specific page (likely the default locale root). This option allows you to do so:

1
i18n({
2
// ...
3
strategy: "prefix",
4
rootRedirect: {
5
status: 301,
6
destination: "/en"
7
}
8
})