Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add language selector #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
12 changes: 12 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// https://nuxt.com/docs/api/configuration/nuxt-config

export default defineNuxtConfig({
extends: ['@nuxt/ui-pro'],
modules: [
Expand Down Expand Up @@ -39,5 +40,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
}