@uniweb/core 1.4.3 → 1.4.4
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/resolve-route.js +23 -9
- package/src/uniweb.js +4 -0
- package/src/website.js +34 -0
package/package.json
CHANGED
package/src/resolve-route.js
CHANGED
|
@@ -150,29 +150,43 @@ export function reverseTranslateRoute(site, displayRoute, locale) {
|
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
153
|
+
* A page's canonical route, from its route canonical or as the active locale shows it: the
|
|
154
|
+
* active locale's prefix comes off, then its display route is reverse-translated. How
|
|
155
|
+
* `localeUrl` reads the route it is given.
|
|
156
156
|
*
|
|
157
157
|
* @param {Object} site - a locale's site content, or its `routeIndex`
|
|
158
|
-
* @param {string} localeCode - the locale the URL is for
|
|
159
158
|
* @param {string} route - the page's route, canonical or as the active locale shows it
|
|
160
159
|
* @param {{ activeLocale?: string, defaultLocale?: string }} [context]
|
|
161
|
-
* @returns {string}
|
|
160
|
+
* @returns {string}
|
|
162
161
|
*/
|
|
163
|
-
export function
|
|
162
|
+
export function canonicalRoute(site, route, context = {}) {
|
|
164
163
|
const index = indexOf(site)
|
|
165
164
|
const activeLocale = context.activeLocale ?? index.activeLocale
|
|
166
165
|
const defaultLocale = context.defaultLocale ?? index.defaultLocale
|
|
167
166
|
let target = route
|
|
168
|
-
|
|
169
|
-
// The active locale's prefix, then its display route → canonical.
|
|
170
167
|
if (activeLocale && activeLocale !== defaultLocale) {
|
|
171
168
|
const prefix = `/${activeLocale}`
|
|
172
169
|
if (target === prefix || target === `${prefix}/`) target = '/'
|
|
173
170
|
else if (target.startsWith(`${prefix}/`)) target = target.slice(prefix.length)
|
|
174
171
|
}
|
|
175
|
-
|
|
172
|
+
return reverseTranslateRoute(index, target, activeLocale)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* ⭐ **A PAGE'S URL IN A LOCALE** — the one rule the language switcher, a redirect's destination
|
|
177
|
+
* and the canonical form share. The locale served unprefixed shows the canonical route; a locale
|
|
178
|
+
* with a domain of its own is that domain; any other is `/{code}` plus its translated route.
|
|
179
|
+
*
|
|
180
|
+
* @param {Object} site - a locale's site content, or its `routeIndex`
|
|
181
|
+
* @param {string} localeCode - the locale the URL is for
|
|
182
|
+
* @param {string} route - the page's route, canonical or as the active locale shows it
|
|
183
|
+
* @param {{ activeLocale?: string, defaultLocale?: string }} [context]
|
|
184
|
+
* @returns {string} a path, or an absolute URL on a locale's own domain
|
|
185
|
+
*/
|
|
186
|
+
export function localeUrl(site, localeCode, route, context = {}) {
|
|
187
|
+
const index = indexOf(site)
|
|
188
|
+
const defaultLocale = context.defaultLocale ?? index.defaultLocale
|
|
189
|
+
const target = canonicalRoute(index, route, context)
|
|
176
190
|
|
|
177
191
|
const designated = index.domainLocales && Object.entries(index.domainLocales).find(([, lang]) => lang === localeCode)
|
|
178
192
|
if (designated) {
|
package/src/uniweb.js
CHANGED
|
@@ -58,6 +58,10 @@ export default class Uniweb {
|
|
|
58
58
|
// (`background: 'self'`). Filled by each boot path, like `childBlockRenderer`.
|
|
59
59
|
this.backgroundRenderer = null
|
|
60
60
|
this.routingComponents = {}
|
|
61
|
+
// How this host switches locale: `({ locale, route, url }) => void`, or null for a page load
|
|
62
|
+
// of `url` (`Website#switchLocale`). An editor's preview, which hands the runtime each
|
|
63
|
+
// locale's content itself, sets it through `initUniweb({ localeSwitch })`.
|
|
64
|
+
this.localeSwitch = null
|
|
61
65
|
this.language = 'en'
|
|
62
66
|
|
|
63
67
|
// Framework-level inset components — every foundation gets these
|
package/src/website.js
CHANGED
|
@@ -22,9 +22,11 @@ import {
|
|
|
22
22
|
routeIndex,
|
|
23
23
|
resolveRoute as resolveRouteIn,
|
|
24
24
|
localeUrl,
|
|
25
|
+
canonicalRoute,
|
|
25
26
|
translateRoute as translateRouteIn,
|
|
26
27
|
reverseTranslateRoute as reverseTranslateRouteIn,
|
|
27
28
|
} from './resolve-route.js'
|
|
29
|
+
import { applyBasePath } from './base-path.js'
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* Services whose provider is not simply an address, keyed by name.
|
|
@@ -1006,6 +1008,38 @@ export default class Website {
|
|
|
1006
1008
|
return localeUrl(this._routeIndex, localeCode, targetRoute, this._routeContext())
|
|
1007
1009
|
}
|
|
1008
1010
|
|
|
1011
|
+
/**
|
|
1012
|
+
* Show the page in another locale.
|
|
1013
|
+
*
|
|
1014
|
+
* A locale is its own document, with its own content and routes, so this is a page load of
|
|
1015
|
+
* `getLocaleUrl()` with the base path applied. A host that hands the runtime each locale's
|
|
1016
|
+
* content itself, like an editor's preview, switches its own way instead
|
|
1017
|
+
* (`uniweb.localeSwitch`): it is told the locale, the page's canonical route and the URL a
|
|
1018
|
+
* page load would go to.
|
|
1019
|
+
*
|
|
1020
|
+
* For a menu that switches from code, like a `<select>`. A menu of links needs no call:
|
|
1021
|
+
* `<Link reload href={website.getLocaleUrl(code)}>` loads the page, and the runtime hands a
|
|
1022
|
+
* click on a link to another locale's page to such a host the same way.
|
|
1023
|
+
*
|
|
1024
|
+
* @param {string} localeCode - One of `getLocales()`
|
|
1025
|
+
* @param {string} [route] - The page, canonical or as the active locale shows it
|
|
1026
|
+
* (default: the current page)
|
|
1027
|
+
* @returns {boolean} false when the site has no such locale, and nothing happens
|
|
1028
|
+
*/
|
|
1029
|
+
switchLocale(localeCode, route = null) {
|
|
1030
|
+
if (!this.getLocale(localeCode)) return false
|
|
1031
|
+
const context = this._routeContext()
|
|
1032
|
+
const page = route || this.activePage.getNavRoute()
|
|
1033
|
+
const url = applyBasePath(localeUrl(this._routeIndex, localeCode, page, context), this.basePath)
|
|
1034
|
+
const hostSwitch = globalThis.uniweb?.localeSwitch
|
|
1035
|
+
if (hostSwitch) {
|
|
1036
|
+
hostSwitch({ locale: localeCode, route: canonicalRoute(this._routeIndex, page, context), url })
|
|
1037
|
+
} else if (typeof window !== 'undefined') {
|
|
1038
|
+
window.location.assign(url)
|
|
1039
|
+
}
|
|
1040
|
+
return true
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1009
1043
|
/**
|
|
1010
1044
|
* Get locale info by code
|
|
1011
1045
|
* @param {string} localeCode - Locale code
|