Skip to content

Commit

Permalink
feat: allow passing external links to localePath (#2759)
Browse files Browse the repository at this point in the history
* feat: return external when passed to `localePath`

* test: assert `localePath` returns external links
  • Loading branch information
BobbieGoede committed Feb 8, 2024
1 parent cd0117d commit d6874b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions specs/fixtures/routing/pages/index.vue
Expand Up @@ -45,6 +45,11 @@ const localeRoute = useLocaleRoute()

<!-- no define name -->
<span class="undefined-name">{{ localePath('vue-i18n') }}</span>

<!-- external -->
<span class="external-link">{{ localePath('https://github.com') }}</span>
<span class="external-mail">{{ localePath('mailto:example@mail.com') }}</span>
<span class="external-phone">{{ localePath('tel:+31612345678') }}</span>
</section>
<ClientOnly>
<section id="locale-route">
Expand Down
5 changes: 5 additions & 0 deletions specs/routing/routing-tests.ts
Expand Up @@ -64,6 +64,11 @@ export async function localePathTests(strategy: Strategies) {
// undefined name
expect(await getText(page, '#locale-path .undefined-name')).toEqual('')

// external
expect(await getText(page, '#locale-path .external-link')).toEqual('https://github.com')
expect(await getText(page, '#locale-path .external-mail')).toEqual('mailto:example@mail.com')
expect(await getText(page, '#locale-path .external-phone')).toEqual('tel:+31612345678')

// for vue-router deprecation
// https://github.com/vuejs/router/blob/main/packages/router/CHANGELOG.md#414-2022-08-22
expect(consoleLogs.find(log => log.text.includes('Discarded invalid param(s)'))).toBeFalsy()
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/routing/compatibles/routing.ts
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { isString, assign } from '@intlify/shared'
import { parsePath, parseQuery, withTrailingSlash, withoutTrailingSlash } from 'ufo'
import { hasProtocol, parsePath, parseQuery, withTrailingSlash, withoutTrailingSlash } from 'ufo'
import { nuxtI18nOptions, DEFAULT_DYNAMIC_PARAMS_KEY } from '#build/i18n.options.mjs'
import { unref } from '#imports'

Expand Down Expand Up @@ -75,6 +75,11 @@ export function localePath(
route: RouteLocationRaw,
locale?: Locale // TODO: locale should be more type inference (completion)
): string {
// return external url as is
if (typeof route === 'string' && hasProtocol(route, { acceptRelative: true })) {
return route
}

const localizedRoute = resolveRoute(common, route, locale)
return localizedRoute == null ? '' : localizedRoute.redirectedFrom?.fullPath || localizedRoute.fullPath
}
Expand Down

0 comments on commit d6874b5

Please sign in to comment.