|
| 1 | +# Multi-Language Sites |
| 2 | + |
| 3 | +`nuxt-kql` comes with built-in support for multi-language Kirby sites. For this to work, you will have to use the [kirby-headless-starter](https://github.com/johannschopplich/kirby-headless-starter), which provides a custom KQL endpoint to retrieve translated content. |
| 4 | + |
| 5 | +To fetch language-specific content from your Kirby instance, pass a `language` option with your query request for every language but the primary one. |
| 6 | + |
| 7 | +Take the following example to fetch data from the about page, where `en` is the default language, `de` the secondary one. The `en` slug is called `about`, the `de` slug `ueber-uns`. |
| 8 | + |
| 9 | +```ts |
| 10 | +// Since `en` is the default language, the `language` option isn't needed |
| 11 | +const { data } = await useKql({ |
| 12 | + query: 'kirby.page("home")' |
| 13 | +}) |
| 14 | + |
| 15 | +// For the `de` slug to be found, add the `language` option |
| 16 | +const { data } = await useKql( |
| 17 | + { |
| 18 | + query: 'kirby.page("ueber-uns")' |
| 19 | + }, |
| 20 | + { |
| 21 | + language: 'de' |
| 22 | + } |
| 23 | +) |
| 24 | +``` |
| 25 | + |
| 26 | +## Dynamic Routes |
| 27 | + |
| 28 | +You will probably use some kind of dynamic routes in your Nuxt application. To make your life easier fetching translated content, you can create a `useLocaleSlug` composable to extract both a page's `slug` and its language code: |
| 29 | + |
| 30 | +```ts |
| 31 | +// `/composables/language.ts` |
| 32 | + |
| 33 | +// Extract language code and slug from path |
| 34 | +export function useLocaleSlug(path: string) { |
| 35 | + const segments = path.split('/').filter(i => i !== '') |
| 36 | + |
| 37 | + return { |
| 38 | + language: segments[0], |
| 39 | + slug: segments.slice(1).join('/'), |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Now you can simplify your templates. For example in `/pages/[...id].vue`: |
| 45 | + |
| 46 | +```vue |
| 47 | +<script setup lang="ts"> |
| 48 | +const route = useRoute() |
| 49 | +const { language, slug } = useLocaleSlug(route.path) |
| 50 | +
|
| 51 | +const { data } = await useKql({ |
| 52 | + query: `kirby.page("${slug}")` |
| 53 | +}, { language }) |
| 54 | +</script> |
| 55 | +``` |
0 commit comments