vue-i18n-routing
Advanced tools
Comparing version 0.0.0-e11df26 to 0.0.0-e1405ae
@@ -1,5 +0,392 @@ | ||
const VueI18nRouting = { | ||
routes: [] | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
console.log("this is vue-i18n-rouging!"); | ||
export { VueI18nRouting }; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __objRest = (source, exclude) => { | ||
var target = {}; | ||
for (var prop in source) | ||
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) | ||
target[prop] = source[prop]; | ||
if (source != null && __getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(source)) { | ||
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) | ||
target[prop] = source[prop]; | ||
} | ||
return target; | ||
}; | ||
import { ref, computed, isVue2, isRef as isRef$1 } from "vue-demi"; | ||
import { useRouter, useRoute } from "@intlify/vue-router-composable"; | ||
import { useI18n } from "@intlify/vue-i18n-bridge"; | ||
/*! | ||
* shared v9.2.0-beta.25 | ||
* (c) 2021 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/ | ||
const assign = Object.assign; | ||
const isString = (val) => typeof val === "string"; | ||
const isSymbol$1 = (val) => typeof val === "symbol"; | ||
const isSymbol = (val) => typeof val === "symbol"; | ||
new Set(Object.getOwnPropertyNames(Symbol).map((key) => Symbol[key]).filter(isSymbol)); | ||
function isRef(r) { | ||
return Boolean(r && r.__v_isRef === true); | ||
} | ||
Promise.resolve(); | ||
function getNormalizedLocales(locales) { | ||
locales = locales || []; | ||
const normalized = []; | ||
for (const locale of locales) { | ||
if (isString(locale)) { | ||
normalized.push({ code: locale }); | ||
} else { | ||
normalized.push(locale); | ||
} | ||
} | ||
return normalized; | ||
} | ||
function isComposer(target, mode) { | ||
return isRef(target.locale) && mode === "composition"; | ||
} | ||
function isI18nInstance(i18n) { | ||
return "global" in i18n && "mode" in i18n; | ||
} | ||
function getLocale(i18n) { | ||
return isI18nInstance(i18n) ? isComposer(i18n.global, i18n.mode) ? i18n.global.locale.value : i18n.global.locale : isRef(i18n.locale) ? i18n.locale.value : i18n.locale; | ||
} | ||
function setLocale(i18n, locale) { | ||
if (isI18nInstance(i18n)) { | ||
if (isComposer(i18n.global, i18n.mode)) { | ||
i18n.global.locale.value = locale; | ||
} else { | ||
i18n.global.locale = locale; | ||
} | ||
} else if (isRef(i18n.locale)) { | ||
i18n.locale.value = locale; | ||
} | ||
} | ||
function adjustRoutePathForTrailingSlash(pagePath, trailingSlash, isChildWithRelativePath) { | ||
return pagePath.replace(/\/+$/, "") + (trailingSlash ? "/" : "") || (isChildWithRelativePath ? "" : "/"); | ||
} | ||
function extendI18n(i18n, { localeCodes = [] } = {}) { | ||
if (!isComposer(i18n.global, i18n.mode)) { | ||
throw new Error(""); | ||
} | ||
const _localeCodes = ref(localeCodes); | ||
i18n.global.locales = computed(() => _localeCodes.value); | ||
console.log("... extends i18n done"); | ||
} | ||
const STRATEGIES = { | ||
PREFIX: "prefix", | ||
PREFIX_EXCEPT_DEFAULT: "prefix_except_default", | ||
PREFIX_AND_DEFAULT: "prefix_and_default", | ||
NO_PREFIX: "no_prefix" | ||
}; | ||
const DEFAULT_LOCALE = ""; | ||
const DEFAULT_STRATEGY = STRATEGIES.PREFIX_EXCEPT_DEFAULT; | ||
const DEFAULT_TRAILING_SLASH = false; | ||
const DEFAULT_ROUTES_NAME_SEPARATOR = "___"; | ||
const DEFAULT_LOCALE_ROUTE_NAME_SUFFIX = "default"; | ||
function localizeRoutes(routes, { | ||
defaultLocale = DEFAULT_LOCALE, | ||
strategy = DEFAULT_STRATEGY, | ||
trailingSlash = DEFAULT_TRAILING_SLASH, | ||
routesNameSeparator = DEFAULT_ROUTES_NAME_SEPARATOR, | ||
defaultLocaleRouteNameSuffix = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX, | ||
includeUprefixedFallback = false, | ||
localeCodes = [] | ||
} = {}) { | ||
if (strategy === "no_prefix") { | ||
return routes; | ||
} | ||
const _localeCodes = localeCodes.map((locale) => isString(locale) ? locale : locale.code); | ||
function makeLocalizedRoutes(route, allowedLocaleCodes, isChild = false, isExtraPageTree = false) { | ||
if (route.redirect && (!route.component || !route.file)) { | ||
return [route]; | ||
} | ||
const targetLocales = allowedLocaleCodes; | ||
return targetLocales.reduce((_routes, locale) => { | ||
const { name } = route; | ||
let { path } = route; | ||
const localizedRoute = __spreadValues({}, route); | ||
if (name) { | ||
localizedRoute.name = `${name}${routesNameSeparator}${locale}`; | ||
} | ||
if (route.children) { | ||
localizedRoute.children = route.children.reduce((children, child) => [...children, ...makeLocalizedRoutes(child, [locale], true, isExtraPageTree)], []); | ||
} | ||
const isDefaultLocale = locale === defaultLocale; | ||
if (isDefaultLocale && strategy === "prefix_and_default") { | ||
if (!isChild) { | ||
const defaultRoute = __spreadProps(__spreadValues({}, localizedRoute), { path }); | ||
if (name) { | ||
defaultRoute.name = `${localizedRoute.name}${routesNameSeparator}${defaultLocaleRouteNameSuffix}`; | ||
} | ||
if (route.children) { | ||
defaultRoute.children = []; | ||
for (const childRoute of route.children) { | ||
defaultRoute.children = defaultRoute.children.concat(makeLocalizedRoutes(childRoute, [locale], true, true)); | ||
} | ||
} | ||
_routes.push(defaultRoute); | ||
} else if (isChild && isExtraPageTree && name) { | ||
localizedRoute.name += `${routesNameSeparator}${defaultLocaleRouteNameSuffix}`; | ||
} | ||
} | ||
const isChildWithRelativePath = isChild && !path.startsWith("/"); | ||
const shouldAddPrefix = !isChildWithRelativePath && !(isDefaultLocale && strategy === "prefix_except_default"); | ||
if (shouldAddPrefix) { | ||
path = `/${locale}${path}`; | ||
} | ||
if (path) { | ||
path = adjustRoutePathForTrailingSlash(path, trailingSlash, isChildWithRelativePath); | ||
} | ||
if (shouldAddPrefix && isDefaultLocale && strategy === "prefix" && includeUprefixedFallback) { | ||
_routes.push(__spreadValues({}, route)); | ||
} | ||
localizedRoute.path = path; | ||
_routes.push(localizedRoute); | ||
return _routes; | ||
}, []); | ||
} | ||
return routes.reduce((localized, route) => [...localized, ...makeLocalizedRoutes(route, _localeCodes || [])], []); | ||
} | ||
function getLocalesRegex(localeCodes) { | ||
return new RegExp(`^/(${localeCodes.join("|")})(?:/|$)`, "i"); | ||
} | ||
function createLocaleFromRouteGetter(localeCodes, routesNameSeparator, defaultLocaleRouteNameSuffix) { | ||
const localesPattern = `(${localeCodes.join("|")})`; | ||
const defaultSuffixPattern = `(?:${routesNameSeparator}${defaultLocaleRouteNameSuffix})?`; | ||
const regexpName = new RegExp(`${routesNameSeparator}${localesPattern}${defaultSuffixPattern}$`, "i"); | ||
const regexpPath = getLocalesRegex(localeCodes); | ||
const getLocaleFromRoute = (route) => { | ||
if (route.name) { | ||
const name = isString(route.name) ? route.name : route.name.toString(); | ||
const matches = name.match(regexpName); | ||
if (matches && matches.length > 1) { | ||
return matches[1]; | ||
} | ||
} else if (route.path) { | ||
const matches = route.path.match(regexpPath); | ||
if (matches && matches.length > 1) { | ||
return matches[1]; | ||
} | ||
} | ||
return ""; | ||
}; | ||
return getLocaleFromRoute; | ||
} | ||
function extendRouter({ | ||
router, | ||
i18n, | ||
defaultLocale = DEFAULT_LOCALE, | ||
strategy = DEFAULT_STRATEGY, | ||
trailingSlash = DEFAULT_TRAILING_SLASH, | ||
routesNameSeparator = DEFAULT_ROUTES_NAME_SEPARATOR, | ||
defaultLocaleRouteNameSuffix = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX, | ||
localeCodes = [] | ||
} = {}) { | ||
const normalizedLocaleCodes = getNormalizedLocales(localeCodes); | ||
const getLocaleFromRoute = createLocaleFromRouteGetter(normalizedLocaleCodes.map((l) => l.code), routesNameSeparator, defaultLocaleRouteNameSuffix); | ||
extendI18n(i18n, { localeCodes: normalizedLocaleCodes }); | ||
if (isVue2) { | ||
const _router = router; | ||
const _VueRouter = _router.constructor; | ||
const routes = _router.options.routes || []; | ||
const localizedRoutes = localizeRoutes(routes, { | ||
localeCodes, | ||
defaultLocale, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix | ||
}); | ||
console.log("vue2 routes", routes, localizedRoutes); | ||
const newRouter = new _VueRouter({ | ||
mode: "history", | ||
base: _router.options.base, | ||
routes: localizedRoutes | ||
}); | ||
newRouter.__defaultLocale = defaultLocale; | ||
newRouter.__strategy = strategy; | ||
newRouter.__trailingSlash = trailingSlash; | ||
newRouter.__routesNameSeparator = routesNameSeparator; | ||
newRouter.__defaultLocaleRouteNameSuffix = defaultLocaleRouteNameSuffix; | ||
newRouter.beforeEach((to, from, next) => { | ||
console.log("beforeEach", to, from); | ||
const currentLocale = getLocale(i18n); | ||
const finalLocale = getLocaleFromRoute(to) || currentLocale || defaultLocale || ""; | ||
console.log("currentLocale", currentLocale, "finalLocale", finalLocale); | ||
if (currentLocale !== finalLocale) { | ||
setLocale(i18n, finalLocale); | ||
} | ||
next(); | ||
}); | ||
return newRouter; | ||
} else { | ||
const _router = router; | ||
const routes = _router.options.routes || []; | ||
const localizedRoutes = localizeRoutes(routes, { | ||
localeCodes, | ||
defaultLocale, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix | ||
}); | ||
console.log("vue3 routes", routes, localizedRoutes, _router); | ||
routes.forEach((r) => _router.removeRoute(r.name)); | ||
localizedRoutes.forEach((route) => _router.addRoute(route)); | ||
_router.__defaultLocale = defaultLocale; | ||
_router.__strategy = strategy; | ||
_router.__trailingSlash = trailingSlash; | ||
_router.__routesNameSeparator = routesNameSeparator; | ||
_router.__defaultLocaleRouteNameSuffix = defaultLocaleRouteNameSuffix; | ||
_router.beforeEach((to, from, next) => { | ||
console.log("beforeEach", to, from); | ||
const currentLocale = getLocale(i18n); | ||
const finalLocale = getLocaleFromRoute(to) || currentLocale || defaultLocale || ""; | ||
console.log("currentLocale", currentLocale, "finalLocale", finalLocale); | ||
if (currentLocale !== finalLocale) { | ||
setLocale(i18n, finalLocale); | ||
} | ||
next(); | ||
}); | ||
return _router; | ||
} | ||
} | ||
function useI18nRouting(options = {}) { | ||
const $i18n = useI18n(); | ||
const $router = useRouter(); | ||
const $route = useRoute(); | ||
const defaultLocaleRouteNameSuffix = options.defaultLocaleRouteNameSuffix || $router.__defaultLocaleRouteNameSuffix; | ||
const defaultLocale = options.defaultLocale || $router.__defaultLocale; | ||
const routesNameSeparator = options.routesNameSeparator || $router.__routesNameSeparator; | ||
const strategy = options.strategy || $router.__strategy; | ||
function getRouteBaseName(givenRoute) { | ||
const route = givenRoute != null ? givenRoute : isRef$1($route) ? $route.value : $route; | ||
if (!route.name) { | ||
return; | ||
} | ||
const name = getRouteName(route.name); | ||
return name.split(routesNameSeparator)[0]; | ||
} | ||
function getLocaleRouteName(routeName, locale) { | ||
let name = getRouteName(routeName) + (strategy === "no_prefix" ? "" : routesNameSeparator + locale); | ||
if (locale === defaultLocale && strategy === "prefix_and_default") { | ||
name += routesNameSeparator + defaultLocaleRouteNameSuffix; | ||
} | ||
return name; | ||
} | ||
function resolveRoute(route, locale) { | ||
const _locale = locale || getLocale($i18n); | ||
let _route = route; | ||
if (isString(route)) { | ||
if (route[0] === "/") { | ||
_route = { path: route }; | ||
} else { | ||
_route = { name: route }; | ||
} | ||
} | ||
let localizedRoute = assign({}, _route); | ||
if (localizedRoute.path && !localizedRoute.name) { | ||
const _resolvedRoute = $router.resolve(localizedRoute); | ||
const resolvedRoute2 = !isVue2 ? _resolvedRoute : _resolvedRoute.route; | ||
const resolvedRouteName = getRouteBaseName(resolvedRoute2); | ||
if (isString(resolvedRouteName)) { | ||
localizedRoute = { | ||
name: getLocaleRouteName(resolvedRouteName, _locale), | ||
params: resolvedRoute2.params, | ||
query: resolvedRoute2.query, | ||
hash: resolvedRoute2.hash | ||
}; | ||
} | ||
} else { | ||
localizedRoute.name = getLocaleRouteName(localizedRoute.name, _locale); | ||
const { params } = localizedRoute; | ||
if (params && params["0"] === void 0 && params.pathMatch) { | ||
params["0"] = params.pathMatch; | ||
} | ||
} | ||
const resolvedRoute = $router.resolve(localizedRoute); | ||
if (isVue2 ? resolvedRoute.route.name : resolvedRoute.name) { | ||
return resolvedRoute; | ||
} | ||
return $router.resolve(route); | ||
} | ||
function localePath(route, locale) { | ||
const localizedRoute = resolveRoute(route, locale); | ||
return localizedRoute == null ? "" : isVue2 ? localizedRoute.route.redirectedFrom || localizedRoute.route.fullPath : localizedRoute.redirectedFrom || localizedRoute.fullPath; | ||
} | ||
function localeRoute(route, locale) { | ||
const resolved = resolveRoute(route, locale); | ||
return resolved == null ? void 0 : isVue2 ? resolved.route : resolved; | ||
} | ||
function localeLocation(route, locale) { | ||
const resolved = resolveRoute(route, locale); | ||
return resolved == null ? void 0 : isVue2 ? resolved.location : resolved.href; | ||
} | ||
function switchLocalePath(locale) { | ||
const name = getRouteBaseName(); | ||
if (!name) { | ||
return ""; | ||
} | ||
const _a = isVue2 && isRef$1($route) ? $route.value : $route, { params } = _a, routeCopy = __objRest(_a, ["params"]); | ||
const langSwitchParams = {}; | ||
const baseRoute = assign({}, routeCopy, { | ||
name, | ||
params: __spreadProps(__spreadValues(__spreadValues({}, params), langSwitchParams), { | ||
0: params.pathMatch | ||
}) | ||
}); | ||
const path = localePath(baseRoute, locale); | ||
console.log("switchLocalePath", $i18n.locale.value, locale, path); | ||
return path; | ||
} | ||
return { | ||
localePath, | ||
localeRoute, | ||
localeLocation, | ||
switchLocalePath | ||
}; | ||
} | ||
function getRouteName(routeName) { | ||
return isString(routeName) ? routeName : isSymbol$1(routeName) ? routeName.toString() : ""; | ||
} | ||
function extendRouting({ | ||
router, | ||
i18n, | ||
defaultLocale = DEFAULT_LOCALE, | ||
trailingSlash = DEFAULT_TRAILING_SLASH, | ||
routesNameSeparator = DEFAULT_ROUTES_NAME_SEPARATOR, | ||
defaultLocaleRouteNameSuffix = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX, | ||
localeCodes = [] | ||
} = {}) { | ||
if (router == null) { | ||
throw new Error("TODO"); | ||
} | ||
return extendRouter({ | ||
router, | ||
i18n, | ||
defaultLocale, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
localeCodes | ||
}); | ||
} | ||
const VERSION = ""; | ||
export { VERSION, extendRouting, localizeRoutes, useI18nRouting }; |
{ | ||
"name": "vue-i18n-routing", | ||
"description": "The i18n routing with using vue-i18n", | ||
"version": "0.0.0-e11df26", | ||
"version": "0.0.0-e1405ae", | ||
"dependencies": { | ||
"vue": "next", | ||
"vue-router": "next" | ||
"@intlify/shared": "beta", | ||
"@intlify/vue-router-composable": "0.0.0-e1405ae", | ||
"@intlify/vue-i18n-bridge": "0.0.0-e1405ae", | ||
"vue-demi": "*" | ||
}, | ||
"devDependencies": { | ||
"@vitejs/plugin-vue": "^1.9.3", | ||
"vite": "^2.6.4" | ||
"@microsoft/api-extractor": "^7.18.20", | ||
"api-docs-gen": "^0.4.0", | ||
"rimraf": "^3.0.2", | ||
"vite": "^2.7.4", | ||
"vite-plugin-dts": "^0.9.6", | ||
"vue": "^3.2.23", | ||
"vue-i18n": "npm:vue-i18n@next", | ||
"vue-i18n-bridge": "beta", | ||
"vue-i18n-legacy": "npm:vue-i18n@latest", | ||
"vue-router": "^4.0.12", | ||
"vue-router3": "npm:vue-router@latest", | ||
"vue2": "npm:vue@2" | ||
}, | ||
"peerDependencies": { | ||
"@vue/composition-api": "^1.4.0", | ||
"vue": "^2.6.14 || ^3.2.0", | ||
"vue-router": "^3.5.3 || ^4.0.0", | ||
"vue-i18n": "^8.26.1 || ^9.2.0-beta.25", | ||
"vue-i18n-bridge": "^9.2.0-beta.25" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@vue/composition-api": { | ||
"optional": true | ||
} | ||
}, | ||
"keywords": [ | ||
@@ -20,10 +44,19 @@ "i18n", | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"index.mjs", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"main": "./dist/vue-i18n-routing.umd.js", | ||
"main": "./dist/vue-i18n-routing.cjs.js", | ||
"module": "./dist/vue-i18n-routing.es.js", | ||
"unpkg": "dist/vue-i18n-routing.iife.js", | ||
"jsdelivr": "dist/vue-i18n-routing.iife.js", | ||
"types": "dist/vue-i18n-routing.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/vue-i18n-routing.es.js", | ||
"require": "./dist/vue-i18n-routing.umd.js" | ||
"import": { | ||
"node": "./index.mjs", | ||
"default": "./dist/vue-i18n-routing.es.js" | ||
}, | ||
"require": "./dist/vue-i18n-routing.cjs.js" | ||
} | ||
@@ -54,4 +87,6 @@ }, | ||
"dev": "vite", | ||
"build": "vite build" | ||
"build": "vite build", | ||
"typecheck": "tsc -p . --noEmit", | ||
"build:docs": "api-docs-gen ./temp/vue-i18n-routing.api.json -c ./docsgen.config.js -o ./ -g noprefix" | ||
} | ||
} |
@@ -7,4 +7,8 @@ # vue-i18n-routing | ||
## đ¤ API | ||
About details, See the [API References]('./api.md) | ||
## Šī¸ License | ||
[MIT](http://opensource.org/licenses/MIT) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
35480
8
559
13
9
12
2
1
+ Added@intlify/shared@beta
+ Addedvue-demi@*
+ Added@intlify/core-base@9.14.2(transitive)
+ Added@intlify/message-compiler@9.14.2(transitive)
+ Added@intlify/shared@9.14.29.3.0-beta.27(transitive)
+ Added@intlify/vue-devtools@9.14.2(transitive)
+ Added@intlify/vue-i18n-bridge@0.0.0-e1405ae(transitive)
+ Added@intlify/vue-router-composable@0.0.0-e1405ae(transitive)
+ Added@vue/composition-api@1.7.2(transitive)
+ Addedvue@2.6.14(transitive)
+ Addedvue-demi@0.14.10(transitive)
+ Addedvue-i18n@9.14.2(transitive)
+ Addedvue-i18n-bridge@9.14.2(transitive)
+ Addedvue-router@4.5.0(transitive)
- Removedvue@next
- Removedvue-router@next
- Removedvue-router@4.0.13(transitive)