-
-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add
useSetI18nParams
to playground
- Loading branch information
1 parent
5cec717
commit f488e82
Showing
7 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<script lang="ts" setup> | ||
const { locale, locales } = useI18n() | ||
const localePath = useLocalePath() | ||
const switchLocalePath = useSwitchLocalePath() | ||
const { data } = await useAsyncData('products', () => $fetch(`/api/products`)) | ||
definePageMeta({ | ||
pageTransition: { | ||
name: 'page', | ||
mode: 'out-in', | ||
onBeforeEnter: async () => { | ||
const { finalizePendingLocaleChange } = useNuxtApp().$i18n | ||
await finalizePendingLocaleChange() | ||
} | ||
} | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<nav style="padding: 1em"> | ||
<span v-for="locale in locales" :key="locale.code"> | ||
<NuxtLink :to="switchLocalePath(locale.code) || ''">{{ locale.name }}</NuxtLink> | | ||
</span> | ||
</nav> | ||
{{ locale }} | ||
<NuxtLink | ||
class="product" | ||
v-for="product in data" | ||
:key="product.id" | ||
:to="localePath({ name: 'products-slug', params: { slug: product?.slugs?.[locale] ?? 'none' } })" | ||
> | ||
{{ product.name?.[locale] }} | ||
</NuxtLink> | ||
<NuxtPage /> | ||
</div> | ||
</template> | ||
|
||
<style> | ||
.product { | ||
padding: 1em 0.5em; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<script lang="ts" setup> | ||
import { useI18n, useLocalePath, useSetI18nParams, useSwitchLocalePath } from '#i18n' | ||
import { ref, computed, onMounted, useHead, useRoute } from '#imports' | ||
// import LangSwitcher from '@/components/LangSwitcher.vue' | ||
const product = ref() | ||
const { locale, locales } = useI18n() | ||
const route = useRoute() | ||
const setI18nParams = useSetI18nParams() | ||
const { data, pending } = await useAsyncData(`products-${route.params.slug}`, () => | ||
$fetch(`/api/products/${route.params.slug}`) | ||
) | ||
if (data.value != null) { | ||
const availableLocales = Object.keys(data.value.slugs) | ||
const slugs: Record<string, string> = {} | ||
for (const l of availableLocales) { | ||
slugs[l] = { slug: data.value.slugs[l] } | ||
} | ||
setI18nParams(slugs) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<section class="product">{{ pending ? 'loading' : data?.name?.[locale] }}</section> | ||
</div> | ||
</template> | ||
|
||
<style> | ||
.product { | ||
padding: 1em 0.5em; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default [ | ||
{ | ||
id: 1, | ||
name: { | ||
en: 'Red mug', | ||
nl: 'Rode mok' | ||
}, | ||
slugs: { | ||
en: 'red-mug', | ||
nl: 'rode-mok' | ||
} | ||
}, | ||
{ | ||
id: 2, | ||
name: { | ||
en: 'Big chair', | ||
nl: 'Grote stoel' | ||
}, | ||
slugs: { | ||
en: 'big-chair', | ||
nl: 'grote-stoel' | ||
} | ||
}, | ||
{ | ||
id: 3, | ||
name: { | ||
en: 'Standing desk', | ||
nl: 'Sta bureau' | ||
}, | ||
slugs: { | ||
en: 'standing-desk', | ||
nl: 'sta-bureau' | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import productsData from './products-data' | ||
|
||
export default defineEventHandler(event => productsData) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import productsData from '../products-data' | ||
|
||
export default defineEventHandler(async event => { | ||
const slug = event.context.params?.product | ||
const found = productsData.find(x => Object.values(x.slugs).includes(slug)) | ||
|
||
// await new Promise(resolve => setTimeout(resolve, 600)) | ||
|
||
if (found == null) { | ||
return {} | ||
} | ||
|
||
return found | ||
}) |