Skip to content

Commit

Permalink
feat: enhanced i18n (#517)
Browse files Browse the repository at this point in the history
* fix(Link): support locale path & improve attrs

* fix(contentLocalePath): handle locale prefix
  • Loading branch information
farnabaz authored Jun 28, 2021
1 parent 2daee98 commit a04c21a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/defaultTheme/components/atoms/Link.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<NuxtLink v-if="isInternal" :to="to">
<NuxtLink v-if="isInternal" :to="$contentLocalePath(to)">
<slot />
<slot name="nuxt-link" />
</NuxtLink>
Expand Down Expand Up @@ -34,7 +34,10 @@ export default defineComponent({
setup(props) {
const isInternal = computed(() => !props.static && props.to.startsWith('/') && props.to.startsWith('//') === false)
const linkAttrs = computed(() => (props.blank ? { rel: 'noopener nofollow', target: '_blank' } : {}))
const linkAttrs = computed(() => ({
rel: isInternal.value ? undefined : 'noopener nofollow noreferrer',
target: blank ? '_blank' : undefined
}))
return {
isInternal,
Expand Down
22 changes: 21 additions & 1 deletion src/i18n/runtime/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@ export default function ({ app }, inject) {
// This helper does not respect `router.trailingSlash`
// and add/remove trailingSlash baded on original path
inject('contentLocalePath', path => {
let localePath = app.localePath(path)
const { localeCodes, defaultLocale } = app.i18n

/**
* If `path` includes a locale do not change the locale
*/
let localePath = localeCodes.some(code => path.startsWith(`/${code}`)) ? path : app.localePath(path)

/**
* Remove default locale from path
*/
if (localePath.startsWith(`/${defaultLocale}`)) {
localePath = localePath.replace(`/${defaultLocale}`, '')
}

/**
* Preserve trailing slash in generated path
*/
if (path.endsWith('/') && !localePath.endsWith('/')) {
localePath += '/'
}

/**
* Remove trailing slash from generated path
*/
if (!path.endsWith('/') && localePath.endsWith('/')) {
localePath = localePath.replace(/\/*$/, '')
}
Expand Down

1 comment on commit a04c21a

@vercel
Copy link

@vercel vercel bot commented on a04c21a Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.