-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add NuxtLinkLocale component (#2229)
* feat: add NuxtLinkLocale component * refactor: rewrite NuxtLinkLocale sfc as render function * fix: path resolution for #imports alias * docs: add components page * test: add NuxtLinkLocale tests * fix: hard-coded NuxtLinkLocale props * docs: change component example to be more realistic * fix: move nuxi prepare from postinstall to test script * fix: add dev:prepare script to run before dev and test scripts
- Loading branch information
1 parent
53161c6
commit 9dfb024
Showing
15 changed files
with
138 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Components | ||
|
||
Components API for Nuxt i18n module. | ||
|
||
## `<NuxtLinkLocale>` | ||
This component is built on top of [`<NuxtLink>`](https://nuxt.com/docs/api/components/nuxt-link#nuxtlink) but changes the default behavior by internally using [`localePath()`](/api/vue#localepath) to make it easier to link to localized routes. | ||
|
||
### Examples | ||
|
||
#### Basic usage | ||
```vue | ||
<template> | ||
<NuxtLinkLocale to="/">{{ $t('home') }}</NuxtLinkLocale> | ||
</template> | ||
<!-- equivalent to --> | ||
<script setup> | ||
const localePath = useLocalePath() | ||
</script> | ||
<template> | ||
<NuxtLink :to="localePath('/')">{{ $t('home') }}</NuxtLink> | ||
</template> | ||
``` | ||
|
||
#### Forcing locale resolution | ||
```vue | ||
<template> | ||
<NuxtLinkLocale to="/" locale="nl">{{ $t('home') }}</NuxtLinkLocale> | ||
</template> | ||
<!-- equivalent to --> | ||
<script setup> | ||
const localePath = useLocalePath() | ||
</script> | ||
<template> | ||
<NuxtLink :to="localePath('/', 'nl')">{{ $t('home') }}</NuxtLink> | ||
</template> | ||
``` | ||
|
||
### Props | ||
This component supports all [props documented for `<NuxtLink>`](https://nuxt.com/docs/api/components/nuxt-link#props) in addition to props described below. | ||
|
||
|Prop |Description | | ||
|--- |--- | | ||
|`locale` |Optional prop to force localization using passed Locale, it defaults to the current locale. Identical to `locale` argument of `localePath()`| | ||
|
||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,30 @@ | ||
import { useLocalePath } from '#i18n' | ||
import { defineComponent, computed, defineNuxtLink, h } from '#imports' | ||
|
||
import type { PropType } from 'vue' | ||
import type { RawLocation, RouteLocation } from '@intlify/vue-router-bridge' | ||
|
||
const NuxtLinkLocale = defineNuxtLink({ componentName: 'NuxtLinkLocale' }) | ||
|
||
export default defineComponent({ | ||
name: 'NuxtLinkLocale', | ||
props: { | ||
...NuxtLinkLocale.props, | ||
to: { | ||
type: [String, Object] as PropType<RawLocation | RouteLocation>, | ||
default: undefined, | ||
required: false | ||
}, | ||
locale: { | ||
type: String as PropType<string>, | ||
default: undefined, | ||
required: false | ||
} | ||
}, | ||
setup(props, { slots }) { | ||
const localePath = useLocalePath() | ||
const resolvedPath = computed(() => (props.to != null ? localePath(props.to, props.locale) : props.to)) | ||
|
||
return () => h(NuxtLinkLocale, { ...props, to: resolvedPath }, slots.default) | ||
} | ||
}) |
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