Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Switch to path-based i18n routing #701

Merged
merged 7 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,19 @@ export default {
],
lazy: true,
langDir: 'locales',
strategy: 'no_prefix',
defaultLocale: 'en',
/**
* This section is critical for the current, iframed production environment
* {@link https://i18n.nuxtjs.org/options-reference/#detectbrowserlanguage}
* `detectBrowserLanguage` must be false to prevent nuxt/i18n from automatically
* setting the locale based on headers or the client-side `navigator` object.
*
* Such detection is handled at the parent level in WP.org.
*
* More info about the Nuxt i18n:
*
* - [detectBrowserLanguage](https://i18n.nuxtjs.org/options-reference/#detectbrowserlanguage)
* - [Browser language detection info](https://i18n.nuxtjs.org/browser-language-detection)
* */
detectBrowserLanguage: {
useCookie: true,
cookieKey: 'i18n_redirected',
alwaysRedirect: true,
cookieCrossOrigin: true,
cookieSecure: true,
},
baseUrl: 'http://localhost:8443',
detectBrowserLanguage: false,
zackkrida marked this conversation as resolved.
Show resolved Hide resolved
vueI18n: '~/plugins/vue-i18n.js',
},
/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/VAllResultsGrid/VAllResultsGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<NuxtLink
v-for="[key, item] in results"
:key="key"
:to="{ path: `/search/${key}`, query: $route.query }"
:to="localePath({ path: `/search/${key}`, query: $route.query })"
class="lg:col-span-2 focus:bg-white focus:border-tx focus:ring focus:ring-pink focus:outline-none focus:shadow-ring focus:text-black rounded-sm"
>
<VContentLink :results-count="item.count" :media-type="key" />
Expand Down
12 changes: 10 additions & 2 deletions src/composables/use-match-routes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { useContext } from '@nuxtjs/composition-api'
import { ref, useRoute, useRouter } from '@nuxtjs/composition-api'

/**
* Reactive property that returns true only on the matching routes.
* Note that routes are matched by name, not the url path.
*
* Routes are also localized before comparison, so 'search' becomes
* 'search__en', for example.
*
* @returns {{matches: import('@nuxtjs/composition-api').Ref<boolean>}}
*/
export const useMatchRoute = (routes = []) => {
const { app } = useContext()
const route = useRoute()
const router = useRouter()
const matches = ref(routes.includes(route.value.name))
const localizedRoutes = routes.map(
(route) => app.localeRoute({ name: route }).name
)
const matches = ref(localizedRoutes.includes(route.value.name))
router.beforeEach((to, from, next) => {
matches.value = routes.includes(to.name)
matches.value = localizedRoutes.includes(to.name)
next()
})
return { matches }
Expand Down
5 changes: 4 additions & 1 deletion src/pages/audio/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ const AudioDetailPage = {
},
beforeRouteEnter(to, from, nextPage) {
nextPage((_this) => {
if (from.path === '/search/' || from.path === '/search/audio') {
if (
from.name === _this.localeRoute({ path: '/search/' }).name ||
from.name === _this.localeRoute({ path: '/search/audio' }).name
) {
_this.shouldShowBreadcrumb = true
_this.breadCrumbURL = from.fullPath
}
Expand Down
5 changes: 4 additions & 1 deletion src/pages/image/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ const PhotoDetailPage = {
},
beforeRouteEnter(to, from, nextPage) {
nextPage((_this) => {
if (from.path === '/search/' || from.path === '/search/image') {
if (
from.name === _this.localeRoute({ path: '/search/' }).name ||
from.name === _this.localeRoute({ path: '/search/image' }).name
) {
_this.shouldShowBreadcrumb = true
_this.breadCrumbURL = from.fullPath
}
Expand Down
10 changes: 6 additions & 4 deletions src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ const HomePage = {
images: setItem.images.map((imageItem) => ({
...imageItem,
src: require(`~/assets/homepage_images/${setItem.prefix}-${imageItem.index}.jpg`),
url: router.resolve({
name: 'image-id',
params: { id: imageItem.identifier },
}).href,
url: router.resolve(
app.localePath({
name: 'image-id',
params: { id: imageItem.identifier },
})
).href,
})),
}))

Expand Down