@museumwnf/viewer-core 2.5.1 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/i18n/language.js +25 -6
package/package.json
CHANGED
package/src/i18n/language.js
CHANGED
|
@@ -93,7 +93,16 @@ export function resolveInitialLanguage(offered) {
|
|
|
93
93
|
export function connectLanguageToRouter({ locale, offered, router }) {
|
|
94
94
|
const multilingual = offered.length > 1
|
|
95
95
|
|
|
96
|
+
// The navigation under way, from the guard until it lands. Its URL already
|
|
97
|
+
// carries the language the guard set; rewriting the page being left
|
|
98
|
+
// instead cancels it, and the visitor stays there in the new language.
|
|
99
|
+
let pending = null
|
|
100
|
+
const settle = (to) => {
|
|
101
|
+
if (pending === to) pending = null
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
router.beforeEach((to) => {
|
|
105
|
+
pending = to
|
|
97
106
|
const next = negotiateLanguage(offered, {
|
|
98
107
|
requested: multilingual ? to.query.lang : undefined,
|
|
99
108
|
stored: readStoredLanguage(),
|
|
@@ -107,18 +116,28 @@ export function connectLanguageToRouter({ locale, offered, router }) {
|
|
|
107
116
|
return true
|
|
108
117
|
})
|
|
109
118
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
119
|
+
router.afterEach((to, from, failure) => {
|
|
120
|
+
settle(to)
|
|
121
|
+
// The visitor switched language while this page loaded.
|
|
122
|
+
if (!failure && multilingual && to.query.lang !== locale.value) {
|
|
123
|
+
router.replace({ query: { ...to.query, lang: locale.value } })
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
router.onError((error, to) => settle(to))
|
|
127
|
+
|
|
128
|
+
// A change of language from the switcher rewrites the URL. Not while a
|
|
129
|
+
// navigation is under way (see `pending`), and so not before the first one
|
|
130
|
+
// has resolved: the router is then still at its start location, `/`, and a
|
|
131
|
+
// replace from there cancels the navigation the visitor arrived with, so
|
|
132
|
+
// every deep link landed on the home page. The guard above puts the
|
|
133
|
+
// language in the URL of that first navigation itself.
|
|
115
134
|
watch(
|
|
116
135
|
locale,
|
|
117
136
|
(code) => {
|
|
118
137
|
applyDocumentLanguage(code)
|
|
119
138
|
storeLanguage(code)
|
|
120
139
|
const current = router.currentRoute.value
|
|
121
|
-
if (multilingual && current !== START_LOCATION && current.query.lang !== code) {
|
|
140
|
+
if (multilingual && !pending && current !== START_LOCATION && current.query.lang !== code) {
|
|
122
141
|
router.replace({ query: { ...current.query, lang: code } })
|
|
123
142
|
}
|
|
124
143
|
},
|