-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(Link): auto-localize internal links when @nuxtjs/i18n is installed
#5537
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
Changes from all commits
ba407ae
32e8343
2a8c9ba
3474576
c8a7002
b0b0493
1ba5a09
27b9ced
6503cde
4ca5bc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,9 +110,9 @@ import { computed } from 'vue' | |
| import { isEqual } from 'ohash/utils' | ||
| import { useForwardProps, Slot } from 'reka-ui' | ||
| import { defu } from 'defu' | ||
| import { reactiveOmit } from '@vueuse/core' | ||
| import { hasProtocol } from 'ufo' | ||
| import { useRoute, useAppConfig } from '#imports' | ||
| import { reactiveOmit } from '@vueuse/core' | ||
| import { useRoute, useAppConfig, useNuxtApp } from '#imports' | ||
| import { mergeClasses } from '../utils' | ||
| import { tv } from '../utils/tv' | ||
| import { isPartiallyEqual } from '../utils/link' | ||
|
|
@@ -130,6 +130,7 @@ defineSlots<LinkSlots>() | |
|
|
||
| const route = useRoute() | ||
| const appConfig = useAppConfig() as Link['AppConfig'] | ||
| const nuxtApp = useNuxtApp() | ||
|
|
||
| const nuxtLinkProps = useForwardProps(reactiveOmit(props, 'as', 'type', 'disabled', 'active', 'exact', 'exactQuery', 'exactHash', 'activeClass', 'inactiveClass', 'to', 'href', 'raw', 'custom', 'class')) | ||
|
|
||
|
|
@@ -145,7 +146,26 @@ const ui = computed(() => tv({ | |
| }, appConfig.ui?.link || {}) | ||
| })) | ||
|
|
||
| const to = computed(() => props.to ?? props.href) | ||
| const to = computed(() => { | ||
| const path = props.to ?? props.href | ||
| if (!path) return path | ||
|
|
||
| // Only localize string paths, leave route objects untouched to preserve state/params | ||
| if (typeof path !== 'string') return path | ||
|
|
||
| // Skip external links and absolute URLs | ||
| if (props.external || hasProtocol(path, { acceptRelative: true })) { | ||
| return path | ||
| } | ||
|
|
||
| // Use `$localePath` from `@nuxtjs/i18n` if available | ||
| const localePath = nuxtApp.$localePath as ((route: RouteLocationRaw, locale?: string) => string) | undefined | ||
| if (localePath) { | ||
| return localePath(path) | ||
| } | ||
|
|
||
| return path | ||
| }) | ||
|
Comment on lines
+149
to
+168
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π§© Analysis chainπ Web query:
π‘ Result: Yes, $localePath in Citations:
Add The typeof guard at line 154 correctly preserves route objects' Two gaps remain:
Consider a global π€ Prompt for AI Agents |
||
|
|
||
| const isInternalLink = computed(() => { | ||
| if (!to.value) return false | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.