vue-i18n-routing
Advanced tools
Comparing version 0.0.0-b5558e5 to 0.0.0-bc09a5f
@@ -1,5 +0,5 @@ | ||
"use strict";var H=Object.defineProperty,Y=Object.defineProperties;var J=Object.getOwnPropertyDescriptors;var A=Object.getOwnPropertySymbols;var N=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable;var C=(e,t,o)=>t in e?H(e,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[t]=o,F=(e,t)=>{for(var o in t||(t={}))N.call(t,o)&&C(e,o,t[o]);if(A)for(var o of A(t))X.call(t,o)&&C(e,o,t[o]);return e},T=(e,t)=>Y(e,J(t));var M=(e,t)=>{var o={};for(var r in e)N.call(e,r)&&t.indexOf(r)<0&&(o[r]=e[r]);if(e!=null&&A)for(var r of A(e))t.indexOf(r)<0&&X.call(e,r)&&(o[r]=e[r]);return o};Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";var E=require("vue-demi"),j=require("@intlify/vue-router-composable"),K=require("vue-i18n");/*! | ||
* shared v9.2.0-beta.25 | ||
* (c) 2021 kazuya kawaguchi | ||
"use strict";var Z=Object.defineProperty,ee=Object.defineProperties;var te=Object.getOwnPropertyDescriptors;var T=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var M=(e,t,a)=>t in e?Z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[t]=a,v=(e,t)=>{for(var a in t||(t={}))C.call(t,a)&&M(e,a,t[a]);if(T)for(var a of T(t))j.call(t,a)&&M(e,a,t[a]);return e},D=(e,t)=>ee(e,te(t));var q=(e,t)=>{var a={};for(var o in e)C.call(e,o)&&t.indexOf(o)<0&&(a[o]=e[o]);if(e!=null&&T)for(var o of T(e))t.indexOf(o)<0&&j.call(e,o)&&(a[o]=e[o]);return a};Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";var _=require("vue-demi"),x=require("@intlify/vue-router-bridge"),G=require("@intlify/vue-i18n-bridge");function ae(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var oe=ae(x);/*! | ||
* shared v9.2.0-beta.28 | ||
* (c) 2022 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/const q=Object.assign,P=e=>typeof e=="string",Q=e=>typeof e=="symbol",Z=e=>typeof e=="symbol";new Set(Object.getOwnPropertyNames(Symbol).map(e=>Symbol[e]).filter(Z));function I(e){return Boolean(e&&e.__v_isRef===!0)}Promise.resolve();function ee(e){e=e||[];const t=[];for(const o of e)P(o)?t.push({code:o}):t.push(o);return t}function y(e,t){return I(e.locale)&&t==="composition"}function G(e){return"global"in e&&"mode"in e}function O(e){return G(e)?y(e.global,e.mode)?e.global.locale.value:e.global.locale:I(e.locale)?e.locale.value:e.locale}function B(e,t){G(e)?y(e.global,e.mode)?e.global.locale.value=t:e.global.locale=t:I(e.locale)&&(e.locale.value=t)}function oe(e,t,o){return e.replace(/\/+$/,"")+(t?"/":"")||(o?"":"/")}function te(e,{localeCodes:t=[]}={}){if(!y(e.global,e.mode))throw new Error("");const o=E.ref(t);e.global.locales=E.computed(()=>o.value),console.log("... extends i18n done")}const ne={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},w="",W=ne.PREFIX_EXCEPT_DEFAULT,D=!1,z="___",U="default";function V(e,{defaultLocale:t=w,strategy:o=W,trailingSlash:r=D,routesNameSeparator:f=z,defaultLocaleRouteNameSuffix:_=U,includeUprefixedFallback:i=!1,localeCodes:$=[]}={}){if(o==="no_prefix")return e;const d=$.map(n=>P(n)?n:n.code);function m(n,p,L=!1,h=!1){return n.redirect&&(!n.component||!n.file)?[n]:p.reduce((l,s)=>{const{name:c}=n;let{path:u}=n;const a=F({},n);c&&(a.name=`${c}${f}${s}`),n.children&&(a.children=n.children.reduce((b,S)=>[...b,...m(S,[s],!0,h)],[]));const x=s===t;if(x&&o==="prefix_and_default")if(L)L&&h&&c&&(a.name+=`${f}${_}`);else{const b=T(F({},a),{path:u});if(c&&(b.name=`${a.name}${f}${_}`),n.children){b.children=[];for(const S of n.children)b.children=b.children.concat(m(S,[s],!0,!0))}l.push(b)}const R=L&&!u.startsWith("/"),v=!R&&!(x&&o==="prefix_except_default");return v&&(u=`/${s}${u}`),u&&(u=oe(u,r,R)),v&&x&&o==="prefix"&&i&&l.push(F({},n)),a.path=u,l.push(a),l},[])}return e.reduce((n,p)=>[...n,...m(p,d||[])],[])}function ce(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function le(e,t,o){const r=`(${e.join("|")})`,f=`(?:${t}${o})?`,_=new RegExp(`${t}${r}${f}$`,"i"),i=ce(e);return d=>{if(d.name){const n=(P(d.name)?d.name:d.name.toString()).match(_);if(n&&n.length>1)return n[1]}else if(d.path){const m=d.path.match(i);if(m&&m.length>1)return m[1]}return""}}function se({router:e,i18n:t,defaultLocale:o=w,strategy:r=W,trailingSlash:f=D,routesNameSeparator:_=z,defaultLocaleRouteNameSuffix:i=U,localeCodes:$=[]}={}){const d=ee($),m=le(d.map(n=>n.code),_,i);if(te(t,{localeCodes:d}),E.isVue2){const n=e,p=n.constructor,L=n.options.routes||[],h=V(L,{localeCodes:$,defaultLocale:o,strategy:r,trailingSlash:f,routesNameSeparator:_,defaultLocaleRouteNameSuffix:i});console.log("vue2 routes",L,h);const g=new p({mode:"history",base:n.options.base,routes:h});return g.__defaultLocale=o,g.__strategy=r,g.__trailingSlash=f,g.__routesNameSeparator=_,g.__defaultLocaleRouteNameSuffix=i,g.beforeEach((l,s,c)=>{console.log("beforeEach",l,s);const u=O(t),a=m(l)||u||o||"";console.log("currentLocale",u,"finalLocale",a),u!==a&&B(t,a),c()}),g}else{const n=e,p=n.options.routes||[],L=V(p,{localeCodes:$,defaultLocale:o,strategy:r,trailingSlash:f,routesNameSeparator:_,defaultLocaleRouteNameSuffix:i});return console.log("vue3 routes",p,L,n),p.forEach(h=>n.removeRoute(h.name)),L.forEach(h=>n.addRoute(h)),n.__defaultLocale=o,n.__strategy=r,n.__trailingSlash=f,n.__routesNameSeparator=_,n.__defaultLocaleRouteNameSuffix=i,n.beforeEach((h,g,l)=>{console.log("beforeEach",h,g);const s=O(t),c=m(h)||s||o||"";console.log("currentLocale",s,"finalLocale",c),s!==c&&B(t,c),l()}),n}}function re(){return K.useI18n()}function ae(e={}){const t=re(),o=j.useRouter(),r=j.useRoute(),f=e.defaultLocaleRouteNameSuffix||o.__defaultLocaleRouteNameSuffix,_=e.defaultLocale||o.__defaultLocale,i=e.routesNameSeparator||o.__routesNameSeparator,$=e.strategy||o.__strategy;function d(l){const s=l!=null?l:E.isRef(r)?r.value:r;return s.name?k(s.name).split(i)[0]:void 0}function m(l,s){let c=k(l)+($==="no_prefix"?"":i+s);return s===_&&$==="prefix_and_default"&&(c+=i+f),c}function n(l,s){const c=s||O(t);let u=l;P(l)&&(l[0]==="/"?u={path:l}:u={name:l});let a=q({},u);if(a.path&&!a.name){const R=o.resolve(a),v=E.isVue2?R.route:R,b=d(v);P(b)&&(a={name:m(b,c),params:v.params,query:v.query,hash:v.hash})}else{a.name=m(a.name,c);const{params:R}=a;R&&R["0"]===void 0&&R.pathMatch&&(R["0"]=R.pathMatch)}const x=o.resolve(a);return(E.isVue2?x.route.name:x.name)?x:o.resolve(l)}function p(l,s){const c=n(l,s);return c==null?"":E.isVue2?c.route.redirectedFrom||c.route.fullPath:c.redirectedFrom||c.fullPath}function L(l,s){const c=n(l,s);return c==null?void 0:E.isVue2?c.route:c}function h(l,s){const c=n(l,s);return c==null?void 0:E.isVue2?c.location:c.href}function g(l){const s=d();if(!s)return"";const v=E.isVue2&&E.isRef(r)?r.value:r,{params:c}=v,u=M(v,["params"]),a={},x=q({},u,{name:s,params:T(F(F({},c),a),{0:c.pathMatch})}),R=p(x,l);return console.log("switchLocalePath",t.locale.value,l,R),R}return{localePath:p,localeRoute:L,localeLocation:h,switchLocalePath:g}}function k(e){return P(e)?e:Q(e)?e.toString():""}function ue({router:e,i18n:t,defaultLocale:o=w,trailingSlash:r=D,routesNameSeparator:f=z,defaultLocaleRouteNameSuffix:_=U,localeCodes:i=[]}={}){if(e==null)throw new Error("TODO");return se({router:e,i18n:t,defaultLocale:o,trailingSlash:r,routesNameSeparator:f,defaultLocaleRouteNameSuffix:_,localeCodes:i})}const ie="";exports.VERSION=ie;exports.extendRouting=ue;exports.localizeRoutes=V;exports.useI18nRouting=ae; | ||
*/const W=Object.assign,F=e=>typeof e=="string",le=e=>typeof e=="symbol",ne=/\/$|\/\?/;function b(e="",t=!1){return t?ne.test(e):e.endsWith("/")}function re(e="",t=!1){if(!t)return(b(e)?e.slice(0,-1):e)||"/";if(!b(e,!0))return e||"/";const[a,...o]=e.split("?");return(a.slice(0,-1)||"/")+(o.length?`?${o.join("?")}`:"")}function ce(e="",t=!1){if(!t)return e.endsWith("/")?e:e+"/";if(b(e,!0))return e||"/";const[a,...o]=e.split("?");return a+"/"+(o.length?`?${o.join("?")}`:"")}function se(e){e=e||[];const t=[];for(const a of e)F(a)?t.push({code:a}):t.push(a);return t}function y(e,t){return _.isRef(e.locale)&&t==="composition"}function B(e){return"global"in e&&"mode"in e}function H(e){return B(e)?y(e.global,e.mode)?e.global.locale.value:e.global.locale:e.locale.value}function ue(e,t){B(e)?y(e.global,e.mode)?e.global.locale.value=t:e.global.locale=t:_.isRef(e.locale)&&(e.locale.value=t)}function ie(e,t,a){return e.replace(/\/+$/,"")+(t?"/":"")||(a?"":"/")}function k(e){return F(e)?e:le(e)?e.toString():"(null)"}function Y(e,t,{defaultLocale:a,strategy:o,routesNameSeparator:r,defaultLocaleRouteNameSuffix:s}){let u=k(e)+(o==="no_prefix"?"":r+t);return t===a&&o==="prefix_and_default"&&(u+=r+s),u}const N={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},O="",V=N.PREFIX_EXCEPT_DEFAULT,J=!1,A="___",w="default";function U(e=x.useRoute(),t=x.useRouter().__routesNameSeparator||A){const a=_.unref(e);return a.name?k(a.name).split(t)[0]:null}const fe=new Set([N.PREFIX_AND_DEFAULT,N.PREFIX_EXCEPT_DEFAULT]);function K(e,t,a){const o=X(e,t,a);return o==null?"":_.isVue3?o.redirectedFrom||o.fullPath:o.route.redirectedFrom||o.route.fullPath}function de(e,t,a){const o=X(e,t,a);return o==null?void 0:_.isVue3?o:o.route}function he(e,t,a){const o=X(e,t,a);return o==null?void 0:_.isVue3?o:o.location}function X(e,t,{router:a=x.useRouter(),i18n:o=G.useI18n(),defaultLocale:r=O,defaultLocaleRouteNameSuffix:s=w,routesNameSeparator:u=A,strategy:m=V,trailingSlash:c=!1}={}){const i=a.__defaultLocaleRouteNameSuffix||s,l=a.__defaultLocale||r,E=a.__routesNameSeparator||u,L=a.__strategy||m,S=t||H(o);let f=e;F(e)&&(f[0]==="/"?f={path:e}:f={name:e});let n=W({},f);if(n.path&&!n.name){const h=a.resolve(n),d=_.isVue3?h:h.route,R=U(d);F(R)?n={name:Y(R,S,{defaultLocale:l,strategy:L,routesNameSeparator:E,defaultLocaleRouteNameSuffix:i}),params:d.params,query:d.query,hash:d.hash}:(!(S===r&&fe.has(L))&&L!==N.NO_PREFIX&&(n.path=`/${S}${n.path}`),n.path=c?ce(n.path,!0):re(n.path,!0))}else{n.name=Y(n.name,S,{defaultLocale:l,strategy:L,routesNameSeparator:E,defaultLocaleRouteNameSuffix:i});const{params:h}=n;h&&h["0"]===void 0&&h.pathMatch&&(h["0"]=h.pathMatch)}const g=a.resolve(n);return(_.isVue3?g.name:g.route.name)?g:a.resolve(e)}function _e(e,{route:t=x.useRoute(),i18n:a=G.useI18n()}={}){const o=U();if(!o)return"";const i=!_.isVue3&&_.isRef(t)?t.value:t,{params:r}=i,s=q(i,["params"]),u={},m=W({},s,{name:o,params:D(v(v({},r),u),{0:r.pathMatch})});return K(m,e,{route:t,i18n:a})}function Q(e,{defaultLocale:t=O,strategy:a=V,trailingSlash:o=J,routesNameSeparator:r=A,defaultLocaleRouteNameSuffix:s=w,includeUprefixedFallback:u=!1,locales:m=[]}={}){if(a==="no_prefix")return e;const c=m.map(l=>F(l)?l:l.code);function i(l,E,L=!1,S=!1){return l.redirect&&(!l.component||!l.file)?[l]:E.reduce((n,g)=>{const{name:h}=l;let{path:d}=l;const R=v({},l);h&&(R.name=`${h}${r}${g}`),l.children&&(R.children=l.children.reduce((p,I)=>[...p,...i(I,[g],!0,S)],[]));const P=g===t;if(P&&a==="prefix_and_default")if(L)L&&S&&h&&(R.name+=`${r}${s}`);else{const p=D(v({},R),{path:d});if(h&&(p.name=`${R.name}${r}${s}`),l.children){p.children=[];for(const I of l.children)p.children=p.children.concat(i(I,[g],!0,!0))}n.push(p)}const $=L&&!d.startsWith("/"),z=!$&&!(P&&a==="prefix_except_default");return z&&(d=`/${g}${d}`),d&&(d=ie(d,o,$)),z&&P&&a==="prefix"&&u&&n.push(v({},l)),R.path=d,n.push(R),n},[])}return e.reduce((l,E)=>[...l,...i(E,c||[])],[])}function me(e,{locales:t=[]}={}){if(!y(e.global,e.mode))throw new Error("TODO:");const a=_.ref(t);e.global.locales=_.computed(()=>a.value)}function Re(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function Le(e,t,a){const o=`(${e.join("|")})`,r=`(?:${t}${a})?`,s=new RegExp(`${t}${o}${r}$`,"i"),u=Re(e);return c=>{if(c.name){const l=(F(c.name)?c.name:c.name.toString()).match(s);if(l&&l.length>1)return l[1]}else if(c.path){const i=c.path.match(u);if(i&&i.length>1)return i[1]}return""}}function ge(e){var t,a,o,r,s,u,m,c;return e.version=(t=e.version)!=null?t:4,e.defaultLocale=(a=e.defaultLocale)!=null?a:O,e.strategy=(o=e.strategy)!=null?o:V,e.trailingSlash=(r=e.trailingSlash)!=null?r:J,e.routesNameSeparator=(s=e.routesNameSeparator)!=null?s:A,e.defaultLocaleRouteNameSuffix=(u=e.defaultLocaleRouteNameSuffix)!=null?u:w,e.locales=(m=e.locales)!=null?m:[],e.routes=(c=e.routes)!=null?c:[],e}function Ee(e,t={}){const{version:a,defaultLocale:o,locales:r,strategy:s,trailingSlash:u,routesNameSeparator:m,defaultLocaleRouteNameSuffix:c,routes:i}=ge(t),l=se(r),E=l.map(n=>n.code),L=Le(E,m,c);me(e,{locales:l});const S=Q(i,{locales:r,defaultLocale:o,strategy:s,trailingSlash:u,routesNameSeparator:m,defaultLocaleRouteNameSuffix:c});t.routes=S;let f=null;if(_.isVue3&&a===4)f=x.createRouter(t);else if(_.isVue2&&a===3)f=new oe.default(t);else throw new Error("TODO:");return f.__defaultLocale=o,f.__localeCodes=E,f.__strategy=s,f.__trailingSlash=u,f.__routesNameSeparator=m,f.__defaultLocaleRouteNameSuffix=c,f.beforeEach((n,g,h)=>{const d=H(e),R=L(n)||d||o||"";d!==R&&ue(e,R),h()}),f}const Se="0.0.0";exports.VERSION=Se;exports.createRouter=Ee;exports.getRouteBaseName=U;exports.localeLocation=he;exports.localePath=K;exports.localeRoute=de;exports.localizeRoutes=Q;exports.switchLocalePath=_e; |
@@ -1,11 +0,34 @@ | ||
import type { I18n } from 'vue-i18n'; | ||
import { Locale } from 'vue-i18n'; | ||
import type { Route as Route_2 } from 'vue-router3'; | ||
import type { RouteConfig } from 'vue-router3'; | ||
import type { RouteLocationNormalizedLoaded } from 'vue-router'; | ||
import type { Router } from 'vue-router'; | ||
import VueI18n from 'vue-i18n-legacy'; | ||
import VueRouter from 'vue-router3'; | ||
import { Composer } from '@intlify/vue-i18n-bridge'; | ||
import type { I18n } from '@intlify/vue-i18n-bridge'; | ||
import type { Locale } from '@intlify/vue-i18n-bridge'; | ||
import type { RawLocation } from '@intlify/vue-router-bridge'; | ||
import type { Route as Route_2 } from '@intlify/vue-router-bridge'; | ||
import type { RouteConfig } from '@intlify/vue-router-bridge'; | ||
import type { RouteLocation } from '@intlify/vue-router-bridge'; | ||
import type { RouteLocationNormalizedLoaded } from '@intlify/vue-router-bridge'; | ||
import type { RouteLocationRaw } from '@intlify/vue-router-bridge'; | ||
import type { Router } from '@intlify/vue-router-bridge'; | ||
import type { RouterOptions } from '@intlify/vue-router-bridge'; | ||
import { useRoute } from '@intlify/vue-router-bridge'; | ||
import { useRouter } 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 | ||
@@ -15,15 +38,29 @@ */ | ||
export declare function extendRouting<TRouter extends VueRouter | Router>({ router, i18n, defaultLocale, trailingSlash, routesNameSeparator, defaultLocaleRouteNameSuffix, localeCodes }?: VueI18nRoutingOptions): TRouter; | ||
/** | ||
* Get route base name | ||
* | ||
* @param givenRoute - A route object, if not provided, the route is returned with `useRoute` will be used | ||
* @param routesNameSeparator - A route name separator, if not provided, default separator is `routesNameSeparator` option of {@link VueI18nRoutingOptions} will be used | ||
* | ||
* @returns The route base name, if route name is not defined, return null | ||
*/ | ||
export declare function getRouteBaseName(givenRoute?: Route_2 | RouteLocationNormalizedLoaded, routesNameSeparator?: string): string | null; | ||
export declare type I18nRoutingOptions = Pick<VueI18nRoutingOptions, 'defaultLocale' | 'strategy' | 'defaultLocaleRouteNameSuffix' | 'trailingSlash' | 'localeCodes'> & { | ||
routesNameSeparator?: string; | ||
declare type I18nRoutingOptions = Pick<VueI18nRoutingOptions, 'defaultLocale' | 'strategy' | 'defaultLocaleRouteNameSuffix' | 'trailingSlash' | 'locales' | 'routesNameSeparator'> & { | ||
route?: ReturnType<typeof useRoute>; | ||
router?: ReturnType<typeof useRouter>; | ||
i18n?: Composer; | ||
}; | ||
export declare interface I18nRoutingReturn { | ||
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; | ||
} | ||
/** | ||
* Resolve locale location | ||
* | ||
* @param route - A route location. The path or name of the route or an object for more complex routes | ||
* @param locale - A locale code, if not specified, uses the current locale | ||
* @param options - An options, see about details {@link I18nRoutingOptions} | ||
* | ||
* @returns Returns the location object for a given route, the location object is resolved by vue-router rather than just a full route path. | ||
*/ | ||
export declare function localeLocation(route: RawLocation | RouteLocationRaw, locale?: Locale, // TODO: locale should be more type inference (completion) | ||
options?: I18nRoutingOptions): Location | RouteLocation | undefined; | ||
@@ -43,3 +80,27 @@ /** | ||
export declare function localizeRoutes(routes: VueI18nRoute[], { defaultLocale, strategy, trailingSlash, routesNameSeparator, defaultLocaleRouteNameSuffix, includeUprefixedFallback, localeCodes }?: Pick<VueI18nRoutingOptions, 'defaultLocale' | 'strategy' | 'localeCodes' | 'routesNameSeparator' | 'trailingSlash' | 'defaultLocaleRouteNameSuffix'> & { | ||
/** | ||
* Resolve locale path | ||
* | ||
* @param route - A route location. The path or name of the route or an object for more complex routes | ||
* @param locale - A locale code, if not specified, uses the current locale | ||
* @param options - An options, see about details {@link I18nRoutingOptions} | ||
* | ||
* @returns Returns the localized URL for a given route | ||
*/ | ||
export declare function localePath(route: RawLocation | RouteLocationRaw, locale?: Locale, // TODO: locale should be more type inference (completion) | ||
options?: I18nRoutingOptions): string; | ||
/** | ||
* Resolve locale route | ||
* | ||
* @param route - A route location. The path or name of the route or an object for more complex routes | ||
* @param locale - A locale code, if not specified, uses the current locale | ||
* @param options - An options, see about details {@link I18nRoutingOptions} | ||
* | ||
* @returns Returns the route object for a given route, the route object is resolved by vue-router rather than just a full route path. | ||
*/ | ||
export declare function localeRoute(route: RawLocation | RouteLocationRaw, locale?: Locale, // TODO: locale should be more type inference (completion) | ||
options?: I18nRoutingOptions): Route_2 | ReturnType<Router['resolve']> | undefined; | ||
export declare function localizeRoutes(routes: VueI18nRoute[], { defaultLocale, strategy, trailingSlash, routesNameSeparator, defaultLocaleRouteNameSuffix, includeUprefixedFallback, locales }?: Pick<VueI18nRoutingOptions, 'defaultLocale' | 'strategy' | 'locales' | 'routesNameSeparator' | 'trailingSlash' | 'defaultLocaleRouteNameSuffix'> & { | ||
includeUprefixedFallback?: boolean; | ||
@@ -82,6 +143,14 @@ }): VueI18nRoute[]; | ||
/** | ||
* Switch locale path | ||
* | ||
* @param locale - A locale code, if not specified, uses the current locale | ||
* @param options - An options, see about details {@link I18nRoutingOptions} | ||
* | ||
* @returns Returns a link to the current route in another language | ||
*/ | ||
export declare function switchLocalePath(locale: Locale, { route, i18n }?: I18nRoutingOptions): string; | ||
declare type UnionToIntersection<T> = (T extends any ? (k: T) => void : never) extends (k: infer U) => void ? U : never; | ||
export declare function useI18nRouting<Legacy extends boolean = false>(options?: I18nRoutingOptions): I18nRoutingReturn; | ||
/** | ||
@@ -93,3 +162,3 @@ * Vue I18n Routing Version | ||
*/ | ||
export declare const VERSION = ""; | ||
export declare const VERSION: string; | ||
@@ -104,28 +173,86 @@ /** | ||
/** | ||
* Vue I18n routing options | ||
* Options to initialize a VueRouter instance | ||
* | ||
* @remarks | ||
* This options is extended from Vue Router `RouterOptioins`, so you can specify those options. | ||
*/ | ||
export declare interface VueI18nRoutingOptions { | ||
export declare type VueI18nRoutingOptions = { | ||
/** | ||
* Vue Router instance | ||
* 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 | ||
*/ | ||
router?: VueRouter | Router; | ||
version?: 3 | 4; | ||
/** | ||
* Vue I18n instance | ||
* 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) | ||
*/ | ||
i18n?: VueI18n | I18n; | ||
defaultLocale?: string; | ||
localeCodes?: string[] | LocaleObject[]; | ||
/** | ||
* 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 { } | ||
import type { ComputedRef } from 'vue' | ||
declare module 'vue-i18n' { | ||
export interface ComposerCustom { | ||
locales: ComputedRef<string[] | LocaleObject[]> | ||
locales: ComputedRef<LocaleObject[]> | ||
} | ||
} | ||
declare module 'vue-i18n-bridge' { | ||
export interface ComposerCustom { | ||
locales: ComputedRef<LocaleObject[]> | ||
} | ||
} |
@@ -32,8 +32,8 @@ var __defProp = Object.defineProperty; | ||
}; | ||
import { ref, computed, isVue2, isRef as isRef$1 } from "vue-demi"; | ||
import { useRouter, useRoute } from "@intlify/vue-router-composable"; | ||
import { useI18n as useI18n$1 } from "vue-i18n"; | ||
import { isRef, unref, isVue3, ref, computed, isVue2 } from "vue-demi"; | ||
import VueRouter3, { useRoute, useRouter, createRouter as createRouter$1 } from "@intlify/vue-router-bridge"; | ||
import { useI18n } from "@intlify/vue-i18n-bridge"; | ||
/*! | ||
* shared v9.2.0-beta.25 | ||
* (c) 2021 kazuya kawaguchi | ||
* shared v9.2.0-beta.28 | ||
* (c) 2022 kazuya kawaguchi | ||
* Released under the MIT License. | ||
@@ -43,9 +43,30 @@ */ | ||
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); | ||
const TRAILING_SLASH_RE = /\/$|\/\?/; | ||
function hasTrailingSlash(input = "", queryParams = false) { | ||
if (!queryParams) { | ||
return input.endsWith("/"); | ||
} | ||
return TRAILING_SLASH_RE.test(input); | ||
} | ||
Promise.resolve(); | ||
function withoutTrailingSlash(input = "", queryParams = false) { | ||
if (!queryParams) { | ||
return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || "/"; | ||
} | ||
if (!hasTrailingSlash(input, true)) { | ||
return input || "/"; | ||
} | ||
const [s0, ...s] = input.split("?"); | ||
return (s0.slice(0, -1) || "/") + (s.length ? `?${s.join("?")}` : ""); | ||
} | ||
function withTrailingSlash(input = "", queryParams = false) { | ||
if (!queryParams) { | ||
return input.endsWith("/") ? input : input + "/"; | ||
} | ||
if (hasTrailingSlash(input, true)) { | ||
return input || "/"; | ||
} | ||
const [s0, ...s] = input.split("?"); | ||
return s0 + "/" + (s.length ? `?${s.join("?")}` : ""); | ||
} | ||
function getNormalizedLocales(locales) { | ||
@@ -70,3 +91,3 @@ locales = locales || []; | ||
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; | ||
return isI18nInstance(i18n) ? isComposer(i18n.global, i18n.mode) ? i18n.global.locale.value : i18n.global.locale : i18n.locale.value; | ||
} | ||
@@ -87,9 +108,16 @@ function setLocale(i18n, locale) { | ||
} | ||
function extendI18n(i18n, { localeCodes = [] } = {}) { | ||
if (!isComposer(i18n.global, i18n.mode)) { | ||
throw new Error(""); | ||
function getRouteName(routeName) { | ||
return isString(routeName) ? routeName : isSymbol(routeName) ? routeName.toString() : "(null)"; | ||
} | ||
function getLocaleRouteName(routeName, locale, { | ||
defaultLocale, | ||
strategy, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix | ||
}) { | ||
let name = getRouteName(routeName) + (strategy === "no_prefix" ? "" : routesNameSeparator + locale); | ||
if (locale === defaultLocale && strategy === "prefix_and_default") { | ||
name += routesNameSeparator + defaultLocaleRouteNameSuffix; | ||
} | ||
const _localeCodes = ref(localeCodes); | ||
i18n.global.locales = computed(() => _localeCodes.value); | ||
console.log("... extends i18n done"); | ||
return name; | ||
} | ||
@@ -107,2 +135,104 @@ const STRATEGIES = { | ||
const DEFAULT_LOCALE_ROUTE_NAME_SUFFIX = "default"; | ||
function getRouteBaseName(givenRoute = useRoute(), routesNameSeparator = useRouter().__routesNameSeparator || DEFAULT_ROUTES_NAME_SEPARATOR) { | ||
const _route = unref(givenRoute); | ||
if (!_route.name) { | ||
return null; | ||
} | ||
const name = getRouteName(_route.name); | ||
return name.split(routesNameSeparator)[0]; | ||
} | ||
const RESOLVED_PREFIXED = new Set([STRATEGIES.PREFIX_AND_DEFAULT, STRATEGIES.PREFIX_EXCEPT_DEFAULT]); | ||
function localePath(route, locale, options) { | ||
const localizedRoute = resolveRoute(route, locale, options); | ||
return localizedRoute == null ? "" : isVue3 ? localizedRoute.redirectedFrom || localizedRoute.fullPath : localizedRoute.route.redirectedFrom || localizedRoute.route.fullPath; | ||
} | ||
function localeRoute(route, locale, options) { | ||
const resolved = resolveRoute(route, locale, options); | ||
return resolved == null ? void 0 : isVue3 ? resolved : resolved.route; | ||
} | ||
function localeLocation(route, locale, options) { | ||
const resolved = resolveRoute(route, locale, options); | ||
return resolved == null ? void 0 : isVue3 ? resolved : resolved.location; | ||
} | ||
function resolveRoute(route, locale, { | ||
router = useRouter(), | ||
i18n = useI18n(), | ||
defaultLocale = DEFAULT_LOCALE, | ||
defaultLocaleRouteNameSuffix = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX, | ||
routesNameSeparator = DEFAULT_ROUTES_NAME_SEPARATOR, | ||
strategy = DEFAULT_STRATEGY, | ||
trailingSlash = false | ||
} = {}) { | ||
const _defaultLocaleRouteNameSuffix = router.__defaultLocaleRouteNameSuffix || defaultLocaleRouteNameSuffix; | ||
const _defaultLocale = router.__defaultLocale || defaultLocale; | ||
const _routesNameSeparator = router.__routesNameSeparator || routesNameSeparator; | ||
const _strategy = router.__strategy || strategy; | ||
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 = isVue3 ? _resolvedRoute : _resolvedRoute.route; | ||
const resolvedRouteName = getRouteBaseName(resolvedRoute2); | ||
if (isString(resolvedRouteName)) { | ||
localizedRoute = { | ||
name: getLocaleRouteName(resolvedRouteName, _locale, { | ||
defaultLocale: _defaultLocale, | ||
strategy: _strategy, | ||
routesNameSeparator: _routesNameSeparator, | ||
defaultLocaleRouteNameSuffix: _defaultLocaleRouteNameSuffix | ||
}), | ||
params: resolvedRoute2.params, | ||
query: resolvedRoute2.query, | ||
hash: resolvedRoute2.hash | ||
}; | ||
} else { | ||
const isDefaultLocale = _locale === defaultLocale; | ||
const isPrefixed = !(isDefaultLocale && RESOLVED_PREFIXED.has(_strategy)) && !(_strategy === STRATEGIES.NO_PREFIX); | ||
if (isPrefixed) { | ||
localizedRoute.path = `/${_locale}${localizedRoute.path}`; | ||
} | ||
localizedRoute.path = trailingSlash ? withTrailingSlash(localizedRoute.path, true) : withoutTrailingSlash(localizedRoute.path, true); | ||
} | ||
} else { | ||
localizedRoute.name = getLocaleRouteName(localizedRoute.name, _locale, { | ||
defaultLocale: _defaultLocale, | ||
strategy: _strategy, | ||
routesNameSeparator: _routesNameSeparator, | ||
defaultLocaleRouteNameSuffix: _defaultLocaleRouteNameSuffix | ||
}); | ||
const { params } = localizedRoute; | ||
if (params && params["0"] === void 0 && params.pathMatch) { | ||
params["0"] = params.pathMatch; | ||
} | ||
} | ||
const resolvedRoute = router.resolve(localizedRoute); | ||
if (isVue3 ? resolvedRoute.name : resolvedRoute.route.name) { | ||
return resolvedRoute; | ||
} | ||
return router.resolve(route); | ||
} | ||
function switchLocalePath(locale, { route = useRoute(), i18n = useI18n() } = {}) { | ||
const name = getRouteBaseName(); | ||
if (!name) { | ||
return ""; | ||
} | ||
const _a = !isVue3 && 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, { route, i18n }); | ||
return path; | ||
} | ||
function localizeRoutes(routes, { | ||
@@ -115,3 +245,3 @@ defaultLocale = DEFAULT_LOCALE, | ||
includeUprefixedFallback = false, | ||
localeCodes = [] | ||
locales = [] | ||
} = {}) { | ||
@@ -121,3 +251,3 @@ if (strategy === "no_prefix") { | ||
} | ||
const _localeCodes = localeCodes.map((locale) => isString(locale) ? locale : locale.code); | ||
const _localeCodes = locales.map((locale) => isString(locale) ? locale : locale.code); | ||
function makeLocalizedRoutes(route, allowedLocaleCodes, isChild = false, isExtraPageTree = false) { | ||
@@ -174,2 +304,9 @@ if (route.redirect && (!route.component || !route.file)) { | ||
} | ||
function extendI18n(i18n, { locales = [] } = {}) { | ||
if (!isComposer(i18n.global, i18n.mode)) { | ||
throw new Error("TODO:"); | ||
} | ||
const _locales = ref(locales); | ||
i18n.global.locales = computed(() => _locales.value); | ||
} | ||
function getLocalesRegex(localeCodes) { | ||
@@ -200,205 +337,63 @@ return new RegExp(`^/(${localeCodes.join("|")})(?:/|$)`, "i"); | ||
} | ||
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 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 useI18n() { | ||
return useI18n$1(); | ||
} | ||
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, | ||
function createRouter(i18n, options = {}) { | ||
const { | ||
version, | ||
defaultLocale, | ||
locales, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
localeCodes | ||
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) => { | ||
const currentLocale = getLocale(i18n); | ||
const finalLocale = getLocaleFromRoute(to) || currentLocale || defaultLocale || ""; | ||
if (currentLocale !== finalLocale) { | ||
setLocale(i18n, finalLocale); | ||
} | ||
next(); | ||
}); | ||
return router; | ||
} | ||
const VERSION = ""; | ||
export { VERSION, extendRouting, localizeRoutes, useI18nRouting }; | ||
const VERSION = "0.0.0"; | ||
export { VERSION, createRouter, getRouteBaseName, localeLocation, localePath, localeRoute, localizeRoutes, switchLocalePath }; |
@@ -1,5 +0,5 @@ | ||
var ue=Object.defineProperty,ie=Object.defineProperties;var fe=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var W=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var H=(r,s,f)=>s in r?ue(r,s,{enumerable:!0,configurable:!0,writable:!0,value:f}):r[s]=f,v=(r,s)=>{for(var f in s||(s={}))W.call(s,f)&&H(r,f,s[f]);if(x)for(var f of x(s))k.call(s,f)&&H(r,f,s[f]);return r},j=(r,s)=>ie(r,fe(s));var Y=(r,s)=>{var f={};for(var $ in r)W.call(r,$)&&s.indexOf($)<0&&(f[$]=r[$]);if(r!=null&&x)for(var $ of x(r))s.indexOf($)<0&&k.call(r,$)&&(f[$]=r[$]);return f};var VueI18nRouting=function(r,s,f,$){"use strict";/*! | ||
* shared v9.2.0-beta.25 | ||
* (c) 2021 kazuya kawaguchi | ||
var Ee=Object.defineProperty,Se=Object.defineProperties;var Fe=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var J=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var Q=(o,n,s)=>n in o?Ee(o,n,{enumerable:!0,configurable:!0,writable:!0,value:s}):o[n]=s,$=(o,n)=>{for(var s in n||(n={}))J.call(n,s)&&Q(o,s,n[s]);if(b)for(var s of b(n))K.call(n,s)&&Q(o,s,n[s]);return o},D=(o,n)=>Se(o,Fe(n));var Z=(o,n)=>{var s={};for(var E in o)J.call(o,E)&&n.indexOf(E)<0&&(s[E]=o[E]);if(o!=null&&b)for(var E of b(o))n.indexOf(E)<0&&K.call(o,E)&&(s[E]=o[E]);return s};var VueI18nRouting=function(o,n,s,E){"use strict";function ee(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var te=ee(s);/*! | ||
* shared v9.2.0-beta.28 | ||
* (c) 2022 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/const C=Object.assign,T=e=>typeof e=="string",J=e=>typeof e=="symbol",K=e=>typeof e=="symbol";new Set(Object.getOwnPropertyNames(Symbol).map(e=>Symbol[e]).filter(K));function y(e){return Boolean(e&&e.__v_isRef===!0)}Promise.resolve();function Q(e){e=e||[];const l=[];for(const n of e)T(n)?l.push({code:n}):l.push(n);return l}function O(e,l){return y(e.locale)&&l==="composition"}function G(e){return"global"in e&&"mode"in e}function V(e){return G(e)?O(e.global,e.mode)?e.global.locale.value:e.global.locale:y(e.locale)?e.locale.value:e.locale}function q(e,l){G(e)?O(e.global,e.mode)?e.global.locale.value=l:e.global.locale=l:y(e.locale)&&(e.locale.value=l)}function Z(e,l,n){return e.replace(/\/+$/,"")+(l?"/":"")||(n?"":"/")}function ee(e,{localeCodes:l=[]}={}){if(!O(e.global,e.mode))throw new Error("");const n=s.ref(l);e.global.locales=s.computed(()=>n.value),console.log("... extends i18n done")}const oe={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},w="",B=oe.PREFIX_EXCEPT_DEFAULT,z=!1,U="___",N="default";function X(e,{defaultLocale:l=w,strategy:n=B,trailingSlash:_=z,routesNameSeparator:h=U,defaultLocaleRouteNameSuffix:m=N,includeUprefixedFallback:d=!1,localeCodes:I=[]}={}){if(n==="no_prefix")return e;const R=I.map(o=>T(o)?o:o.code);function g(o,E,F=!1,p=!1){return o.redirect&&(!o.component||!o.file)?[o]:E.reduce((c,a)=>{const{name:t}=o;let{path:i}=o;const u=v({},o);t&&(u.name=`${t}${h}${a}`),o.children&&(u.children=o.children.reduce((A,M)=>[...A,...g(M,[a],!0,p)],[]));const S=a===l;if(S&&n==="prefix_and_default")if(F)F&&p&&t&&(u.name+=`${h}${m}`);else{const A=j(v({},u),{path:i});if(t&&(A.name=`${u.name}${h}${m}`),o.children){A.children=[];for(const M of o.children)A.children=A.children.concat(g(M,[a],!0,!0))}c.push(A)}const L=F&&!i.startsWith("/"),P=!L&&!(S&&n==="prefix_except_default");return P&&(i=`/${a}${i}`),i&&(i=Z(i,_,L)),P&&S&&n==="prefix"&&d&&c.push(v({},o)),u.path=i,c.push(u),c},[])}return e.reduce((o,E)=>[...o,...g(E,R||[])],[])}function te(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function ne(e,l,n){const _=`(${e.join("|")})`,h=`(?:${l}${n})?`,m=new RegExp(`${l}${_}${h}$`,"i"),d=te(e);return R=>{if(R.name){const o=(T(R.name)?R.name:R.name.toString()).match(m);if(o&&o.length>1)return o[1]}else if(R.path){const g=R.path.match(d);if(g&&g.length>1)return g[1]}return""}}function ce({router:e,i18n:l,defaultLocale:n=w,strategy:_=B,trailingSlash:h=z,routesNameSeparator:m=U,defaultLocaleRouteNameSuffix:d=N,localeCodes:I=[]}={}){const R=Q(I),g=ne(R.map(o=>o.code),m,d);if(ee(l,{localeCodes:R}),s.isVue2){const o=e,E=o.constructor,F=o.options.routes||[],p=X(F,{localeCodes:I,defaultLocale:n,strategy:_,trailingSlash:h,routesNameSeparator:m,defaultLocaleRouteNameSuffix:d});console.log("vue2 routes",F,p);const b=new E({mode:"history",base:o.options.base,routes:p});return b.__defaultLocale=n,b.__strategy=_,b.__trailingSlash=h,b.__routesNameSeparator=m,b.__defaultLocaleRouteNameSuffix=d,b.beforeEach((c,a,t)=>{console.log("beforeEach",c,a);const i=V(l),u=g(c)||i||n||"";console.log("currentLocale",i,"finalLocale",u),i!==u&&q(l,u),t()}),b}else{const o=e,E=o.options.routes||[],F=X(E,{localeCodes:I,defaultLocale:n,strategy:_,trailingSlash:h,routesNameSeparator:m,defaultLocaleRouteNameSuffix:d});return console.log("vue3 routes",E,F,o),E.forEach(p=>o.removeRoute(p.name)),F.forEach(p=>o.addRoute(p)),o.__defaultLocale=n,o.__strategy=_,o.__trailingSlash=h,o.__routesNameSeparator=m,o.__defaultLocaleRouteNameSuffix=d,o.beforeEach((p,b,c)=>{console.log("beforeEach",p,b);const a=V(l),t=g(p)||a||n||"";console.log("currentLocale",a,"finalLocale",t),a!==t&&q(l,t),c()}),o}}function le(){return $.useI18n()}function ae(e={}){const l=le(),n=f.useRouter(),_=f.useRoute(),h=e.defaultLocaleRouteNameSuffix||n.__defaultLocaleRouteNameSuffix,m=e.defaultLocale||n.__defaultLocale,d=e.routesNameSeparator||n.__routesNameSeparator,I=e.strategy||n.__strategy;function R(c){const a=c!=null?c:s.isRef(_)?_.value:_;return a.name?D(a.name).split(d)[0]:void 0}function g(c,a){let t=D(c)+(I==="no_prefix"?"":d+a);return a===m&&I==="prefix_and_default"&&(t+=d+h),t}function o(c,a){const t=a||V(l);let i=c;T(c)&&(c[0]==="/"?i={path:c}:i={name:c});let u=C({},i);if(u.path&&!u.name){const L=n.resolve(u),P=s.isVue2?L.route:L,A=R(P);T(A)&&(u={name:g(A,t),params:P.params,query:P.query,hash:P.hash})}else{u.name=g(u.name,t);const{params:L}=u;L&&L["0"]===void 0&&L.pathMatch&&(L["0"]=L.pathMatch)}const S=n.resolve(u);return(s.isVue2?S.route.name:S.name)?S:n.resolve(c)}function E(c,a){const t=o(c,a);return t==null?"":s.isVue2?t.route.redirectedFrom||t.route.fullPath:t.redirectedFrom||t.fullPath}function F(c,a){const t=o(c,a);return t==null?void 0:s.isVue2?t.route:t}function p(c,a){const t=o(c,a);return t==null?void 0:s.isVue2?t.location:t.href}function b(c){const a=R();if(!a)return"";const P=s.isVue2&&s.isRef(_)?_.value:_,{params:t}=P,i=Y(P,["params"]),u={},S=C({},i,{name:a,params:j(v(v({},t),u),{0:t.pathMatch})}),L=E(S,c);return console.log("switchLocalePath",l.locale.value,c,L),L}return{localePath:E,localeRoute:F,localeLocation:p,switchLocalePath:b}}function D(e){return T(e)?e:J(e)?e.toString():""}function se({router:e,i18n:l,defaultLocale:n=w,trailingSlash:_=z,routesNameSeparator:h=U,defaultLocaleRouteNameSuffix:m=N,localeCodes:d=[]}={}){if(e==null)throw new Error("TODO");return ce({router:e,i18n:l,defaultLocale:n,trailingSlash:_,routesNameSeparator:h,defaultLocaleRouteNameSuffix:m,localeCodes:d})}const re="";return r.VERSION=re,r.extendRouting=se,r.localizeRoutes=X,r.useI18nRouting=ae,Object.defineProperty(r,"__esModule",{value:!0}),r[Symbol.toStringTag]="Module",r}({},VueDemi,VueRouterComposable,VueI18n); | ||
*/const j=Object.assign,A=e=>typeof e=="string",ae=e=>typeof e=="symbol",le=/\/$|\/\?/;function O(e="",t=!1){return t?le.test(e):e.endsWith("/")}function ne(e="",t=!1){if(!t)return(O(e)?e.slice(0,-1):e)||"/";if(!O(e,!0))return e||"/";const[a,...l]=e.split("?");return(a.slice(0,-1)||"/")+(l.length?`?${l.join("?")}`:"")}function oe(e="",t=!1){if(!t)return e.endsWith("/")?e:e+"/";if(O(e,!0))return e||"/";const[a,...l]=e.split("?");return a+"/"+(l.length?`?${l.join("?")}`:"")}function ce(e){e=e||[];const t=[];for(const a of e)A(a)?t.push({code:a}):t.push(a);return t}function y(e,t){return n.isRef(e.locale)&&t==="composition"}function M(e){return"global"in e&&"mode"in e}function G(e){return M(e)?y(e.global,e.mode)?e.global.locale.value:e.global.locale:e.locale.value}function re(e,t){M(e)?y(e.global,e.mode)?e.global.locale.value=t:e.global.locale=t:n.isRef(e.locale)&&(e.locale.value=t)}function se(e,t,a){return e.replace(/\/+$/,"")+(t?"/":"")||(a?"":"/")}function W(e){return A(e)?e:ae(e)?e.toString():"(null)"}function q(e,t,{defaultLocale:a,strategy:l,routesNameSeparator:u,defaultLocaleRouteNameSuffix:i}){let d=W(e)+(l==="no_prefix"?"":u+t);return t===a&&l==="prefix_and_default"&&(d+=u+i),d}const p={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},w="",U=p.PREFIX_EXCEPT_DEFAULT,H=!1,I="___",X="default";function v(e=s.useRoute(),t=s.useRouter().__routesNameSeparator||I){const a=n.unref(e);return a.name?W(a.name).split(t)[0]:null}const ue=new Set([p.PREFIX_AND_DEFAULT,p.PREFIX_EXCEPT_DEFAULT]);function k(e,t,a){const l=V(e,t,a);return l==null?"":n.isVue3?l.redirectedFrom||l.fullPath:l.route.redirectedFrom||l.route.fullPath}function fe(e,t,a){const l=V(e,t,a);return l==null?void 0:n.isVue3?l:l.route}function ie(e,t,a){const l=V(e,t,a);return l==null?void 0:n.isVue3?l:l.location}function V(e,t,{router:a=s.useRouter(),i18n:l=E.useI18n(),defaultLocale:u=w,defaultLocaleRouteNameSuffix:i=X,routesNameSeparator:d=I,strategy:L=U,trailingSlash:f=!1}={}){const h=a.__defaultLocaleRouteNameSuffix||i,c=a.__defaultLocale||u,P=a.__routesNameSeparator||d,S=a.__strategy||L,T=t||G(l);let _=e;A(e)&&(_[0]==="/"?_={path:e}:_={name:e});let r=j({},_);if(r.path&&!r.name){const m=a.resolve(r),R=n.isVue3?m:m.route,g=v(R);A(g)?r={name:q(g,T,{defaultLocale:c,strategy:S,routesNameSeparator:P,defaultLocaleRouteNameSuffix:h}),params:R.params,query:R.query,hash:R.hash}:(!(T===u&&ue.has(S))&&S!==p.NO_PREFIX&&(r.path=`/${T}${r.path}`),r.path=f?oe(r.path,!0):ne(r.path,!0))}else{r.name=q(r.name,T,{defaultLocale:c,strategy:S,routesNameSeparator:P,defaultLocaleRouteNameSuffix:h});const{params:m}=r;m&&m["0"]===void 0&&m.pathMatch&&(m["0"]=m.pathMatch)}const F=a.resolve(r);return(n.isVue3?F.name:F.route.name)?F:a.resolve(e)}function de(e,{route:t=s.useRoute(),i18n:a=E.useI18n()}={}){const l=v();if(!l)return"";const h=!n.isVue3&&n.isRef(t)?t.value:t,{params:u}=h,i=Z(h,["params"]),d={},L=j({},i,{name:l,params:D($($({},u),d),{0:u.pathMatch})});return k(L,e,{route:t,i18n:a})}function B(e,{defaultLocale:t=w,strategy:a=U,trailingSlash:l=H,routesNameSeparator:u=I,defaultLocaleRouteNameSuffix:i=X,includeUprefixedFallback:d=!1,locales:L=[]}={}){if(a==="no_prefix")return e;const f=L.map(c=>A(c)?c:c.code);function h(c,P,S=!1,T=!1){return c.redirect&&(!c.component||!c.file)?[c]:P.reduce((r,F)=>{const{name:m}=c;let{path:R}=c;const g=$({},c);m&&(g.name=`${m}${u}${F}`),c.children&&(g.children=c.children.reduce((N,C)=>[...N,...h(C,[F],!0,T)],[]));const x=F===t;if(x&&a==="prefix_and_default")if(S)S&&T&&m&&(g.name+=`${u}${i}`);else{const N=D($({},g),{path:R});if(m&&(N.name=`${g.name}${u}${i}`),c.children){N.children=[];for(const C of c.children)N.children=N.children.concat(h(C,[F],!0,!0))}r.push(N)}const z=S&&!R.startsWith("/"),Y=!z&&!(x&&a==="prefix_except_default");return Y&&(R=`/${F}${R}`),R&&(R=se(R,l,z)),Y&&x&&a==="prefix"&&d&&r.push($({},c)),g.path=R,r.push(g),r},[])}return e.reduce((c,P)=>[...c,...h(P,f||[])],[])}function he(e,{locales:t=[]}={}){if(!y(e.global,e.mode))throw new Error("TODO:");const a=n.ref(t);e.global.locales=n.computed(()=>a.value)}function _e(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function Re(e,t,a){const l=`(${e.join("|")})`,u=`(?:${t}${a})?`,i=new RegExp(`${t}${l}${u}$`,"i"),d=_e(e);return f=>{if(f.name){const c=(A(f.name)?f.name:f.name.toString()).match(i);if(c&&c.length>1)return c[1]}else if(f.path){const h=f.path.match(d);if(h&&h.length>1)return h[1]}return""}}function me(e){var t,a,l,u,i,d,L,f;return e.version=(t=e.version)!=null?t:4,e.defaultLocale=(a=e.defaultLocale)!=null?a:w,e.strategy=(l=e.strategy)!=null?l:U,e.trailingSlash=(u=e.trailingSlash)!=null?u:H,e.routesNameSeparator=(i=e.routesNameSeparator)!=null?i:I,e.defaultLocaleRouteNameSuffix=(d=e.defaultLocaleRouteNameSuffix)!=null?d:X,e.locales=(L=e.locales)!=null?L:[],e.routes=(f=e.routes)!=null?f:[],e}function Le(e,t={}){const{version:a,defaultLocale:l,locales:u,strategy:i,trailingSlash:d,routesNameSeparator:L,defaultLocaleRouteNameSuffix:f,routes:h}=me(t),c=ce(u),P=c.map(r=>r.code),S=Re(P,L,f);he(e,{locales:c});const T=B(h,{locales:u,defaultLocale:l,strategy:i,trailingSlash:d,routesNameSeparator:L,defaultLocaleRouteNameSuffix:f});t.routes=T;let _=null;if(n.isVue3&&a===4)_=s.createRouter(t);else if(n.isVue2&&a===3)_=new te.default(t);else throw new Error("TODO:");return _.__defaultLocale=l,_.__localeCodes=P,_.__strategy=i,_.__trailingSlash=d,_.__routesNameSeparator=L,_.__defaultLocaleRouteNameSuffix=f,_.beforeEach((r,F,m)=>{const R=G(e),g=S(r)||R||l||"";R!==g&&re(e,g),m()}),_}const ge="0.0.0";return o.VERSION=ge,o.createRouter=Le,o.getRouteBaseName=v,o.localeLocation=ie,o.localePath=k,o.localeRoute=fe,o.localizeRoutes=B,o.switchLocalePath=de,Object.defineProperty(o,"__esModule",{value:!0}),o[Symbol.toStringTag]="Module",o}({},VueDemi,VueRouter,VueI18n); |
{ | ||
"name": "vue-i18n-routing", | ||
"description": "The i18n routing with using vue-i18n", | ||
"version": "0.0.0-b5558e5", | ||
"version": "0.0.0-bc09a5f", | ||
"dependencies": { | ||
"@intlify/shared": "beta", | ||
"@intlify/vue-router-composable": "0.0.0-b5558e5", | ||
"vue-demi": "*" | ||
"@intlify/vue-i18n-bridge": "^0.3.4", | ||
"@intlify/vue-router-bridge": "^0.3.4", | ||
"ufo": "^0.7.9", | ||
"vue-demi": "^0.12.1" | ||
}, | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.18.20", | ||
"@vue/test-utils": "^1.3.0", | ||
"api-docs-gen": "^0.4.0", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.5.4", | ||
"vite": "^2.7.4", | ||
@@ -20,2 +24,3 @@ "vite-plugin-dts": "^0.9.6", | ||
"vue-i18n-legacy": "npm:vue-i18n@latest", | ||
"vue-template-compiler": "^2.6.14", | ||
"vue-router": "^4.0.12", | ||
@@ -28,2 +33,4 @@ "vue-router3": "npm:vue-router@latest", | ||
"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" | ||
@@ -30,0 +37,0 @@ }, |
Sorry, the diff of this file is not supported yet
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
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
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
42164
683
0
10
15
1
+ Addedufo@^0.7.9
+ Added@intlify/core-base@9.14.2(transitive)
+ Added@intlify/message-compiler@9.14.2(transitive)
+ Added@intlify/shared@9.14.2(transitive)
+ Added@intlify/vue-devtools@9.14.2(transitive)
+ Added@intlify/vue-i18n-bridge@0.3.6(transitive)
+ Added@intlify/vue-router-bridge@0.3.6(transitive)
+ Addedufo@0.7.11(transitive)
+ Addedvue-demi@0.12.5(transitive)
+ Addedvue-i18n@9.14.2(transitive)
+ Addedvue-i18n-bridge@9.14.2(transitive)
- Removed@intlify/vue-router-composable@0.0.0-b5558e5(transitive)
Updatedvue-demi@^0.12.1