Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-i18n-routing

Package Overview
Dependencies
Maintainers
1
Versions
239
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-i18n-routing - npm Package Compare versions

Comparing version 0.0.0-12d0627 to 0.0.0-15c1118

6

dist/vue-i18n-routing.cjs.js

@@ -1,1 +0,5 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";const n=function(e,u={}){console.log("install vue-i18n-rouging!")},o="0.0.0";exports.VERSION=o;exports.VueI18nRoutingPlugin=n;
"use strict";var W=Object.defineProperty,k=Object.defineProperties;var H=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var y=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var O=(e,t,a)=>t in e?W(e,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[t]=a,F=(e,t)=>{for(var a in t||(t={}))y.call(t,a)&&O(e,a,t[a]);if(b)for(var a of b(t))D.call(t,a)&&O(e,a,t[a]);return e},P=(e,t)=>k(e,H(t));var V=(e,t)=>{var a={};for(var r in e)y.call(e,r)&&t.indexOf(r)<0&&(a[r]=e[r]);if(e!=null&&b)for(var r of b(e))t.indexOf(r)<0&&D.call(e,r)&&(a[r]=e[r]);return a};Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";var m=require("vue-demi"),A=require("@intlify/vue-router-bridge"),Y=require("@intlify/vue-i18n-bridge");function J(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var K=J(A);/*!
* shared v9.2.0-beta.26
* (c) 2021 kazuya kawaguchi
* Released under the MIT License.
*/const U=Object.assign,T=e=>typeof e=="string",Q=e=>typeof e=="symbol";function Z(e){e=e||[];const t=[];for(const a of e)T(a)?t.push({code:a}):t.push(a);return t}function I(e,t){return m.isRef(e.locale)&&t==="composition"}function w(e){return"global"in e&&"mode"in e}function z(e){return w(e)?I(e.global,e.mode)?e.global.locale.value:e.global.locale:e.locale.value}function ee(e,t){w(e)?I(e.global,e.mode)?e.global.locale.value=t:e.global.locale=t:m.isRef(e.locale)&&(e.locale.value=t)}function te(e,t,a){return e.replace(/\/+$/,"")+(t?"/":"")||(a?"":"/")}function ae(e={}){const t=Y.useI18n(),a=A.useRouter(),r=A.useRoute(),R=e.defaultLocaleRouteNameSuffix||a.__defaultLocaleRouteNameSuffix,g=e.defaultLocale||a.__defaultLocale,_=e.routesNameSeparator||a.__routesNameSeparator,L=e.strategy||a.__strategy;function s(o){const c=o!=null?o:m.isRef(r)?r.value:r;return c.name?C(c.name).split(_)[0]:void 0}function h(o,c){let l=C(o)+(L==="no_prefix"?"":_+c);return c===g&&L==="prefix_and_default"&&(l+=_+R),l}function n(o,c){const l=c||z(t);let f=o;T(o)&&(o[0]==="/"?f={path:o}:f={name:o});let u=U({},f);if(u.path&&!u.name){const d=a.resolve(u),v=m.isVue2?d.route:d,E=s(v);T(E)&&(u={name:h(E,l),params:v.params,query:v.query,hash:v.hash})}else{u.name=h(u.name,l);const{params:d}=u;d&&d["0"]===void 0&&d.pathMatch&&(d["0"]=d.pathMatch)}const S=a.resolve(u);return(m.isVue2?S.route.name:S.name)?S:a.resolve(o)}function p(o,c){const l=n(o,c);return l==null?"":m.isVue2?l.route.redirectedFrom||l.route.fullPath:l.redirectedFrom||l.fullPath}function x(o,c){const l=n(o,c);return l==null?void 0:m.isVue2?l.route:l}function $(o,c){const l=n(o,c);return l==null?void 0:m.isVue2?l.location:l.href}function i(o){const c=s();if(!c)return"";const v=m.isVue2&&m.isRef(r)?r.value:r,{params:l}=v,f=V(v,["params"]),u={},S=U({},f,{name:c,params:P(F(F({},l),u),{0:l.pathMatch})}),d=p(S,o);return console.log("switchLocalePath",t.locale.value,o,d),d}return{getRouteBaseName:s,localePath:p,localeRoute:x,localeLocation:$,switchLocalePath:i}}function C(e){return T(e)?e:Q(e)?e.toString():"(null)"}const oe={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},X="",M=oe.PREFIX_EXCEPT_DEFAULT,j=!1,q="___",G="default";function B(e,{defaultLocale:t=X,strategy:a=M,trailingSlash:r=j,routesNameSeparator:R=q,defaultLocaleRouteNameSuffix:g=G,includeUprefixedFallback:_=!1,locales:L=[]}={}){if(a==="no_prefix")return e;const s=L.map(n=>T(n)?n:n.code);function h(n,p,x=!1,$=!1){return n.redirect&&(!n.component||!n.file)?[n]:p.reduce((o,c)=>{const{name:l}=n;let{path:f}=n;const u=F({},n);l&&(u.name=`${l}${R}${c}`),n.children&&(u.children=n.children.reduce((E,N)=>[...E,...h(N,[c],!0,$)],[]));const S=c===t;if(S&&a==="prefix_and_default")if(x)x&&$&&l&&(u.name+=`${R}${g}`);else{const E=P(F({},u),{path:f});if(l&&(E.name=`${u.name}${R}${g}`),n.children){E.children=[];for(const N of n.children)E.children=E.children.concat(h(N,[c],!0,!0))}o.push(E)}const d=x&&!f.startsWith("/"),v=!d&&!(S&&a==="prefix_except_default");return v&&(f=`/${c}${f}`),f&&(f=te(f,r,d)),v&&S&&a==="prefix"&&_&&o.push(F({},n)),u.path=f,o.push(u),o},[])}return e.reduce((n,p)=>[...n,...h(p,s||[])],[])}function le(e,{locales:t=[]}={}){if(!I(e.global,e.mode))throw new Error("TODO:");const a=m.ref(t);e.global.locales=m.computed(()=>a.value),console.log("... extends i18n done")}function ne(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function re(e,t,a){const r=`(${e.join("|")})`,R=`(?:${t}${a})?`,g=new RegExp(`${t}${r}${R}$`,"i"),_=ne(e);return s=>{if(s.name){const n=(T(s.name)?s.name:s.name.toString()).match(g);if(n&&n.length>1)return n[1]}else if(s.path){const h=s.path.match(_);if(h&&h.length>1)return h[1]}return""}}function ce(e){var t,a,r,R,g,_,L,s;return e.version=(t=e.version)!=null?t:4,e.defaultLocale=(a=e.defaultLocale)!=null?a:X,e.strategy=(r=e.strategy)!=null?r:M,e.trailingSlash=(R=e.trailingSlash)!=null?R:j,e.routesNameSeparator=(g=e.routesNameSeparator)!=null?g:q,e.defaultLocaleRouteNameSuffix=(_=e.defaultLocaleRouteNameSuffix)!=null?_:G,e.locales=(L=e.locales)!=null?L:[],e.routes=(s=e.routes)!=null?s:[],e}function ue(e,t={}){const{version:a,defaultLocale:r,locales:R,strategy:g,trailingSlash:_,routesNameSeparator:L,defaultLocaleRouteNameSuffix:s,routes:h}=ce(t),n=Z(R),p=n.map(o=>o.code),x=re(p,L,s);le(e,{locales:n});const $=B(h,{locales:R,defaultLocale:r,strategy:g,trailingSlash:_,routesNameSeparator:L,defaultLocaleRouteNameSuffix:s});t.routes=$;let i=null;if(m.isVue3&&a===4)i=A.createRouter(t);else if(m.isVue2&&a===3)i=new K.default(t);else throw new Error("TODO:");return i.__defaultLocale=r,i.__localeCodes=p,i.__strategy=g,i.__trailingSlash=_,i.__routesNameSeparator=L,i.__defaultLocaleRouteNameSuffix=s,i.beforeEach((o,c,l)=>{console.log("beforeEach",o,c);const f=z(e),u=x(o)||f||r||"";console.log("currentLocale",f,"finalLocale",u),f!==u&&ee(e,u),l()}),console.log("create router",i),i}const se="";exports.VERSION=se;exports.createRouter=ue;exports.localizeRoutes=B;exports.useI18nRouting=ae;

@@ -0,2 +1,98 @@

import type { I18n } from '@intlify/vue-i18n-bridge';
import { Locale } from '@intlify/vue-i18n-bridge';
import type { Route as Route_2 } from '@intlify/vue-router-bridge';
import type { RouteConfig } from '@intlify/vue-router-bridge';
import type { RouteLocationNormalizedLoaded } from '@intlify/vue-router-bridge';
import type { Router } from '@intlify/vue-router-bridge';
import type { RouterOptions } from '@intlify/vue-router-bridge';
import type { VueRouter } from '@intlify/vue-router-bridge';
/**
* Create a Vue Router instance
*
* @param i18n - A Vue I18n instance, see [Vue I18n API docs](https://vue-i18n.intlify.dev/api/general.html#i18n)
* @param options - An options, see {@link VueI18nRoutingOptions}
*
* @returns A Vue Router instance
*
* @remakrs
* You can create a vue router instance to be used by the Vue app.
*
* The routes of the created router instance are handled with i18n routing.
*
* At the Vue 2 will return a [Vue Router v3 instance](https://router.vuejs.org/api/#router-construction-options), and at the Vue 3 will return a [Vue Router v4 instance](https://next.router.vuejs.org/api/#createrouter).
*/
export declare function createRouter<Options extends VueI18nRoutingOptions = VueI18nRoutingOptions>(i18n: I18n, options?: Options): Options['version'] extends 4 ? Router : VueRouter;
/**
* Direction
*/
export declare type Directions = 'ltr' | 'rtl' | 'auto';
export declare interface I18nRouting {
getRouteBaseName(givenRoute?: Route_2 | RouteLocationNormalizedLoaded): string;
localePath(route: any, locale?: Locale): string;
localeRoute(route: any, locale?: Locale): any;
localeLocation(route: any, locale?: Locale): any;
switchLocalePath(locale: Locale): void;
}
export declare type I18nRoutingOptions = Pick<VueI18nRoutingOptions, 'defaultLocale' | 'strategy' | 'defaultLocaleRouteNameSuffix' | 'trailingSlash' | 'locales' | 'routesNameSeparator'>;
/**
* Locale object
*/
export declare interface LocaleObject extends Record<string, any> {
code: Locale;
name?: string;
dir?: Directions;
domain?: string;
file?: string;
isCatchallLocale?: boolean;
iso?: string;
}
export declare function localizeRoutes(routes: VueI18nRoute[], { defaultLocale, strategy, trailingSlash, routesNameSeparator, defaultLocaleRouteNameSuffix, includeUprefixedFallback, locales }?: Pick<VueI18nRoutingOptions, 'defaultLocale' | 'strategy' | 'locales' | 'routesNameSeparator' | 'trailingSlash' | 'defaultLocaleRouteNameSuffix'> & {
includeUprefixedFallback?: boolean;
}): VueI18nRoute[];
/**
* Route config for vue-router v4
*/
export declare interface Route {
name?: string;
path: string;
file?: string;
children?: Route[];
}
declare type _Route = UnionToIntersection<RouteConfig>;
/**
* Route config for lagacy vue-router v3
*/
export declare interface RouteLegacy extends Pick<_Route, Exclude<keyof _Route, 'children' | 'component'>> {
chunkName?: string;
chunkNames?: Record<string, string>;
component?: _Route['component'] | string;
children?: RouteLegacy[];
}
declare const STRATEGIES: {
readonly PREFIX: "prefix";
readonly PREFIX_EXCEPT_DEFAULT: "prefix_except_default";
readonly PREFIX_AND_DEFAULT: "prefix_and_default";
readonly NO_PREFIX: "no_prefix";
};
/**
* Routing strategy
*/
export declare type Strategies = typeof STRATEGIES[keyof typeof STRATEGIES];
declare type UnionToIntersection<T> = (T extends any ? (k: T) => void : never) extends (k: infer U) => void ? U : never;
export declare function useI18nRouting(options?: I18nRoutingOptions): I18nRouting;
/**
* Vue I18n Routing Version

@@ -7,10 +103,96 @@ *

*/
export declare const VERSION: string;
export declare const VERSION = "";
export declare interface VueI18nRoutingOptions {
routes: [];
}
/**
* Route config for vue-i18n-routing
*/
export declare type VueI18nRoute = Route & RouteLegacy & {
redirect?: string;
};
export declare const VueI18nRoutingPlugin: (VueOrApp: any, options?: any) => void;
/**
* Options to initialize a VueRouter instance
*
* @remarks
* This options is extended from Vue Router `RouterOptioins`, so you can specify those options.
*/
export declare type VueI18nRoutingOptions = {
/**
* Vue Router version
*
* @remarks
* You can choice between vue-router v3 and v4.
*
* If you specify `3`, this function return Vue Router v3 instance, else specify `4`, this function return Vue Router v4 instance.
*
* @defaultValue 4
*/
version?: 3 | 4;
/**
* The app's default locale
*
* @remarks
* When using `prefix_except_default` strategy, URLs for locale specified here won't have a prefix.
*
* It's recommended to set this to some locale regardless of chosen strategy, as it will be used as a fallback locale when navigating to a non-existent route
*
* @defaultValue '' (emputy string)
*/
defaultLocale?: string;
/**
* List of locales supported by your app
*
* @remarks TODO:
*
* @defaultValue []
*/
locales?: string[] | LocaleObject[];
/**
* Routes strategy
*
* @remarks
* Can be set to one of the following:
*
* - `no_prefix`: routes won't have a locale prefix
* - `prefix_except_default`: locale prefix added for every locale except default
* - `prefix`: locale prefix added for every locale
* - `prefix_and_default`: locale prefix added for every locale and default
*
* @defaultValue 'prefix_except_default'
*/
strategy?: Strategies;
/**
* Whether to use trailing slash
*
* @defaultValue false
*/
trailingSlash?: boolean;
/**
* Internal separator used for generated route names for each locale. You shouldn't need to change this
*
* @defaultValue '___'
*/
routesNameSeparator?: string;
/**
* Internal suffix added to generated route names for default locale
*
* @remarks
* if strategy is prefix_and_default. You shouldn't need to change this.
*
* @defaultValue 'default'
*/
defaultLocaleRouteNameSuffix?: string;
} & RouterOptions;
export { }
declare module 'vue-i18n' {
export interface ComposerCustom {
locales: ComputedRef<string[] | LocaleObject[]>
}
}
declare module 'vue-i18n-bridge' {
export interface ComposerCustom {
locales: ComputedRef<string[] | LocaleObject[]>
}
}

@@ -1,5 +0,349 @@

const VueI18nRoutingPlugin = function(VueOrApp, options = {}) {
console.log("install vue-i18n-rouging!");
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;
};
const VERSION = "0.0.0";
export { VERSION, VueI18nRoutingPlugin };
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 { isRef, isVue2, ref, computed, isVue3 } from "vue-demi";
import VueRouter3, { useRouter, useRoute, createRouter as createRouter$1 } from "@intlify/vue-router-bridge";
import { useI18n } from "@intlify/vue-i18n-bridge";
/*!
* shared v9.2.0-beta.26
* (c) 2021 kazuya kawaguchi
* Released under the MIT License.
*/
const assign = Object.assign;
const isString = (val) => typeof val === "string";
const isSymbol = (val) => typeof val === "symbol";
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 : i18n.locale.value;
}
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 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($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($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 {
getRouteBaseName,
localePath,
localeRoute,
localeLocation,
switchLocalePath
};
}
function getRouteName(routeName) {
return isString(routeName) ? routeName : isSymbol(routeName) ? routeName.toString() : "(null)";
}
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,
locales = []
} = {}) {
if (strategy === "no_prefix") {
return routes;
}
const _localeCodes = locales.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 extendI18n(i18n, { locales = [] } = {}) {
if (!isComposer(i18n.global, i18n.mode)) {
throw new Error("TODO:");
}
const _locales = ref(locales);
i18n.global.locales = computed(() => _locales.value);
console.log("... extends i18n done");
}
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 asDefaultVueI18nRouterOptions(options) {
var _a, _b, _c, _d, _e, _f, _g, _h;
options.version = (_a = options.version) != null ? _a : 4;
options.defaultLocale = (_b = options.defaultLocale) != null ? _b : DEFAULT_LOCALE;
options.strategy = (_c = options.strategy) != null ? _c : DEFAULT_STRATEGY;
options.trailingSlash = (_d = options.trailingSlash) != null ? _d : DEFAULT_TRAILING_SLASH;
options.routesNameSeparator = (_e = options.routesNameSeparator) != null ? _e : DEFAULT_ROUTES_NAME_SEPARATOR;
options.defaultLocaleRouteNameSuffix = (_f = options.defaultLocaleRouteNameSuffix) != null ? _f : DEFAULT_LOCALE_ROUTE_NAME_SUFFIX;
options.locales = (_g = options.locales) != null ? _g : [];
options.routes = (_h = options.routes) != null ? _h : [];
return options;
}
function createRouter(i18n, options = {}) {
const {
version,
defaultLocale,
locales,
strategy,
trailingSlash,
routesNameSeparator,
defaultLocaleRouteNameSuffix,
routes
} = asDefaultVueI18nRouterOptions(options);
const normalizedLocaleCodes = getNormalizedLocales(locales);
const localeCodes = normalizedLocaleCodes.map((l) => l.code);
const getLocaleFromRoute = createLocaleFromRouteGetter(localeCodes, routesNameSeparator, defaultLocaleRouteNameSuffix);
extendI18n(i18n, { locales: normalizedLocaleCodes });
const localizedRoutes = localizeRoutes(routes, {
locales,
defaultLocale,
strategy,
trailingSlash,
routesNameSeparator,
defaultLocaleRouteNameSuffix
});
options.routes = localizedRoutes;
let router = null;
if (isVue3 && version === 4) {
router = createRouter$1(options);
} else if (isVue2 && version === 3) {
router = new VueRouter3(options);
} else {
throw new Error("TODO:");
}
router.__defaultLocale = defaultLocale;
router.__localeCodes = localeCodes;
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();
});
console.log("create router", router);
return router;
}
const VERSION = "";
export { VERSION, createRouter, localizeRoutes, useI18nRouting };

@@ -1,1 +0,5 @@

(()=>{var t=function(n){"use strict";const u=function(o,e={}){console.log("install vue-i18n-rouging!")},i="0.0.0";return n.VERSION=i,n.VueI18nRoutingPlugin=u,Object.defineProperty(n,"__esModule",{value:!0}),n[Symbol.toStringTag]="Module",n}({});})();
var ue=Object.defineProperty,se=Object.defineProperties;var fe=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable;var k=(r,t,f)=>t in r?ue(r,t,{enumerable:!0,configurable:!0,writable:!0,value:f}):r[t]=f,P=(r,t)=>{for(var f in t||(t={}))D.call(t,f)&&k(r,f,t[f]);if(b)for(var f of b(t))W.call(t,f)&&k(r,f,t[f]);return r},v=(r,t)=>se(r,fe(t));var B=(r,t)=>{var f={};for(var p in r)D.call(r,p)&&t.indexOf(p)<0&&(f[p]=r[p]);if(r!=null&&b)for(var p of b(r))t.indexOf(p)<0&&W.call(r,p)&&(f[p]=r[p]);return f};var VueI18nRouting=function(r,t,f,p){"use strict";function H(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var Y=H(f);/*!
* shared v9.2.0-beta.26
* (c) 2021 kazuya kawaguchi
* Released under the MIT License.
*/const V=Object.assign,I=e=>typeof e=="string",J=e=>typeof e=="symbol";function K(e){e=e||[];const l=[];for(const c of e)I(c)?l.push({code:c}):l.push(c);return l}function y(e,l){return t.isRef(e.locale)&&l==="composition"}function U(e){return"global"in e&&"mode"in e}function w(e){return U(e)?y(e.global,e.mode)?e.global.locale.value:e.global.locale:e.locale.value}function Q(e,l){U(e)?y(e.global,e.mode)?e.global.locale.value=l:e.global.locale=l:t.isRef(e.locale)&&(e.locale.value=l)}function Z(e,l,c){return e.replace(/\/+$/,"")+(l?"/":"")||(c?"":"/")}function ee(e={}){const l=p.useI18n(),c=f.useRouter(),_=f.useRoute(),m=e.defaultLocaleRouteNameSuffix||c.__defaultLocaleRouteNameSuffix,E=e.defaultLocale||c.__defaultLocale,g=e.routesNameSeparator||c.__routesNameSeparator,S=e.strategy||c.__strategy;function i(a){const u=a!=null?a:t.isRef(_)?_.value:_;return u.name?z(u.name).split(g)[0]:void 0}function L(a,u){let o=z(a)+(S==="no_prefix"?"":g+u);return u===E&&S==="prefix_and_default"&&(o+=g+m),o}function n(a,u){const o=u||w(l);let d=a;I(a)&&(a[0]==="/"?d={path:a}:d={name:a});let s=V({},d);if(s.path&&!s.name){const R=c.resolve(s),$=t.isVue2?R.route:R,F=i($);I(F)&&(s={name:L(F,o),params:$.params,query:$.query,hash:$.hash})}else{s.name=L(s.name,o);const{params:R}=s;R&&R["0"]===void 0&&R.pathMatch&&(R["0"]=R.pathMatch)}const x=c.resolve(s);return(t.isVue2?x.route.name:x.name)?x:c.resolve(a)}function T(a,u){const o=n(a,u);return o==null?"":t.isVue2?o.route.redirectedFrom||o.route.fullPath:o.redirectedFrom||o.fullPath}function A(a,u){const o=n(a,u);return o==null?void 0:t.isVue2?o.route:o}function N(a,u){const o=n(a,u);return o==null?void 0:t.isVue2?o.location:o.href}function h(a){const u=i();if(!u)return"";const $=t.isVue2&&t.isRef(_)?_.value:_,{params:o}=$,d=B($,["params"]),s={},x=V({},d,{name:u,params:v(P(P({},o),s),{0:o.pathMatch})}),R=T(x,a);return console.log("switchLocalePath",l.locale.value,a,R),R}return{getRouteBaseName:i,localePath:T,localeRoute:A,localeLocation:N,switchLocalePath:h}}function z(e){return I(e)?e:J(e)?e.toString():"(null)"}const te={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},C="",X=te.PREFIX_EXCEPT_DEFAULT,M=!1,j="___",G="default";function q(e,{defaultLocale:l=C,strategy:c=X,trailingSlash:_=M,routesNameSeparator:m=j,defaultLocaleRouteNameSuffix:E=G,includeUprefixedFallback:g=!1,locales:S=[]}={}){if(c==="no_prefix")return e;const i=S.map(n=>I(n)?n:n.code);function L(n,T,A=!1,N=!1){return n.redirect&&(!n.component||!n.file)?[n]:T.reduce((a,u)=>{const{name:o}=n;let{path:d}=n;const s=P({},n);o&&(s.name=`${o}${m}${u}`),n.children&&(s.children=n.children.reduce((F,O)=>[...F,...L(O,[u],!0,N)],[]));const x=u===l;if(x&&c==="prefix_and_default")if(A)A&&N&&o&&(s.name+=`${m}${E}`);else{const F=v(P({},s),{path:d});if(o&&(F.name=`${s.name}${m}${E}`),n.children){F.children=[];for(const O of n.children)F.children=F.children.concat(L(O,[u],!0,!0))}a.push(F)}const R=A&&!d.startsWith("/"),$=!R&&!(x&&c==="prefix_except_default");return $&&(d=`/${u}${d}`),d&&(d=Z(d,_,R)),$&&x&&c==="prefix"&&g&&a.push(P({},n)),s.path=d,a.push(s),a},[])}return e.reduce((n,T)=>[...n,...L(T,i||[])],[])}function ae(e,{locales:l=[]}={}){if(!y(e.global,e.mode))throw new Error("TODO:");const c=t.ref(l);e.global.locales=t.computed(()=>c.value),console.log("... extends i18n done")}function oe(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function le(e,l,c){const _=`(${e.join("|")})`,m=`(?:${l}${c})?`,E=new RegExp(`${l}${_}${m}$`,"i"),g=oe(e);return i=>{if(i.name){const n=(I(i.name)?i.name:i.name.toString()).match(E);if(n&&n.length>1)return n[1]}else if(i.path){const L=i.path.match(g);if(L&&L.length>1)return L[1]}return""}}function ne(e){var l,c,_,m,E,g,S,i;return e.version=(l=e.version)!=null?l:4,e.defaultLocale=(c=e.defaultLocale)!=null?c:C,e.strategy=(_=e.strategy)!=null?_:X,e.trailingSlash=(m=e.trailingSlash)!=null?m:M,e.routesNameSeparator=(E=e.routesNameSeparator)!=null?E:j,e.defaultLocaleRouteNameSuffix=(g=e.defaultLocaleRouteNameSuffix)!=null?g:G,e.locales=(S=e.locales)!=null?S:[],e.routes=(i=e.routes)!=null?i:[],e}function ce(e,l={}){const{version:c,defaultLocale:_,locales:m,strategy:E,trailingSlash:g,routesNameSeparator:S,defaultLocaleRouteNameSuffix:i,routes:L}=ne(l),n=K(m),T=n.map(a=>a.code),A=le(T,S,i);ae(e,{locales:n});const N=q(L,{locales:m,defaultLocale:_,strategy:E,trailingSlash:g,routesNameSeparator:S,defaultLocaleRouteNameSuffix:i});l.routes=N;let h=null;if(t.isVue3&&c===4)h=f.createRouter(l);else if(t.isVue2&&c===3)h=new Y.default(l);else throw new Error("TODO:");return h.__defaultLocale=_,h.__localeCodes=T,h.__strategy=E,h.__trailingSlash=g,h.__routesNameSeparator=S,h.__defaultLocaleRouteNameSuffix=i,h.beforeEach((a,u,o)=>{console.log("beforeEach",a,u);const d=w(e),s=A(a)||d||_||"";console.log("currentLocale",d,"finalLocale",s),d!==s&&Q(e,s),o()}),console.log("create router",h),h}const re="";return r.VERSION=re,r.createRouter=ce,r.localizeRoutes=q,r.useI18nRouting=ee,Object.defineProperty(r,"__esModule",{value:!0}),r[Symbol.toStringTag]="Module",r}({},VueDemi,VueRouter,VueI18n);

30

package.json
{
"name": "vue-i18n-routing",
"description": "The i18n routing with using vue-i18n",
"version": "0.0.0-12d0627",
"version": "0.0.0-15c1118",
"dependencies": {
"vue-demi": "*"
"@intlify/shared": "beta",
"@intlify/vue-i18n-bridge": "^0.3.4",
"@intlify/vue-router-bridge": "^0.3.4",
"vue-demi": "^0.12.1"
},
"devDependencies": {
"vite": "^2.6.4",
"@microsoft/api-extractor": "^7.18.20",
"api-docs-gen": "^0.4.0",
"rimraf": "^3.0.2",
"typescript": "^4.5.4",
"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",
"vue2": "npm:vue@2",
"vue-router3": "npm:vue-router@latest",
"rimraf": "^3.0.2",
"@microsoft/api-extractor": "^7.18.20",
"api-docs-gen": "^0.4.0"
"vue2": "npm:vue@2"
},
"peerDependencies": {
"@vue/composition-api": "^1.4.0",
"vue": "^2.6.14 || ^3.2.0",
"vue-i18n": "^8.26.1 || ^9.2.0-beta.25",
"vue-i18n-bridge": "^9.2.0-beta.25",
"vue-router": "^3.5.3 || ^4.0.0"
},
"peerDependenciesMeta": {
"@vue/composition-api": {
"optional": true
}
},
"keywords": [

@@ -73,4 +88,5 @@ "i18n",

"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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc