Skip to content

Commit

Permalink
feat: add language selector
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanSchleret committed Jun 13, 2024
1 parent d9d8718 commit 497f34c
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 6 deletions.
22 changes: 20 additions & 2 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@
import type { ParsedContent } from '@nuxt/content/dist/runtime/types'
const { seo } = useAppConfig()
const route = useRoute()
const { data: navigation, refresh: refreshNavigation } = await useAsyncData('navigation', () => fetchContentNavigation(), {
transform: (data) => {
const result = []
data.forEach((item) => {
if (item.title.toLowerCase() === route.fullPath.split('/')[1]) {
if (item.children) {
item.children.forEach((child) => {
result.push(child)
})
}
}
})
return result
}
})
const { data: navigation } = await useAsyncData('navigation', () => fetchContentNavigation())
const { data: files } = useLazyFetch<ParsedContent[]>('/api/search.json', {
default: () => [],
server: false
Expand All @@ -29,7 +47,7 @@ useSeoMeta({
twitterCard: 'summary_large_image'
})
provide('navigation', navigation)
provide('navigation', { navigation, refreshNavigation })
</script>

<template>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion content/index.yml → content/en/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ hero:
- label: Get started
icon: i-heroicons-arrow-right-20-solid
trailing: true
to: '/getting-started'
to: '/en/getting-started'
size: lg
- label: Use this template
icon: i-simple-icons-github
Expand Down
30 changes: 29 additions & 1 deletion layouts/docs.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
<script setup lang="ts">
import type { NavItem } from '@nuxt/content/dist/runtime/types'
import type { Ref } from 'vue'
import type { Lang } from '~/types/Lang'
const navigation = inject<Ref<NavItem[]>>('navigation')
const route = useRoute()
const router = useRouter()
const config = useRuntimeConfig()
const { navigation, refreshNavigation } = inject<Ref<NavItem[]>>('navigation')
const languages = config.public.languages.map((lang: Lang) => ({
name: lang.name,
value: lang.code
}))
const language = ref(languages.find((lang) => lang.value === route.fullPath.split('/')[1] && lang.value)?.value || config.public.defaultLanguage)
watch(language, (value) => {
router.push(
route.fullPath.replace(`/${route.fullPath.split('/')[1]}/`, `/${value}/`)
)
refreshNavigation()
})
onMounted(() => {
refreshNavigation()
})
</script>

<template>
<UContainer>
<UPage>
<template #left>
<UAside>
<USelect
v-model="language"
:options="languages"
option-attribute="name"
class="mb-2"
/>
<UNavigationTree :links="mapContentNavigation(navigation)" />
</UAside>
</template>
Expand Down
14 changes: 14 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// https://nuxt.com/docs/api/configuration/nuxt-config

import { resolve } from 'node:url'

export default defineNuxtConfig({
extends: ['@nuxt/ui-pro'],
modules: [
Expand Down Expand Up @@ -39,5 +42,16 @@ export default defineNuxtConfig({
braceStyle: '1tbs'
}
}
},
runtimeConfig: {
public: {
defaultLanguage: 'en',
languages: [
{
code: 'en',
name: 'English'
},
]
}
}
})
6 changes: 4 additions & 2 deletions pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script setup lang="ts">
const { data: page } = await useAsyncData('index', () => queryContent('/').findOne())
const config = useRuntimeConfig()
const { data: page } = await useAsyncData('index', () => queryContent(`/${config.public.defaultLanguage}`).findOne())
useSeoMeta({
titleTemplate: '',
Expand Down Expand Up @@ -46,7 +48,7 @@ useSeoMeta({
</template>

<template #title>
<MDC :value="page.hero.title" />
<MDC :value="page.hero.title"/>
</template>

<MDC
Expand Down
4 changes: 4 additions & 0 deletions types/Lang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Lang {
name: string
code: string
}

0 comments on commit 497f34c

Please sign in to comment.