-
-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add
useSetI18nParams
composable (#2580)
* feat: add `useSetI18nParams` composable * test: test `useI18nParams` composable * chore: add `useSetI18nParams` to playground * docs: remove and replace dynamic parameter usage with `useSetI18nParams` * fix: add `useSetI18nParams` to auto imports * test: fix dynamic parameters test * fix: prevent hydration mismatch by not using `useState` * fix: add deprecation warning for `nuxtI18n` usage * fix: revert docs and refer to composable * docs: improve `useSetI18nParams` documentation * test: improve `useSetI18nParams` test * chore: cleanup playground * fix: invert `useSetI18nParams` defaults * fix: playground products page not changing locale * fix: use `useLogger` for meta deprecation bundler plugin * fix: change `useSetI18nParams` parameters
- Loading branch information
1 parent
7d5971f
commit 898d32a
Showing
33 changed files
with
838 additions
and
51 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
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
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
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', | ||
duration: 0, | ||
mode: 'default', | ||
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> | ||
<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,34 @@ | ||
<script lang="ts" setup> | ||
import { useI18n, useSetI18nParams } from '#i18n' | ||
import { useRoute } from '#imports' | ||
const { locale } = 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
Oops, something went wrong.