vue-i18n-routing
Advanced tools
Comparing version 0.1.4 to 0.1.5-57be896
@@ -1,5 +0,1042 @@ | ||
const VueI18nRouting=function(f,h,g,w){"use strict";const ye=(e=>e&&typeof e=="object"&&"default"in e?e:{default:e})(g),Q={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},G="",B=Q.PREFIX_EXCEPT_DEFAULT,M=!1,k="___",z="default",Y="ltr",J="";/*! | ||
* shared v9.3.0-beta.3 | ||
* (c) 2022 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/process.env.NODE_ENV;const Ie=typeof Symbol=="function"&&typeof Symbol.toStringTag=="symbol",Ce=e=>Ie?Symbol(e):e,H=Object.assign,Te=Array.isArray,Oe=e=>typeof e=="function",T=e=>typeof e=="string",be=e=>typeof e=="boolean",Pe=e=>typeof e=="symbol",K=e=>e!==null&&typeof e=="object",Ue=/\/$|\/\?/;function Z(e="",t=!1){return t?Ue.test(e):e.endsWith("/")}function Ne(e="",t=!1){if(!t)return(Z(e)?e.slice(0,-1):e)||"/";if(!Z(e,!0))return e||"/";const[o,...n]=e.split("?");return(o.slice(0,-1)||"/")+(n.length?`?${n.join("?")}`:"")}function Fe(e="",t=!1){if(!t)return e.endsWith("/")?e:e+"/";if(Z(e,!0))return e||"/";const[o,...n]=e.split("?");return o+"/"+(n.length?`?${n.join("?")}`:"")}const we=typeof window<"u";function ae(e,t){typeof console<"u"&&(console.warn("[vue-i18n-routing] "+e),t&&console.warn(t.stack))}function ce(e){e=e||[];const t=[];for(const o of e)T(o)?t.push({code:o}):t.push(o);return t}function U(e){return e!=null&&"global"in e&&"mode"in e}function N(e){return e!=null&&!("__composer"in e)&&h.isRef(e.locale)}function O(e){return e!=null&&"__composer"in e}function $(e){return e!=null&&!("__composer"in e)&&!h.isRef(e.locale)}function y(e){return e!=null&&("__VUE_I18N_BRIDGE__"in e||"_sync"in e)}function se(e){return U(e)?N(e.global)?e.global:e.global.__composer:O(e)?e.__composer:e}function X(e){const t=U(e)?e.global:e;return N(t)?h.isVue2&&y(e)?e.locale:t.locale.value:($(t)||O(t)||y(t),t.locale)}function re(e){const t=U(e)?e.global:e;return N(t)?h.isVue2&&y(e)?e.locales:t.locales.value:($(t)||O(t)||y(t),t.locales)}function $e(e){const t=U(e)?e.global:e;return N(t)?h.isVue2&&y(e)?e.localeCodes:t.localeCodes.value:($(t)||O(t)||y(t),t.localeCodes)}function ie(e,t){const o=U(e)?e.global:e;if(N(o))h.isVue2&&y(e)?e.locale=t:o.locale.value=t;else if($(o)||O(o)||y(o))o.locale=t;else throw new Error("TODO:")}function Ve(e,t,o){return e.replace(/\/+$/,"")+(t?"/":"")||(o?"":"/")}function ue(e){return g.isVueRouter4,h.isRef(e)?e.value:e}function fe(e){return T(e)?e:Pe(e)?e.toString():"(null)"}function de(e,t,{defaultLocale:o,strategy:n,routesNameSeparator:a,defaultLocaleRouteNameSuffix:l}){let c=fe(e)+(n==="no_prefix"?"":a+t);return t===o&&n==="prefix_and_default"&&(c+=a+l),c}function he(e,t){return Oe(e)?e(t):e}function je(e,t){const o=[];for(const[n,a]of t.entries()){const l=e.find(c=>c.iso.toLowerCase()===a.toLowerCase());if(l){o.push({code:l.code,score:1-n/t.length});break}}for(const[n,a]of t.entries()){const l=a.split("-")[0].toLowerCase(),c=e.find(s=>s.iso.split("-")[0].toLowerCase()===l);if(c){o.push({code:c.code,score:.999-n/t.length});break}}return o}const ve=je;function Ge(e,t){return e.score===t.score?t.code.length-e.code.length:t.score-e.score}const Be=Ge;function Me(e,t,{matcher:o=ve,comparer:n=Be}={}){const a=[];for(const c of e){const{code:s}=c,i=c.iso||s;a.push({code:s,iso:i})}const l=o(a,t);return l.length>1&&l.sort(n),l.length?l[0].code:""}function Le(e,{defaultLocale:t=G,strategy:o=B,trailingSlash:n=M,routesNameSeparator:a=k,defaultLocaleRouteNameSuffix:l=z,includeUprefixedFallback:c=!1,optionsResolver:s=void 0,locales:i=[]}={}){if(o==="no_prefix")return e;const u=i.map(r=>T(r)?r:r.code);function d(r,L,_=!1,I=!1){if(r.redirect&&(!r.component||!r.file))return[r];let p=null;if(s!=null&&(p=s(r,L),p==null))return[r];const R={locales:u,paths:{}};if(p!=null&&H(R,p),H(R,{locales:L}),R.locales.length>0&&p&&p.locales!=null&&p.locales.length>0){const E=[];for(const m of R.locales)p.locales.includes(m)&&E.push(m);R.locales=E}return R.locales.reduce((E,m)=>{const{name:v}=r;let{path:S}=r;const A={...r};v&&(A.name=`${v}${a}${m}`),r.children&&(A.children=r.children.reduce((P,le)=>[...P,...d(le,[m],!0,I)],[])),R.paths&&R.paths[m]&&(S=R.paths[m]);const F=m===t;if(F&&o==="prefix_and_default")if(_)_&&I&&v&&(A.name+=`${a}${l}`);else{const P={...A,path:S};if(v&&(P.name=`${A.name}${a}${l}`),r.children){P.children=[];for(const le of r.children)P.children=P.children.concat(d(le,[m],!0,!0))}E.push(P)}const Se=_&&!S.startsWith("/"),Ae=!Se&&!(F&&o==="prefix_except_default");return Ae&&(S=`/${m}${S}`),S&&(S=Ve(S,n,Se)),Ae&&F&&o==="prefix"&&c&&E.push({...r}),A.path=S,E.push(A),E},[])}return e.reduce((r,L)=>[...r,...d(L,u||[])],[])}function C(e){return function(){return Reflect.apply(e,{getRouteBaseName:this.getRouteBaseName,localePath:this.localePath,localeRoute:this.localeRoute,localeLocation:this.localeLocation,resolveRoute:this.resolveRoute,switchLocalePath:this.switchLocalePath,localeHead:this.localeHead,i18n:this.$i18n,route:this.$route,router:this.$router},arguments)}}function ge(e,{locales:t=[],localeCodes:o=[],baseUrl:n=J,hooks:a={}}={}){const l=h.effectScope(),c=e.install;return e.install=(s,...i)=>{Reflect.apply(c,e,[s,...i]);const u=se(e);l.run(()=>ke(u,{locales:t,localeCodes:o,baseUrl:n,hooks:a})),O(e.global)&&He(e.global,a.onExtendVueI18n);const d=s,r=e.mode==="composition"?h.isVue3?d.config.globalProperties.$i18n:e:h.isVue2?e:null;if(r&&ze(r,u,a.onExtendExportedGlobal),(Xe(i[0])?i[0]:{inject:!0}).inject&&s.mixin({methods:{resolveRoute:C(V),localePath:C(q),localeRoute:C(x),localeLocation:C(te),switchLocalePath:C(j),getRouteBaseName:C(b),localeHead:C(oe)}}),d.unmount){const _=d.unmount;d.unmount=()=>{l.stop(),_()}}},l}function ke(e,t){const{locales:o,localeCodes:n,baseUrl:a}=t,l=h.ref(o),c=h.ref(n);e.locales=h.computed(()=>l.value),e.localeCodes=h.computed(()=>c.value),e.__baseUrl=he(a,{}),t.hooks&&t.hooks.onExtendComposer&&t.hooks.onExtendComposer(e)}function ze(e,t,o){const n=[{locales:{get(){return t.locales.value}},localeCodes:{get(){return t.localeCodes.value}},__baseUrl:{get(){return t.__baseUrl}}}];o&&n.push(o(t));for(const a of n)for(const[l,c]of Object.entries(a))Object.defineProperty(e,l,c)}function He(e,t){const o=se(e),n=[{locales:{get(){return o.locales.value}},localeCodes:{get(){return o.localeCodes.value}},__baseUrl:{get(){return o.__baseUrl}}}];t&&n.push(t(o));for(const a of n)for(const[l,c]of Object.entries(a))Object.defineProperty(e,l,c)}function Xe(e){return K(e)&&"inject"in e&&be(e.inject)}const ee=Ce("vue-i18n-routing-gor");function _e(e,t){e[ee]?ae("already registered global options"):e[ee]=t}function me(e){var t;return(t=e[ee])!=null?t:{}}function De(e,t={}){const{version:o,defaultLocale:n,locales:a,strategy:l,trailingSlash:c,routesNameSeparator:s,defaultLocaleRouteNameSuffix:i,defaultDirection:u,baseUrl:d,routes:r,routeOptionsResolver:L}=xe(t),_=ce(a),I=_.map(m=>m.code),p=pe(I,s,i);ge(e,{locales:_,baseUrl:d,localeCodes:I});const R=Le(r,{locales:a,defaultLocale:n,strategy:l,trailingSlash:c,routesNameSeparator:s,defaultLocaleRouteNameSuffix:i,optionsResolver:L});t.routes=R;const E=qe(t,o);return _e(E,{defaultLocale:n,localeCodes:I,strategy:l,trailingSlash:c,routesNameSeparator:s,defaultLocaleRouteNameSuffix:i,defaultDirection:u}),E.beforeEach((m,v,S)=>{const A=X(e),F=p(m)||A||n||"";A!==F&&ie(e,F),S()}),E}function qe(e,t){if(h.isVue3&&t===4)return g.createRouter(e);if(h.isVue2&&t===3)return new ye.default(e);throw new Error("TODO:")}function Re(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function pe(e,t,o){const n=`(${e.join("|")})`,a=`(?:${t}${o})?`,l=new RegExp(`${t}${n}${a}$`,"i"),c=Re(e);return i=>{if(K(i)){if(i.name){const d=(T(i.name)?i.name:i.name.toString()).match(l);if(d&&d.length>1)return d[1]}else if(i.path){const u=i.path.match(c);if(u&&u.length>1)return u[1]}}else if(T(i)){const u=i.match(c);if(u&&u.length>1)return u[1]}return""}}function xe(e){var t,o,n,a,l,c,s,i,u,d;return e.version=(t=e.version)!=null?t:4,e.defaultLocale=(o=e.defaultLocale)!=null?o:G,e.strategy=(n=e.strategy)!=null?n:B,e.trailingSlash=(a=e.trailingSlash)!=null?a:M,e.routesNameSeparator=(l=e.routesNameSeparator)!=null?l:k,e.defaultLocaleRouteNameSuffix=(c=e.defaultLocaleRouteNameSuffix)!=null?c:z,e.locales=(s=e.locales)!=null?s:[],e.defaultDirection=(i=e.defaultDirection)!=null?i:Y,e.baseUrl=(u=e.baseUrl)!=null?u:J,e.routes=(d=e.routes)!=null?d:[],e}function D(e,t,{defaultLocale:o=G,defaultDirection:n=Y,defaultLocaleRouteNameSuffix:a=z,routesNameSeparator:l=k,strategy:c=B,trailingSlash:s=M,localeCodes:i=[]}={}){const u=me(e);return{defaultLocale:t.defaultLocale||u.defaultLocale||o,defaultDirection:t.defaultDirection||u.defaultDirection||n,defaultLocaleRouteNameSuffix:t.defaultLocaleRouteNameSuffix||u.defaultLocaleRouteNameSuffix||a,routesNameSeparator:t.routesNameSeparator||u.routesNameSeparator||l,strategy:t.strategy||u.strategy||c,trailingSlash:t.trailingSlash||u.trailingSlash||s,localeCodes:t.localeCodes||u.localeCodes||i}}const We=new Set(["prefix_and_default","prefix_except_default"]);function b(e){const t=this.router,{routesNameSeparator:o}=D(t,this),n=e!=null?h.isRef(e)?h.unref(e):e:this.route;return n==null||!n.name?void 0:fe(n.name).split(o)[0]}function q(e,t){const o=V.call(this,e,t);return o==null?"":h.isVue3?o.redirectedFrom||o.fullPath:o.route.redirectedFrom||o.route.fullPath}function x(e,t){const o=V.call(this,e,t);return o==null?void 0:h.isVue3?o:o.route}function te(e,t){const o=V.call(this,e,t);return o==null?void 0:h.isVue3?o:o.location}function V(e,t){const o=this.router,n=this.i18n,a=t||X(n),{routesNameSeparator:l,defaultLocale:c,defaultLocaleRouteNameSuffix:s,strategy:i,trailingSlash:u}=D(o,this);let d=e;T(e)&&(d[0]==="/"?d={path:e}:d={name:e});let r=H({},d);if(r.path&&!r.name){let L=null;try{L=o.resolve(r)}catch{}const _=h.isVue3?L:L.route,I=b.call(this,_);T(I)?(r={name:de(I,a,{defaultLocale:c,strategy:i,routesNameSeparator:l,defaultLocaleRouteNameSuffix:s}),params:_.params,query:_.query,hash:_.hash},h.isVue3&&(r.state=_.state)):(!(a===c&&We.has(i))&&i!=="no_prefix"&&(r.path=`/${a}${r.path}`),r.path=u?Fe(r.path,!0):Ne(r.path,!0))}else if(!r.name&&!r.path&&(r.name=b.call(this,this.route)),r.name=de(r.name,a,{defaultLocale:c,strategy:i,routesNameSeparator:l,defaultLocaleRouteNameSuffix:s}),h.isVue2){const{params:L}=r;L&&L[0]===void 0&&L.pathMatch&&(L[0]=L.pathMatch)}try{const L=o.resolve(r);return(h.isVue3?L.name:L.route.name)?L:o.resolve(e)}catch(L){if(h.isVue3&&L.type===1)return null;if(h.isVue2)return null}}function j(e){const t=this.route,o=b.call(this,t);if(!o)return"";const{params:n,...a}=!h.isVue3&&h.isRef(t)?t.value:t,c={name:o,params:{...n,...{}}};h.isVue2&&(c.params[0]=n.pathMatch);const s=H({},a,c);return q.call(this,s,e)}function oe({addDirAttribute:e=!1,addSeoAttributes:t=!1}={}){const o=this.router,n=this.i18n,{defaultDirection:a}=D(o,this),l={htmlAttrs:{},link:[],meta:[]};if(n.locales==null||n.__baseUrl==null)return l;const c=X(n),s=re(n),i=ce(s).find(r=>r.code===c)||{code:c},u=i.iso,d=i.dir||a;return e&&(l.htmlAttrs.dir=d),t&&c&&n.locales&&(u&&(l.htmlAttrs.lang=u),Qe.call(this,s,n.__baseUrl,l.link),Ye.call(this,n.__baseUrl,l.link,t),Je(i,u,l.meta),Ke(s,u,l.meta)),l}function Qe(e,t,o){const n=this.router,{defaultLocale:a,strategy:l}=D(n,this);if(l===Q.NO_PREFIX)return;const c=new Map;for(const s of e){const i=s.iso;if(!i){ae("Locale ISO code is required to generate alternate link");continue}const[u,d]=i.split("-");u&&d&&(s.isCatchallLocale||!c.has(u))&&c.set(u,s),c.set(i,s)}for(const[s,i]of c.entries()){const u=j.call(this,i.code);u&&o.push({hid:`i18n-alt-${s}`,rel:"alternate",href:ne(u,t),hreflang:s})}if(a){const s=j.call(this,a);s&&o.push({hid:"i18n-xd",rel:"alternate",href:ne(s,t),hreflang:"x-default"})}}function Ye(e,t,o){const n=this.route,a=x.call(this,{...n,name:b.call(this,n)});if(a){let l=ne(a.path,e);const c=K(o)&&o.canonicalQueries||[];if(c.length){const s=a.query,i=new URLSearchParams;for(const d of c)if(d in s){const r=s[d];Te(r)?r.forEach(L=>i.append(d,L||"")):i.append(d,r||"")}const u=i.toString();u&&(l=`${l}?${u}`)}t.push({hid:"i18n-can",rel:"canonical",href:l})}}function Je(e,t,o){!(e&&t)||o.push({hid:"i18n-og",property:"og:locale",content:Ee(t)})}function Ke(e,t,o){const n=e.filter(a=>{const l=a.iso;return l&&l!==t});if(n.length){const a=n.map(l=>({hid:`i18n-og-alt-${l.iso}`,property:"og:locale:alternate",content:Ee(l.iso)}));o.push(...a)}}function Ee(e){return(e||"").replace(/-/g,"_")}function ne(e,t){return e.match(/^https?:\/\//)?e:t+e}function W(e,t){const{router:o,route:n,i18n:a,defaultLocale:l,strategy:c,defaultLocaleRouteNameSuffix:s,trailingSlash:i,routesNameSeparator:u}=e;return function(...d){return Reflect.apply(t,{router:o,route:n,i18n:a,defaultLocale:l,strategy:c,defaultLocaleRouteNameSuffix:s,trailingSlash:i,routesNameSeparator:u},d)}}function Ze(e=g.useRoute(),{router:t=g.useRouter(),routesNameSeparator:o=void 0}={}){const n={router:t,route:e,routesNameSeparator:o};return b.call(n,e)}function et({router:e=g.useRouter(),route:t=g.useRoute(),i18n:o=w.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:c=void 0,trailingSlash:s=void 0}={}){return W({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:c,trailingSlash:s},q)}function tt({router:e=g.useRouter(),route:t=g.useRoute(),i18n:o=w.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:c=void 0,trailingSlash:s=void 0}={}){return W({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:c,trailingSlash:s},x)}function ot({router:e=g.useRouter(),route:t=g.useRoute(),i18n:o=w.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:c=void 0,trailingSlash:s=void 0}={}){return W({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:c,trailingSlash:s},te)}function nt({router:e=g.useRouter(),route:t=g.useRoute(),i18n:o=w.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:c=void 0,trailingSlash:s=void 0}={}){return W({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:c,trailingSlash:s},j)}function lt({addDirAttribute:e=!1,addSeoAttributes:t=!1,strategy:o=void 0,defaultLocale:n=void 0,route:a=g.useRoute(),router:l=g.useRouter(),i18n:c=w.useI18n()}={}){const s=l,i=h.ref({htmlAttrs:{},link:[],meta:[]});function u(){i.value={htmlAttrs:{},link:[],meta:[]}}function d(r){i.value=Reflect.apply(oe,{router:l,route:r,i18n:c,defaultLocale:n,strategy:o},[{addDirAttribute:e,addSeoAttributes:t}])}if(we)if(h.isVue3){const r=h.watchEffect(()=>{u(),d(ue(s.currentRoute))});h.onUnmounted(()=>r())}else{const r=s.afterEach((L,_)=>{u(),d(L)});h.onUnmounted(()=>r()),d(a)}else d(ue(s.currentRoute));return i}const at="0.1.4";return f.DEFAULT_BASE_URL=J,f.DEFAULT_DETECTION_DIRECTION=Y,f.DEFAULT_LOCALE=G,f.DEFAULT_LOCALE_ROUTE_NAME_SUFFIX=z,f.DEFAULT_ROUTES_NAME_SEPARATOR=k,f.DEFAULT_STRATEGY=B,f.DEFAULT_TRAILING_SLASH=M,f.STRATEGIES=Q,f.VERSION=at,f.createLocaleFromRouteGetter=pe,f.createRouter=De,f.extendI18n=ge,f.findBrowserLocale=Me,f.getGlobalOptions=me,f.getLocale=X,f.getLocaleCodes=$e,f.getLocales=re,f.getLocalesRegex=Re,f.getRouteBaseName=b,f.isComposer=N,f.isExportedGlobalComposer=$,f.isI18nInstance=U,f.isLegacyVueI18n=y,f.isVueI18n=O,f.localeHead=oe,f.localeLocation=te,f.localePath=q,f.localeRoute=x,f.localizeRoutes=Le,f.proxyVueInstance=C,f.registerGlobalOptions=_e,f.resolveBaseUrl=he,f.resolveRoute=V,f.setLocale=ie,f.switchLocalePath=j,f.useLocaleHead=lt,f.useLocaleLocation=ot,f.useLocalePath=et,f.useLocaleRoute=tt,f.useRouteBaseName=Ze,f.useSwitchLocalePath=nt,Object.defineProperties(f,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),f}({},VueDemi,VueRouter,VueI18n); | ||
var VueI18nRouting = function(exports, vueDemi, VueRouter3, vueI18nBridge) { | ||
"use strict"; | ||
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e }; | ||
const VueRouter3__default = /* @__PURE__ */ _interopDefaultLegacy(VueRouter3); | ||
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"; | ||
const DEFAULT_DETECTION_DIRECTION = "ltr"; | ||
const DEFAULT_BASE_URL = ""; | ||
/*! | ||
* shared v9.3.0-beta.3 | ||
* (c) 2022 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/ | ||
const hasSymbol = typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol"; | ||
const makeSymbol = (name) => hasSymbol ? Symbol(name) : name; | ||
const assign = Object.assign; | ||
const isArray = Array.isArray; | ||
const isFunction = (val) => typeof val === "function"; | ||
const isString = (val) => typeof val === "string"; | ||
const isBoolean = (val) => typeof val === "boolean"; | ||
const isSymbol = (val) => typeof val === "symbol"; | ||
const isObject = (val) => val !== null && typeof val === "object"; | ||
const TRAILING_SLASH_RE = /\/$|\/\?/; | ||
function hasTrailingSlash(input = "", queryParams = false) { | ||
if (!queryParams) { | ||
return input.endsWith("/"); | ||
} | ||
return TRAILING_SLASH_RE.test(input); | ||
} | ||
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("?")}` : ""); | ||
} | ||
const inBrowser = typeof window !== "undefined"; | ||
function warn(msg, err) { | ||
if (typeof console !== "undefined") { | ||
console.warn(`[vue-i18n-routing] ` + msg); | ||
if (err) { | ||
console.warn(err.stack); | ||
} | ||
} | ||
} | ||
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 isI18nInstance(i18n) { | ||
return i18n != null && "global" in i18n && "mode" in i18n; | ||
} | ||
function isComposer(target) { | ||
return target != null && !("__composer" in target) && vueDemi.isRef(target.locale); | ||
} | ||
function isVueI18n(target) { | ||
return target != null && "__composer" in target; | ||
} | ||
function isExportedGlobalComposer(target) { | ||
return target != null && !("__composer" in target) && !vueDemi.isRef(target.locale); | ||
} | ||
function isLegacyVueI18n(target) { | ||
return target != null && ("__VUE_I18N_BRIDGE__" in target || "_sync" in target); | ||
} | ||
function getComposer(i18n) { | ||
return isI18nInstance(i18n) ? isComposer(i18n.global) ? i18n.global : i18n.global.__composer : isVueI18n(i18n) ? i18n.__composer : i18n; | ||
} | ||
function getLocale(i18n) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
return isComposer(target) ? vueDemi.isVue2 && isLegacyVueI18n(i18n) ? i18n.locale : target.locale.value : isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target) ? target.locale : target.locale; | ||
} | ||
function getLocales(i18n) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
return isComposer(target) ? vueDemi.isVue2 && isLegacyVueI18n(i18n) ? i18n.locales : target.locales.value : isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target) ? target.locales : target.locales; | ||
} | ||
function getLocaleCodes(i18n) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
return isComposer(target) ? vueDemi.isVue2 && isLegacyVueI18n(i18n) ? i18n.localeCodes : target.localeCodes.value : isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target) ? target.localeCodes : target.localeCodes; | ||
} | ||
function setLocale(i18n, locale) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
if (isComposer(target)) { | ||
if (vueDemi.isVue2 && isLegacyVueI18n(i18n)) { | ||
i18n.locale = locale; | ||
} else { | ||
target.locale.value = locale; | ||
} | ||
} else if (isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target)) { | ||
target.locale = locale; | ||
} else { | ||
throw new Error("TODO:"); | ||
} | ||
} | ||
function adjustRoutePathForTrailingSlash(pagePath, trailingSlash, isChildWithRelativePath) { | ||
return pagePath.replace(/\/+$/, "") + (trailingSlash ? "/" : "") || (isChildWithRelativePath ? "" : "/"); | ||
} | ||
function toRawRoute(maybeRoute) { | ||
return VueRouter3.isVueRouter4 ? vueDemi.isRef(maybeRoute) ? maybeRoute.value : maybeRoute : vueDemi.isRef(maybeRoute) ? maybeRoute.value : maybeRoute; | ||
} | ||
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; | ||
} | ||
return name; | ||
} | ||
function resolveBaseUrl(baseUrl, context) { | ||
if (isFunction(baseUrl)) { | ||
return baseUrl(context); | ||
} | ||
return baseUrl; | ||
} | ||
function matchBrowserLocale(locales, browserLocales) { | ||
const matchedLocales = []; | ||
for (const [index, browserCode] of browserLocales.entries()) { | ||
const matchedLocale = locales.find((l) => l.iso.toLowerCase() === browserCode.toLowerCase()); | ||
if (matchedLocale) { | ||
matchedLocales.push({ code: matchedLocale.code, score: 1 - index / browserLocales.length }); | ||
break; | ||
} | ||
} | ||
for (const [index, browserCode] of browserLocales.entries()) { | ||
const languageCode = browserCode.split("-")[0].toLowerCase(); | ||
const matchedLocale = locales.find((l) => l.iso.split("-")[0].toLowerCase() === languageCode); | ||
if (matchedLocale) { | ||
matchedLocales.push({ code: matchedLocale.code, score: 0.999 - index / browserLocales.length }); | ||
break; | ||
} | ||
} | ||
return matchedLocales; | ||
} | ||
const DefaultBrowserLocaleMatcher = matchBrowserLocale; | ||
function compareBrowserLocale(a, b) { | ||
if (a.score === b.score) { | ||
return b.code.length - a.code.length; | ||
} | ||
return b.score - a.score; | ||
} | ||
const DefaultBrowerLocaleComparer = compareBrowserLocale; | ||
function findBrowserLocale(locales, browserLocales, { matcher = DefaultBrowserLocaleMatcher, comparer = DefaultBrowerLocaleComparer } = {}) { | ||
const normalizedLocales = []; | ||
for (const l of locales) { | ||
const { code } = l; | ||
const iso = l.iso || code; | ||
normalizedLocales.push({ code, iso }); | ||
} | ||
const matchedLocales = matcher(normalizedLocales, browserLocales); | ||
if (matchedLocales.length > 1) { | ||
matchedLocales.sort(comparer); | ||
} | ||
return matchedLocales.length ? matchedLocales[0].code : ""; | ||
} | ||
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, | ||
optionsResolver = void 0, | ||
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]; | ||
} | ||
let routeOptions = null; | ||
if (optionsResolver != null) { | ||
routeOptions = optionsResolver(route, allowedLocaleCodes); | ||
if (routeOptions == null) { | ||
return [route]; | ||
} | ||
} | ||
const componentOptions = { | ||
locales: _localeCodes, | ||
paths: {} | ||
}; | ||
if (routeOptions != null) { | ||
assign(componentOptions, routeOptions); | ||
} | ||
assign(componentOptions, { locales: allowedLocaleCodes }); | ||
if (componentOptions.locales.length > 0 && routeOptions && routeOptions.locales != null && routeOptions.locales.length > 0) { | ||
const filteredLocales = []; | ||
for (const locale of componentOptions.locales) { | ||
if (routeOptions.locales.includes(locale)) { | ||
filteredLocales.push(locale); | ||
} | ||
} | ||
componentOptions.locales = filteredLocales; | ||
} | ||
return componentOptions.locales.reduce((_routes, locale) => { | ||
const { name } = route; | ||
let { path } = route; | ||
const localizedRoute = { ...route }; | ||
if (name) { | ||
localizedRoute.name = `${name}${routesNameSeparator}${locale}`; | ||
} | ||
if (route.children) { | ||
localizedRoute.children = route.children.reduce( | ||
(children, child) => [...children, ...makeLocalizedRoutes(child, [locale], true, isExtraPageTree)], | ||
[] | ||
); | ||
} | ||
if (componentOptions.paths && componentOptions.paths[locale]) { | ||
path = componentOptions.paths[locale]; | ||
} | ||
const isDefaultLocale = locale === defaultLocale; | ||
if (isDefaultLocale && strategy === "prefix_and_default") { | ||
if (!isChild) { | ||
const defaultRoute = { ...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({ ...route }); | ||
} | ||
localizedRoute.path = path; | ||
_routes.push(localizedRoute); | ||
return _routes; | ||
}, []); | ||
} | ||
return routes.reduce( | ||
(localized, route) => [...localized, ...makeLocalizedRoutes(route, _localeCodes || [])], | ||
[] | ||
); | ||
} | ||
function proxyVueInstance(target) { | ||
return function() { | ||
return Reflect.apply( | ||
target, | ||
{ | ||
getRouteBaseName: this.getRouteBaseName, | ||
localePath: this.localePath, | ||
localeRoute: this.localeRoute, | ||
localeLocation: this.localeLocation, | ||
resolveRoute: this.resolveRoute, | ||
switchLocalePath: this.switchLocalePath, | ||
localeHead: this.localeHead, | ||
i18n: this.$i18n, | ||
route: this.$route, | ||
router: this.$router | ||
}, | ||
arguments | ||
); | ||
}; | ||
} | ||
function extendI18n(i18n, { locales = [], localeCodes = [], baseUrl = DEFAULT_BASE_URL, hooks = {} } = {}) { | ||
const scope = vueDemi.effectScope(); | ||
const orgInstall = i18n.install; | ||
i18n.install = (vue, ...options) => { | ||
Reflect.apply(orgInstall, i18n, [vue, ...options]); | ||
const composer = getComposer(i18n); | ||
scope.run(() => extendComposer(composer, { locales, localeCodes, baseUrl, hooks })); | ||
if (isVueI18n(i18n.global)) { | ||
extendVueI18n(i18n.global, hooks.onExtendVueI18n); | ||
} | ||
const app = vue; | ||
const exported = i18n.mode === "composition" ? vueDemi.isVue3 ? app.config.globalProperties.$i18n : i18n : vueDemi.isVue2 ? i18n : null; | ||
if (exported) { | ||
extendExportedGlobal(exported, composer, hooks.onExtendExportedGlobal); | ||
} | ||
const pluginOptions = isPluginOptions(options[0]) ? options[0] : { inject: true }; | ||
if (pluginOptions.inject) { | ||
vue.mixin({ | ||
methods: { | ||
resolveRoute: proxyVueInstance(resolveRoute), | ||
localePath: proxyVueInstance(localePath), | ||
localeRoute: proxyVueInstance(localeRoute), | ||
localeLocation: proxyVueInstance(localeLocation), | ||
switchLocalePath: proxyVueInstance(switchLocalePath), | ||
getRouteBaseName: proxyVueInstance(getRouteBaseName), | ||
localeHead: proxyVueInstance(localeHead) | ||
} | ||
}); | ||
} | ||
if (app.unmount) { | ||
const unmountApp = app.unmount; | ||
app.unmount = () => { | ||
scope.stop(); | ||
unmountApp(); | ||
}; | ||
} | ||
}; | ||
return scope; | ||
} | ||
function extendComposer(composer, options) { | ||
const { locales, localeCodes, baseUrl } = options; | ||
const _locales = vueDemi.ref(locales); | ||
const _localeCodes = vueDemi.ref(localeCodes); | ||
composer.locales = vueDemi.computed(() => _locales.value); | ||
composer.localeCodes = vueDemi.computed(() => _localeCodes.value); | ||
composer.__baseUrl = resolveBaseUrl(baseUrl, {}); | ||
if (options.hooks && options.hooks.onExtendComposer) { | ||
options.hooks.onExtendComposer(composer); | ||
} | ||
} | ||
function extendExportedGlobal(exported, global, hook) { | ||
const properties = [ | ||
{ | ||
locales: { | ||
get() { | ||
return global.locales.value; | ||
} | ||
}, | ||
localeCodes: { | ||
get() { | ||
return global.localeCodes.value; | ||
} | ||
}, | ||
__baseUrl: { | ||
get() { | ||
return global.__baseUrl; | ||
} | ||
} | ||
} | ||
]; | ||
hook && properties.push(hook(global)); | ||
for (const property of properties) { | ||
for (const [key, descriptor] of Object.entries(property)) { | ||
Object.defineProperty(exported, key, descriptor); | ||
} | ||
} | ||
} | ||
function extendVueI18n(vueI18n, hook) { | ||
const composer = getComposer(vueI18n); | ||
const properties = [ | ||
{ | ||
locales: { | ||
get() { | ||
return composer.locales.value; | ||
} | ||
}, | ||
localeCodes: { | ||
get() { | ||
return composer.localeCodes.value; | ||
} | ||
}, | ||
__baseUrl: { | ||
get() { | ||
return composer.__baseUrl; | ||
} | ||
} | ||
} | ||
]; | ||
hook && properties.push(hook(composer)); | ||
for (const property of properties) { | ||
for (const [key, descriptor] of Object.entries(property)) { | ||
Object.defineProperty(vueI18n, key, descriptor); | ||
} | ||
} | ||
} | ||
function isPluginOptions(options) { | ||
return isObject(options) && "inject" in options && isBoolean(options.inject); | ||
} | ||
const GlobalOptionsRegistory = makeSymbol("vue-i18n-routing-gor"); | ||
function registerGlobalOptions(router, options) { | ||
const _options = router[GlobalOptionsRegistory]; | ||
if (_options) { | ||
warn("already registered global options"); | ||
} else { | ||
router[GlobalOptionsRegistory] = options; | ||
} | ||
} | ||
function getGlobalOptions(router) { | ||
var _a; | ||
return (_a = router[GlobalOptionsRegistory]) != null ? _a : {}; | ||
} | ||
function createRouter(i18n, options = {}) { | ||
const { | ||
version, | ||
defaultLocale, | ||
locales, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
defaultDirection, | ||
baseUrl, | ||
routes, | ||
routeOptionsResolver: optionsResolver | ||
} = asDefaultVueI18nRouterOptions(options); | ||
const normalizedLocaleCodes = getNormalizedLocales(locales); | ||
const localeCodes = normalizedLocaleCodes.map((l) => l.code); | ||
const getLocaleFromRoute = createLocaleFromRouteGetter(localeCodes, routesNameSeparator, defaultLocaleRouteNameSuffix); | ||
extendI18n(i18n, { locales: normalizedLocaleCodes, baseUrl, localeCodes }); | ||
const localizedRoutes = localizeRoutes(routes, { | ||
locales, | ||
defaultLocale, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
optionsResolver | ||
}); | ||
options.routes = localizedRoutes; | ||
const router = createVueRouter(options, version); | ||
registerGlobalOptions(router, { | ||
defaultLocale, | ||
localeCodes, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
defaultDirection | ||
}); | ||
router.beforeEach((to, from, next) => { | ||
const currentLocale = getLocale(i18n); | ||
const finalLocale = getLocaleFromRoute(to) || currentLocale || defaultLocale || ""; | ||
if (currentLocale !== finalLocale) { | ||
setLocale(i18n, finalLocale); | ||
} | ||
next(); | ||
}); | ||
return router; | ||
} | ||
function createVueRouter(options, version) { | ||
if (vueDemi.isVue3 && version === 4) { | ||
return VueRouter3.createRouter(options); | ||
} else if (vueDemi.isVue2 && version === 3) { | ||
return new VueRouter3__default.default(options); | ||
} else { | ||
throw new Error("TODO:"); | ||
} | ||
} | ||
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 (isObject(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]; | ||
} | ||
} | ||
} else if (isString(route)) { | ||
const matches = route.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, _i, _j; | ||
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.defaultDirection = (_h = options.defaultDirection) != null ? _h : DEFAULT_DETECTION_DIRECTION; | ||
options.baseUrl = (_i = options.baseUrl) != null ? _i : DEFAULT_BASE_URL; | ||
options.routes = (_j = options.routes) != null ? _j : []; | ||
return options; | ||
} | ||
function getI18nRoutingOptions(router, proxy, { | ||
defaultLocale = DEFAULT_LOCALE, | ||
defaultDirection = DEFAULT_DETECTION_DIRECTION, | ||
defaultLocaleRouteNameSuffix = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX, | ||
routesNameSeparator = DEFAULT_ROUTES_NAME_SEPARATOR, | ||
strategy = DEFAULT_STRATEGY, | ||
trailingSlash = DEFAULT_TRAILING_SLASH, | ||
localeCodes = [] | ||
} = {}) { | ||
const options = getGlobalOptions(router); | ||
return { | ||
defaultLocale: proxy.defaultLocale || options.defaultLocale || defaultLocale, | ||
defaultDirection: proxy.defaultDirection || options.defaultDirection || defaultDirection, | ||
defaultLocaleRouteNameSuffix: proxy.defaultLocaleRouteNameSuffix || options.defaultLocaleRouteNameSuffix || defaultLocaleRouteNameSuffix, | ||
routesNameSeparator: proxy.routesNameSeparator || options.routesNameSeparator || routesNameSeparator, | ||
strategy: proxy.strategy || options.strategy || strategy, | ||
trailingSlash: proxy.trailingSlash || options.trailingSlash || trailingSlash, | ||
localeCodes: proxy.localeCodes || options.localeCodes || localeCodes | ||
}; | ||
} | ||
const RESOLVED_PREFIXED = /* @__PURE__ */ new Set(["prefix_and_default", "prefix_except_default"]); | ||
function getRouteBaseName(givenRoute) { | ||
const router = this.router; | ||
const { routesNameSeparator } = getI18nRoutingOptions(router, this); | ||
const route = givenRoute != null ? vueDemi.isRef(givenRoute) ? vueDemi.unref(givenRoute) : givenRoute : this.route; | ||
if (route == null || !route.name) { | ||
return; | ||
} | ||
const name = getRouteName(route.name); | ||
return name.split(routesNameSeparator)[0]; | ||
} | ||
function localePath(route, locale) { | ||
const localizedRoute = resolveRoute.call(this, route, locale); | ||
return localizedRoute == null ? "" : vueDemi.isVue3 ? localizedRoute.redirectedFrom || localizedRoute.fullPath : localizedRoute.route.redirectedFrom || localizedRoute.route.fullPath; | ||
} | ||
function localeRoute(route, locale) { | ||
const resolved = resolveRoute.call(this, route, locale); | ||
return resolved == null ? void 0 : vueDemi.isVue3 ? resolved : resolved.route; | ||
} | ||
function localeLocation(route, locale) { | ||
const resolved = resolveRoute.call(this, route, locale); | ||
return resolved == null ? void 0 : vueDemi.isVue3 ? resolved : resolved.location; | ||
} | ||
function resolveRoute(route, locale) { | ||
const router = this.router; | ||
const i18n = this.i18n; | ||
const _locale = locale || getLocale(i18n); | ||
const { routesNameSeparator, defaultLocale, defaultLocaleRouteNameSuffix, strategy, trailingSlash } = getI18nRoutingOptions(router, this); | ||
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) { | ||
let _resolvedRoute = null; | ||
try { | ||
_resolvedRoute = router.resolve(localizedRoute); | ||
} catch { | ||
} | ||
const resolvedRoute = vueDemi.isVue3 ? _resolvedRoute : _resolvedRoute.route; | ||
const resolvedRouteName = getRouteBaseName.call(this, resolvedRoute); | ||
if (isString(resolvedRouteName)) { | ||
localizedRoute = { | ||
name: getLocaleRouteName(resolvedRouteName, _locale, { | ||
defaultLocale, | ||
strategy, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix | ||
}), | ||
params: resolvedRoute.params, | ||
query: resolvedRoute.query, | ||
hash: resolvedRoute.hash | ||
}; | ||
if (vueDemi.isVue3) { | ||
localizedRoute.state = resolvedRoute.state; | ||
} | ||
} else { | ||
const isDefaultLocale = _locale === defaultLocale; | ||
const isPrefixed = !(isDefaultLocale && RESOLVED_PREFIXED.has(strategy)) && !(strategy === "no_prefix"); | ||
if (isPrefixed) { | ||
localizedRoute.path = `/${_locale}${localizedRoute.path}`; | ||
} | ||
localizedRoute.path = trailingSlash ? withTrailingSlash(localizedRoute.path, true) : withoutTrailingSlash(localizedRoute.path, true); | ||
} | ||
} else { | ||
if (!localizedRoute.name && !localizedRoute.path) { | ||
localizedRoute.name = getRouteBaseName.call(this, this.route); | ||
} | ||
localizedRoute.name = getLocaleRouteName(localizedRoute.name, _locale, { | ||
defaultLocale, | ||
strategy, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix | ||
}); | ||
if (vueDemi.isVue2) { | ||
const { params } = localizedRoute; | ||
if (params && params["0"] === void 0 && params.pathMatch) { | ||
params["0"] = params.pathMatch; | ||
} | ||
} | ||
} | ||
try { | ||
const resolvedRoute = router.resolve(localizedRoute); | ||
if (vueDemi.isVue3 ? resolvedRoute.name : resolvedRoute.route.name) { | ||
return resolvedRoute; | ||
} | ||
return router.resolve(route); | ||
} catch (e) { | ||
if (vueDemi.isVue3 && e.type === 1) { | ||
return null; | ||
} else if (vueDemi.isVue2) { | ||
return null; | ||
} | ||
} | ||
} | ||
function switchLocalePath(locale) { | ||
const route = this.route; | ||
const name = getRouteBaseName.call(this, route); | ||
if (!name) { | ||
return ""; | ||
} | ||
const { params, ...routeCopy } = !vueDemi.isVue3 && vueDemi.isRef(route) ? route.value : route; | ||
const langSwitchParams = {}; | ||
const _baseRoute = { | ||
name, | ||
params: { | ||
...params, | ||
...langSwitchParams | ||
} | ||
}; | ||
if (vueDemi.isVue2) { | ||
_baseRoute.params[0] = params.pathMatch; | ||
} | ||
const baseRoute = assign({}, routeCopy, _baseRoute); | ||
const path = localePath.call(this, baseRoute, locale); | ||
return path; | ||
} | ||
function localeHead({ addDirAttribute = false, addSeoAttributes = false } = {}) { | ||
const router = this.router; | ||
const i18n = this.i18n; | ||
const { defaultDirection } = getI18nRoutingOptions(router, this); | ||
const metaObject = { | ||
htmlAttrs: {}, | ||
link: [], | ||
meta: [] | ||
}; | ||
if (i18n.locales == null || i18n.__baseUrl == null) { | ||
return metaObject; | ||
} | ||
const locale = getLocale(i18n); | ||
const locales = getLocales(i18n); | ||
const currentLocale = getNormalizedLocales(locales).find((l) => l.code === locale) || { | ||
code: locale | ||
}; | ||
const currentLocaleIso = currentLocale.iso; | ||
const currentLocaleDir = currentLocale.dir || defaultDirection; | ||
if (addDirAttribute) { | ||
metaObject.htmlAttrs.dir = currentLocaleDir; | ||
} | ||
if (addSeoAttributes && locale && i18n.locales) { | ||
if (currentLocaleIso) { | ||
metaObject.htmlAttrs.lang = currentLocaleIso; | ||
} | ||
addHreflangLinks.call(this, locales, i18n.__baseUrl, metaObject.link); | ||
addCanonicalLinks.call(this, i18n.__baseUrl, metaObject.link, addSeoAttributes); | ||
addCurrentOgLocale(currentLocale, currentLocaleIso, metaObject.meta); | ||
addAlternateOgLocales(locales, currentLocaleIso, metaObject.meta); | ||
} | ||
return metaObject; | ||
} | ||
function addHreflangLinks(locales, baseUrl, link) { | ||
const router = this.router; | ||
const { defaultLocale, strategy } = getI18nRoutingOptions(router, this); | ||
if (strategy === STRATEGIES.NO_PREFIX) { | ||
return; | ||
} | ||
const localeMap = /* @__PURE__ */ new Map(); | ||
for (const locale of locales) { | ||
const localeIso = locale.iso; | ||
if (!localeIso) { | ||
warn("Locale ISO code is required to generate alternate link"); | ||
continue; | ||
} | ||
const [language, region] = localeIso.split("-"); | ||
if (language && region && (locale.isCatchallLocale || !localeMap.has(language))) { | ||
localeMap.set(language, locale); | ||
} | ||
localeMap.set(localeIso, locale); | ||
} | ||
for (const [iso, mapLocale] of localeMap.entries()) { | ||
const localePath2 = switchLocalePath.call(this, mapLocale.code); | ||
if (localePath2) { | ||
link.push({ | ||
hid: `i18n-alt-${iso}`, | ||
rel: "alternate", | ||
href: toAbsoluteUrl(localePath2, baseUrl), | ||
hreflang: iso | ||
}); | ||
} | ||
} | ||
if (defaultLocale) { | ||
const localePath2 = switchLocalePath.call(this, defaultLocale); | ||
if (localePath2) { | ||
link.push({ | ||
hid: "i18n-xd", | ||
rel: "alternate", | ||
href: toAbsoluteUrl(localePath2, baseUrl), | ||
hreflang: "x-default" | ||
}); | ||
} | ||
} | ||
} | ||
function addCanonicalLinks(baseUrl, link, seoAttributesOptions) { | ||
const route = this.route; | ||
const currentRoute = localeRoute.call(this, { | ||
...route, | ||
name: getRouteBaseName.call(this, route) | ||
}); | ||
if (currentRoute) { | ||
let href = toAbsoluteUrl(currentRoute.path, baseUrl); | ||
const canonicalQueries = isObject(seoAttributesOptions) && seoAttributesOptions.canonicalQueries || []; | ||
if (canonicalQueries.length) { | ||
const currentRouteQueryParams = currentRoute.query; | ||
const params = new URLSearchParams(); | ||
for (const queryParamName of canonicalQueries) { | ||
if (queryParamName in currentRouteQueryParams) { | ||
const queryParamValue = currentRouteQueryParams[queryParamName]; | ||
if (isArray(queryParamValue)) { | ||
queryParamValue.forEach((v) => params.append(queryParamName, v || "")); | ||
} else { | ||
params.append(queryParamName, queryParamValue || ""); | ||
} | ||
} | ||
} | ||
const queryString = params.toString(); | ||
if (queryString) { | ||
href = `${href}?${queryString}`; | ||
} | ||
} | ||
link.push({ | ||
hid: "i18n-can", | ||
rel: "canonical", | ||
href | ||
}); | ||
} | ||
} | ||
function addCurrentOgLocale(currentLocale, currentLocaleIso, meta) { | ||
const hasCurrentLocaleAndIso = currentLocale && currentLocaleIso; | ||
if (!hasCurrentLocaleAndIso) { | ||
return; | ||
} | ||
meta.push({ | ||
hid: "i18n-og", | ||
property: "og:locale", | ||
content: hypenToUnderscore(currentLocaleIso) | ||
}); | ||
} | ||
function addAlternateOgLocales(locales, currentLocaleIso, meta) { | ||
const localesWithoutCurrent = locales.filter((locale) => { | ||
const localeIso = locale.iso; | ||
return localeIso && localeIso !== currentLocaleIso; | ||
}); | ||
if (localesWithoutCurrent.length) { | ||
const alternateLocales = localesWithoutCurrent.map((locale) => ({ | ||
hid: `i18n-og-alt-${locale.iso}`, | ||
property: "og:locale:alternate", | ||
content: hypenToUnderscore(locale.iso) | ||
})); | ||
meta.push(...alternateLocales); | ||
} | ||
} | ||
function hypenToUnderscore(str) { | ||
return (str || "").replace(/-/g, "_"); | ||
} | ||
function toAbsoluteUrl(urlOrPath, baseUrl) { | ||
if (urlOrPath.match(/^https?:\/\//)) { | ||
return urlOrPath; | ||
} | ||
return baseUrl + urlOrPath; | ||
} | ||
function proxyForComposable(options, target) { | ||
const { | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
strategy, | ||
defaultLocaleRouteNameSuffix, | ||
trailingSlash, | ||
routesNameSeparator | ||
} = options; | ||
return function(...args) { | ||
return Reflect.apply( | ||
target, | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
strategy, | ||
defaultLocaleRouteNameSuffix, | ||
trailingSlash, | ||
routesNameSeparator | ||
}, | ||
args | ||
); | ||
}; | ||
} | ||
function useRouteBaseName(givenRoute = VueRouter3.useRoute(), { router = VueRouter3.useRouter(), routesNameSeparator = void 0 } = {}) { | ||
const proxy = { | ||
router, | ||
route: givenRoute, | ||
routesNameSeparator | ||
}; | ||
return getRouteBaseName.call(proxy, givenRoute); | ||
} | ||
function useLocalePath({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ router, route, i18n, defaultLocale, defaultLocaleRouteNameSuffix, routesNameSeparator, strategy, trailingSlash }, | ||
localePath | ||
); | ||
} | ||
function useLocaleRoute({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
defaultLocaleRouteNameSuffix, | ||
routesNameSeparator, | ||
strategy, | ||
trailingSlash | ||
}, | ||
localeRoute | ||
); | ||
} | ||
function useLocaleLocation({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
defaultLocaleRouteNameSuffix, | ||
routesNameSeparator, | ||
strategy, | ||
trailingSlash | ||
}, | ||
localeLocation | ||
); | ||
} | ||
function useSwitchLocalePath({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
defaultLocaleRouteNameSuffix, | ||
routesNameSeparator, | ||
strategy, | ||
trailingSlash | ||
}, | ||
switchLocalePath | ||
); | ||
} | ||
function useLocaleHead({ | ||
addDirAttribute = false, | ||
addSeoAttributes = false, | ||
strategy = void 0, | ||
defaultLocale = void 0, | ||
route = VueRouter3.useRoute(), | ||
router = VueRouter3.useRouter(), | ||
i18n = vueI18nBridge.useI18n() | ||
} = {}) { | ||
const _router = router; | ||
const metaObject = vueDemi.ref({ | ||
htmlAttrs: {}, | ||
link: [], | ||
meta: [] | ||
}); | ||
function cleanMeta() { | ||
metaObject.value = { | ||
htmlAttrs: {}, | ||
link: [], | ||
meta: [] | ||
}; | ||
} | ||
function updateMeta(_route) { | ||
metaObject.value = Reflect.apply( | ||
localeHead, | ||
{ | ||
router, | ||
route: _route, | ||
i18n, | ||
defaultLocale, | ||
strategy | ||
}, | ||
[{ addDirAttribute, addSeoAttributes }] | ||
); | ||
} | ||
if (inBrowser) { | ||
if (vueDemi.isVue3) { | ||
const stop = vueDemi.watchEffect(() => { | ||
cleanMeta(); | ||
updateMeta(toRawRoute(_router.currentRoute)); | ||
}); | ||
vueDemi.onUnmounted(() => stop()); | ||
} else { | ||
const handler = _router.afterEach( | ||
(to, from) => { | ||
cleanMeta(); | ||
updateMeta(to); | ||
} | ||
); | ||
vueDemi.onUnmounted(() => handler()); | ||
updateMeta(route); | ||
} | ||
} else { | ||
updateMeta(toRawRoute(_router.currentRoute)); | ||
} | ||
return metaObject; | ||
} | ||
const VERSION = "0.1.5"; | ||
exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL; | ||
exports.DEFAULT_DETECTION_DIRECTION = DEFAULT_DETECTION_DIRECTION; | ||
exports.DEFAULT_LOCALE = DEFAULT_LOCALE; | ||
exports.DEFAULT_LOCALE_ROUTE_NAME_SUFFIX = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX; | ||
exports.DEFAULT_ROUTES_NAME_SEPARATOR = DEFAULT_ROUTES_NAME_SEPARATOR; | ||
exports.DEFAULT_STRATEGY = DEFAULT_STRATEGY; | ||
exports.DEFAULT_TRAILING_SLASH = DEFAULT_TRAILING_SLASH; | ||
exports.STRATEGIES = STRATEGIES; | ||
exports.VERSION = VERSION; | ||
exports.createLocaleFromRouteGetter = createLocaleFromRouteGetter; | ||
exports.createRouter = createRouter; | ||
exports.extendI18n = extendI18n; | ||
exports.findBrowserLocale = findBrowserLocale; | ||
exports.getGlobalOptions = getGlobalOptions; | ||
exports.getLocale = getLocale; | ||
exports.getLocaleCodes = getLocaleCodes; | ||
exports.getLocales = getLocales; | ||
exports.getLocalesRegex = getLocalesRegex; | ||
exports.getRouteBaseName = getRouteBaseName; | ||
exports.isComposer = isComposer; | ||
exports.isExportedGlobalComposer = isExportedGlobalComposer; | ||
exports.isI18nInstance = isI18nInstance; | ||
exports.isLegacyVueI18n = isLegacyVueI18n; | ||
exports.isVueI18n = isVueI18n; | ||
exports.localeHead = localeHead; | ||
exports.localeLocation = localeLocation; | ||
exports.localePath = localePath; | ||
exports.localeRoute = localeRoute; | ||
exports.localizeRoutes = localizeRoutes; | ||
exports.proxyVueInstance = proxyVueInstance; | ||
exports.registerGlobalOptions = registerGlobalOptions; | ||
exports.resolveBaseUrl = resolveBaseUrl; | ||
exports.resolveRoute = resolveRoute; | ||
exports.setLocale = setLocale; | ||
exports.switchLocalePath = switchLocalePath; | ||
exports.useLocaleHead = useLocaleHead; | ||
exports.useLocaleLocation = useLocaleLocation; | ||
exports.useLocalePath = useLocalePath; | ||
exports.useLocaleRoute = useLocaleRoute; | ||
exports.useRouteBaseName = useRouteBaseName; | ||
exports.useSwitchLocalePath = useSwitchLocalePath; | ||
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); | ||
return exports; | ||
}({}, VueDemi, VueRouter, VueI18n); |
@@ -1,5 +0,1042 @@ | ||
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const d=require("vue-demi"),p=require("@intlify/vue-router-bridge"),N=require("@intlify/vue-i18n-bridge"),ve=e=>e&&typeof e=="object"&&"default"in e?e:{default:e},Ae=ve(p),K={PREFIX:"prefix",PREFIX_EXCEPT_DEFAULT:"prefix_except_default",PREFIX_AND_DEFAULT:"prefix_and_default",NO_PREFIX:"no_prefix"},$="",B=K.PREFIX_EXCEPT_DEFAULT,j=!1,G="___",M="default",Z="ltr",ee="";/*! | ||
"use strict"; | ||
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); | ||
const vueDemi = require("vue-demi"); | ||
const VueRouter3 = require("@intlify/vue-router-bridge"); | ||
const vueI18nBridge = require("@intlify/vue-i18n-bridge"); | ||
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e }; | ||
const VueRouter3__default = /* @__PURE__ */ _interopDefaultLegacy(VueRouter3); | ||
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"; | ||
const DEFAULT_DETECTION_DIRECTION = "ltr"; | ||
const DEFAULT_BASE_URL = ""; | ||
/*! | ||
* shared v9.3.0-beta.3 | ||
* (c) 2022 kazuya kawaguchi | ||
* Released under the MIT License. | ||
*/process.env.NODE_ENV;const Ie=typeof Symbol=="function"&&typeof Symbol.toStringTag=="symbol",ye=e=>Ie?Symbol(e):e,w=Object.assign,Ce=Array.isArray,Te=e=>typeof e=="function",C=e=>typeof e=="string",xe=e=>typeof e=="boolean",be=e=>typeof e=="symbol",te=e=>e!==null&&typeof e=="object",Oe=/\/$|\/\?/;function Q(e="",t=!1){return t?Oe.test(e):e.endsWith("/")}function Pe(e="",t=!1){if(!t)return(Q(e)?e.slice(0,-1):e)||"/";if(!Q(e,!0))return e||"/";const[o,...n]=e.split("?");return(o.slice(0,-1)||"/")+(n.length?`?${n.join("?")}`:"")}function De(e="",t=!1){if(!t)return e.endsWith("/")?e:e+"/";if(Q(e,!0))return e||"/";const[o,...n]=e.split("?");return o+"/"+(n.length?`?${n.join("?")}`:"")}const Ue=typeof window<"u";function re(e,t){typeof console<"u"&&(console.warn("[vue-i18n-routing] "+e),t&&console.warn(t.stack))}function ie(e){e=e||[];const t=[];for(const o of e)C(o)?t.push({code:o}):t.push(o);return t}function O(e){return e!=null&&"global"in e&&"mode"in e}function P(e){return e!=null&&!("__composer"in e)&&d.isRef(e.locale)}function x(e){return e!=null&&"__composer"in e}function V(e){return e!=null&&!("__composer"in e)&&!d.isRef(e.locale)}function A(e){return e!=null&&("__VUE_I18N_BRIDGE__"in e||"_sync"in e)}function ue(e){return O(e)?P(e.global)?e.global:e.global.__composer:x(e)?e.__composer:e}function k(e){const t=O(e)?e.global:e;return P(t)?d.isVue2&&A(e)?e.locale:t.locale.value:(V(t)||x(t)||A(t),t.locale)}function fe(e){const t=O(e)?e.global:e;return P(t)?d.isVue2&&A(e)?e.locales:t.locales.value:(V(t)||x(t)||A(t),t.locales)}function Ne(e){const t=O(e)?e.global:e;return P(t)?d.isVue2&&A(e)?e.localeCodes:t.localeCodes.value:(V(t)||x(t)||A(t),t.localeCodes)}function de(e,t){const o=O(e)?e.global:e;if(P(o))d.isVue2&&A(e)?e.locale=t:o.locale.value=t;else if(V(o)||x(o)||A(o))o.locale=t;else throw new Error("TODO:")}function Ve(e,t,o){return e.replace(/\/+$/,"")+(t?"/":"")||(o?"":"/")}function se(e){return p.isVueRouter4,d.isRef(e)?e.value:e}function he(e){return C(e)?e:be(e)?e.toString():"(null)"}function ce(e,t,{defaultLocale:o,strategy:n,routesNameSeparator:a,defaultLocaleRouteNameSuffix:l}){let s=he(e)+(n==="no_prefix"?"":a+t);return t===o&&n==="prefix_and_default"&&(s+=a+l),s}function me(e,t){return Te(e)?e(t):e}function Fe(e,t){const o=[];for(const[n,a]of t.entries()){const l=e.find(s=>s.iso.toLowerCase()===a.toLowerCase());if(l){o.push({code:l.code,score:1-n/t.length});break}}for(const[n,a]of t.entries()){const l=a.split("-")[0].toLowerCase(),s=e.find(c=>c.iso.split("-")[0].toLowerCase()===l);if(s){o.push({code:s.code,score:.999-n/t.length});break}}return o}const we=Fe;function $e(e,t){return e.score===t.score?t.code.length-e.code.length:t.score-e.score}const Be=$e;function je(e,t,{matcher:o=we,comparer:n=Be}={}){const a=[];for(const s of e){const{code:c}=s,i=s.iso||c;a.push({code:c,iso:i})}const l=o(a,t);return l.length>1&&l.sort(n),l.length?l[0].code:""}function pe(e,{defaultLocale:t=$,strategy:o=B,trailingSlash:n=j,routesNameSeparator:a=G,defaultLocaleRouteNameSuffix:l=M,includeUprefixedFallback:s=!1,optionsResolver:c=void 0,locales:i=[]}={}){if(o==="no_prefix")return e;const u=i.map(r=>C(r)?r:r.code);function f(r,h,m=!1,v=!1){if(r.redirect&&(!r.component||!r.file))return[r];let R=null;if(c!=null&&(R=c(r,h),R==null))return[r];const g={locales:u,paths:{}};if(R!=null&&w(g,R),w(g,{locales:h}),g.locales.length>0&&R&&R.locales!=null&&R.locales.length>0){const _=[];for(const L of g.locales)R.locales.includes(L)&&_.push(L);g.locales=_}return g.locales.reduce((_,L)=>{const{name:D}=r;let{path:E}=r;const S={...r};D&&(S.name=`${D}${a}${L}`),r.children&&(S.children=r.children.reduce((y,W)=>[...y,...f(W,[L],!0,v)],[])),g.paths&&g.paths[L]&&(E=g.paths[L]);const b=L===t;if(b&&o==="prefix_and_default")if(m)m&&v&&D&&(S.name+=`${a}${l}`);else{const y={...S,path:E};if(D&&(y.name=`${S.name}${a}${l}`),r.children){y.children=[];for(const W of r.children)y.children=y.children.concat(f(W,[L],!0,!0))}_.push(y)}const le=m&&!E.startsWith("/"),ae=!le&&!(b&&o==="prefix_except_default");return ae&&(E=`/${L}${E}`),E&&(E=Ve(E,n,le)),ae&&b&&o==="prefix"&&s&&_.push({...r}),S.path=E,_.push(S),_},[])}return e.reduce((r,h)=>[...r,...f(h,u||[])],[])}function I(e){return function(){return Reflect.apply(e,{getRouteBaseName:this.getRouteBaseName,localePath:this.localePath,localeRoute:this.localeRoute,localeLocation:this.localeLocation,resolveRoute:this.resolveRoute,switchLocalePath:this.switchLocalePath,localeHead:this.localeHead,i18n:this.$i18n,route:this.$route,router:this.$router},arguments)}}function Le(e,{locales:t=[],localeCodes:o=[],baseUrl:n=ee,hooks:a={}}={}){const l=d.effectScope(),s=e.install;return e.install=(c,...i)=>{Reflect.apply(s,e,[c,...i]);const u=ue(e);l.run(()=>Ge(u,{locales:t,localeCodes:o,baseUrl:n,hooks:a})),x(e.global)&&ke(e.global,a.onExtendVueI18n);const f=c,r=e.mode==="composition"?d.isVue3?f.config.globalProperties.$i18n:e:d.isVue2?e:null;if(r&&Me(r,u,a.onExtendExportedGlobal),(ze(i[0])?i[0]:{inject:!0}).inject&&c.mixin({methods:{resolveRoute:I(F),localePath:I(H),localeRoute:I(X),localeLocation:I(oe),switchLocalePath:I(U),getRouteBaseName:I(T),localeHead:I(ne)}}),f.unmount){const m=f.unmount;f.unmount=()=>{l.stop(),m()}}},l}function Ge(e,t){const{locales:o,localeCodes:n,baseUrl:a}=t,l=d.ref(o),s=d.ref(n);e.locales=d.computed(()=>l.value),e.localeCodes=d.computed(()=>s.value),e.__baseUrl=me(a,{}),t.hooks&&t.hooks.onExtendComposer&&t.hooks.onExtendComposer(e)}function Me(e,t,o){const n=[{locales:{get(){return t.locales.value}},localeCodes:{get(){return t.localeCodes.value}},__baseUrl:{get(){return t.__baseUrl}}}];o&&n.push(o(t));for(const a of n)for(const[l,s]of Object.entries(a))Object.defineProperty(e,l,s)}function ke(e,t){const o=ue(e),n=[{locales:{get(){return o.locales.value}},localeCodes:{get(){return o.localeCodes.value}},__baseUrl:{get(){return o.__baseUrl}}}];t&&n.push(t(o));for(const a of n)for(const[l,s]of Object.entries(a))Object.defineProperty(e,l,s)}function ze(e){return te(e)&&"inject"in e&&xe(e.inject)}const Y=ye("vue-i18n-routing-gor");function ge(e,t){e[Y]?re("already registered global options"):e[Y]=t}function Re(e){var t;return(t=e[Y])!=null?t:{}}function He(e,t={}){const{version:o,defaultLocale:n,locales:a,strategy:l,trailingSlash:s,routesNameSeparator:c,defaultLocaleRouteNameSuffix:i,defaultDirection:u,baseUrl:f,routes:r,routeOptionsResolver:h}=qe(t),m=ie(a),v=m.map(L=>L.code),R=Ee(v,c,i);Le(e,{locales:m,baseUrl:f,localeCodes:v});const g=pe(r,{locales:a,defaultLocale:n,strategy:l,trailingSlash:s,routesNameSeparator:c,defaultLocaleRouteNameSuffix:i,optionsResolver:h});t.routes=g;const _=Xe(t,o);return ge(_,{defaultLocale:n,localeCodes:v,strategy:l,trailingSlash:s,routesNameSeparator:c,defaultLocaleRouteNameSuffix:i,defaultDirection:u}),_.beforeEach((L,D,E)=>{const S=k(e),b=R(L)||S||n||"";S!==b&&de(e,b),E()}),_}function Xe(e,t){if(d.isVue3&&t===4)return p.createRouter(e);if(d.isVue2&&t===3)return new Ae.default(e);throw new Error("TODO:")}function _e(e){return new RegExp(`^/(${e.join("|")})(?:/|$)`,"i")}function Ee(e,t,o){const n=`(${e.join("|")})`,a=`(?:${t}${o})?`,l=new RegExp(`${t}${n}${a}$`,"i"),s=_e(e);return i=>{if(te(i)){if(i.name){const f=(C(i.name)?i.name:i.name.toString()).match(l);if(f&&f.length>1)return f[1]}else if(i.path){const u=i.path.match(s);if(u&&u.length>1)return u[1]}}else if(C(i)){const u=i.match(s);if(u&&u.length>1)return u[1]}return""}}function qe(e){var t,o,n,a,l,s,c,i,u,f;return e.version=(t=e.version)!=null?t:4,e.defaultLocale=(o=e.defaultLocale)!=null?o:$,e.strategy=(n=e.strategy)!=null?n:B,e.trailingSlash=(a=e.trailingSlash)!=null?a:j,e.routesNameSeparator=(l=e.routesNameSeparator)!=null?l:G,e.defaultLocaleRouteNameSuffix=(s=e.defaultLocaleRouteNameSuffix)!=null?s:M,e.locales=(c=e.locales)!=null?c:[],e.defaultDirection=(i=e.defaultDirection)!=null?i:Z,e.baseUrl=(u=e.baseUrl)!=null?u:ee,e.routes=(f=e.routes)!=null?f:[],e}function z(e,t,{defaultLocale:o=$,defaultDirection:n=Z,defaultLocaleRouteNameSuffix:a=M,routesNameSeparator:l=G,strategy:s=B,trailingSlash:c=j,localeCodes:i=[]}={}){const u=Re(e);return{defaultLocale:t.defaultLocale||u.defaultLocale||o,defaultDirection:t.defaultDirection||u.defaultDirection||n,defaultLocaleRouteNameSuffix:t.defaultLocaleRouteNameSuffix||u.defaultLocaleRouteNameSuffix||a,routesNameSeparator:t.routesNameSeparator||u.routesNameSeparator||l,strategy:t.strategy||u.strategy||s,trailingSlash:t.trailingSlash||u.trailingSlash||c,localeCodes:t.localeCodes||u.localeCodes||i}}const We=new Set(["prefix_and_default","prefix_except_default"]);function T(e){const t=this.router,{routesNameSeparator:o}=z(t,this),n=e!=null?d.isRef(e)?d.unref(e):e:this.route;return n==null||!n.name?void 0:he(n.name).split(o)[0]}function H(e,t){const o=F.call(this,e,t);return o==null?"":d.isVue3?o.redirectedFrom||o.fullPath:o.route.redirectedFrom||o.route.fullPath}function X(e,t){const o=F.call(this,e,t);return o==null?void 0:d.isVue3?o:o.route}function oe(e,t){const o=F.call(this,e,t);return o==null?void 0:d.isVue3?o:o.location}function F(e,t){const o=this.router,n=this.i18n,a=t||k(n),{routesNameSeparator:l,defaultLocale:s,defaultLocaleRouteNameSuffix:c,strategy:i,trailingSlash:u}=z(o,this);let f=e;C(e)&&(f[0]==="/"?f={path:e}:f={name:e});let r=w({},f);if(r.path&&!r.name){let h=null;try{h=o.resolve(r)}catch{}const m=d.isVue3?h:h.route,v=T.call(this,m);C(v)?(r={name:ce(v,a,{defaultLocale:s,strategy:i,routesNameSeparator:l,defaultLocaleRouteNameSuffix:c}),params:m.params,query:m.query,hash:m.hash},d.isVue3&&(r.state=m.state)):(!(a===s&&We.has(i))&&i!=="no_prefix"&&(r.path=`/${a}${r.path}`),r.path=u?De(r.path,!0):Pe(r.path,!0))}else if(!r.name&&!r.path&&(r.name=T.call(this,this.route)),r.name=ce(r.name,a,{defaultLocale:s,strategy:i,routesNameSeparator:l,defaultLocaleRouteNameSuffix:c}),d.isVue2){const{params:h}=r;h&&h[0]===void 0&&h.pathMatch&&(h[0]=h.pathMatch)}try{const h=o.resolve(r);return(d.isVue3?h.name:h.route.name)?h:o.resolve(e)}catch(h){if(d.isVue3&&h.type===1)return null;if(d.isVue2)return null}}function U(e){const t=this.route,o=T.call(this,t);if(!o)return"";const{params:n,...a}=!d.isVue3&&d.isRef(t)?t.value:t,s={name:o,params:{...n,...{}}};d.isVue2&&(s.params[0]=n.pathMatch);const c=w({},a,s);return H.call(this,c,e)}function ne({addDirAttribute:e=!1,addSeoAttributes:t=!1}={}){const o=this.router,n=this.i18n,{defaultDirection:a}=z(o,this),l={htmlAttrs:{},link:[],meta:[]};if(n.locales==null||n.__baseUrl==null)return l;const s=k(n),c=fe(n),i=ie(c).find(r=>r.code===s)||{code:s},u=i.iso,f=i.dir||a;return e&&(l.htmlAttrs.dir=f),t&&s&&n.locales&&(u&&(l.htmlAttrs.lang=u),Qe.call(this,c,n.__baseUrl,l.link),Ye.call(this,n.__baseUrl,l.link,t),Je(i,u,l.meta),Ke(c,u,l.meta)),l}function Qe(e,t,o){const n=this.router,{defaultLocale:a,strategy:l}=z(n,this);if(l===K.NO_PREFIX)return;const s=new Map;for(const c of e){const i=c.iso;if(!i){re("Locale ISO code is required to generate alternate link");continue}const[u,f]=i.split("-");u&&f&&(c.isCatchallLocale||!s.has(u))&&s.set(u,c),s.set(i,c)}for(const[c,i]of s.entries()){const u=U.call(this,i.code);u&&o.push({hid:`i18n-alt-${c}`,rel:"alternate",href:J(u,t),hreflang:c})}if(a){const c=U.call(this,a);c&&o.push({hid:"i18n-xd",rel:"alternate",href:J(c,t),hreflang:"x-default"})}}function Ye(e,t,o){const n=this.route,a=X.call(this,{...n,name:T.call(this,n)});if(a){let l=J(a.path,e);const s=te(o)&&o.canonicalQueries||[];if(s.length){const c=a.query,i=new URLSearchParams;for(const f of s)if(f in c){const r=c[f];Ce(r)?r.forEach(h=>i.append(f,h||"")):i.append(f,r||"")}const u=i.toString();u&&(l=`${l}?${u}`)}t.push({hid:"i18n-can",rel:"canonical",href:l})}}function Je(e,t,o){!(e&&t)||o.push({hid:"i18n-og",property:"og:locale",content:Se(t)})}function Ke(e,t,o){const n=e.filter(a=>{const l=a.iso;return l&&l!==t});if(n.length){const a=n.map(l=>({hid:`i18n-og-alt-${l.iso}`,property:"og:locale:alternate",content:Se(l.iso)}));o.push(...a)}}function Se(e){return(e||"").replace(/-/g,"_")}function J(e,t){return e.match(/^https?:\/\//)?e:t+e}function q(e,t){const{router:o,route:n,i18n:a,defaultLocale:l,strategy:s,defaultLocaleRouteNameSuffix:c,trailingSlash:i,routesNameSeparator:u}=e;return function(...f){return Reflect.apply(t,{router:o,route:n,i18n:a,defaultLocale:l,strategy:s,defaultLocaleRouteNameSuffix:c,trailingSlash:i,routesNameSeparator:u},f)}}function Ze(e=p.useRoute(),{router:t=p.useRouter(),routesNameSeparator:o=void 0}={}){const n={router:t,route:e,routesNameSeparator:o};return T.call(n,e)}function et({router:e=p.useRouter(),route:t=p.useRoute(),i18n:o=N.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:s=void 0,trailingSlash:c=void 0}={}){return q({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:s,trailingSlash:c},H)}function tt({router:e=p.useRouter(),route:t=p.useRoute(),i18n:o=N.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:s=void 0,trailingSlash:c=void 0}={}){return q({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:s,trailingSlash:c},X)}function ot({router:e=p.useRouter(),route:t=p.useRoute(),i18n:o=N.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:s=void 0,trailingSlash:c=void 0}={}){return q({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:s,trailingSlash:c},oe)}function nt({router:e=p.useRouter(),route:t=p.useRoute(),i18n:o=N.useI18n(),defaultLocale:n=void 0,defaultLocaleRouteNameSuffix:a=void 0,routesNameSeparator:l=void 0,strategy:s=void 0,trailingSlash:c=void 0}={}){return q({router:e,route:t,i18n:o,defaultLocale:n,defaultLocaleRouteNameSuffix:a,routesNameSeparator:l,strategy:s,trailingSlash:c},U)}function lt({addDirAttribute:e=!1,addSeoAttributes:t=!1,strategy:o=void 0,defaultLocale:n=void 0,route:a=p.useRoute(),router:l=p.useRouter(),i18n:s=N.useI18n()}={}){const c=l,i=d.ref({htmlAttrs:{},link:[],meta:[]});function u(){i.value={htmlAttrs:{},link:[],meta:[]}}function f(r){i.value=Reflect.apply(ne,{router:l,route:r,i18n:s,defaultLocale:n,strategy:o},[{addDirAttribute:e,addSeoAttributes:t}])}if(Ue)if(d.isVue3){const r=d.watchEffect(()=>{u(),f(se(c.currentRoute))});d.onUnmounted(()=>r())}else{const r=c.afterEach((h,m)=>{u(),f(h)});d.onUnmounted(()=>r()),f(a)}else f(se(c.currentRoute));return i}const at="0.1.4";exports.DEFAULT_BASE_URL=ee;exports.DEFAULT_DETECTION_DIRECTION=Z;exports.DEFAULT_LOCALE=$;exports.DEFAULT_LOCALE_ROUTE_NAME_SUFFIX=M;exports.DEFAULT_ROUTES_NAME_SEPARATOR=G;exports.DEFAULT_STRATEGY=B;exports.DEFAULT_TRAILING_SLASH=j;exports.STRATEGIES=K;exports.VERSION=at;exports.createLocaleFromRouteGetter=Ee;exports.createRouter=He;exports.extendI18n=Le;exports.findBrowserLocale=je;exports.getGlobalOptions=Re;exports.getLocale=k;exports.getLocaleCodes=Ne;exports.getLocales=fe;exports.getLocalesRegex=_e;exports.getRouteBaseName=T;exports.isComposer=P;exports.isExportedGlobalComposer=V;exports.isI18nInstance=O;exports.isLegacyVueI18n=A;exports.isVueI18n=x;exports.localeHead=ne;exports.localeLocation=oe;exports.localePath=H;exports.localeRoute=X;exports.localizeRoutes=pe;exports.proxyVueInstance=I;exports.registerGlobalOptions=ge;exports.resolveBaseUrl=me;exports.resolveRoute=F;exports.setLocale=de;exports.switchLocalePath=U;exports.useLocaleHead=lt;exports.useLocaleLocation=ot;exports.useLocalePath=et;exports.useLocaleRoute=tt;exports.useRouteBaseName=Ze;exports.useSwitchLocalePath=nt; | ||
*/ | ||
const hasSymbol = typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol"; | ||
const makeSymbol = (name) => hasSymbol ? Symbol(name) : name; | ||
const assign = Object.assign; | ||
const isArray = Array.isArray; | ||
const isFunction = (val) => typeof val === "function"; | ||
const isString = (val) => typeof val === "string"; | ||
const isBoolean = (val) => typeof val === "boolean"; | ||
const isSymbol = (val) => typeof val === "symbol"; | ||
const isObject = (val) => val !== null && typeof val === "object"; | ||
const TRAILING_SLASH_RE = /\/$|\/\?/; | ||
function hasTrailingSlash(input = "", queryParams = false) { | ||
if (!queryParams) { | ||
return input.endsWith("/"); | ||
} | ||
return TRAILING_SLASH_RE.test(input); | ||
} | ||
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("?")}` : ""); | ||
} | ||
const inBrowser = typeof window !== "undefined"; | ||
function warn(msg, err) { | ||
if (typeof console !== "undefined") { | ||
console.warn(`[vue-i18n-routing] ` + msg); | ||
if (err) { | ||
console.warn(err.stack); | ||
} | ||
} | ||
} | ||
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 isI18nInstance(i18n) { | ||
return i18n != null && "global" in i18n && "mode" in i18n; | ||
} | ||
function isComposer(target) { | ||
return target != null && !("__composer" in target) && vueDemi.isRef(target.locale); | ||
} | ||
function isVueI18n(target) { | ||
return target != null && "__composer" in target; | ||
} | ||
function isExportedGlobalComposer(target) { | ||
return target != null && !("__composer" in target) && !vueDemi.isRef(target.locale); | ||
} | ||
function isLegacyVueI18n(target) { | ||
return target != null && ("__VUE_I18N_BRIDGE__" in target || "_sync" in target); | ||
} | ||
function getComposer(i18n) { | ||
return isI18nInstance(i18n) ? isComposer(i18n.global) ? i18n.global : i18n.global.__composer : isVueI18n(i18n) ? i18n.__composer : i18n; | ||
} | ||
function getLocale(i18n) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
return isComposer(target) ? vueDemi.isVue2 && isLegacyVueI18n(i18n) ? i18n.locale : target.locale.value : isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target) ? target.locale : target.locale; | ||
} | ||
function getLocales(i18n) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
return isComposer(target) ? vueDemi.isVue2 && isLegacyVueI18n(i18n) ? i18n.locales : target.locales.value : isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target) ? target.locales : target.locales; | ||
} | ||
function getLocaleCodes(i18n) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
return isComposer(target) ? vueDemi.isVue2 && isLegacyVueI18n(i18n) ? i18n.localeCodes : target.localeCodes.value : isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target) ? target.localeCodes : target.localeCodes; | ||
} | ||
function setLocale(i18n, locale) { | ||
const target = isI18nInstance(i18n) ? i18n.global : i18n; | ||
if (isComposer(target)) { | ||
if (vueDemi.isVue2 && isLegacyVueI18n(i18n)) { | ||
i18n.locale = locale; | ||
} else { | ||
target.locale.value = locale; | ||
} | ||
} else if (isExportedGlobalComposer(target) || isVueI18n(target) || isLegacyVueI18n(target)) { | ||
target.locale = locale; | ||
} else { | ||
throw new Error("TODO:"); | ||
} | ||
} | ||
function adjustRoutePathForTrailingSlash(pagePath, trailingSlash, isChildWithRelativePath) { | ||
return pagePath.replace(/\/+$/, "") + (trailingSlash ? "/" : "") || (isChildWithRelativePath ? "" : "/"); | ||
} | ||
function toRawRoute(maybeRoute) { | ||
return VueRouter3.isVueRouter4 ? vueDemi.isRef(maybeRoute) ? maybeRoute.value : maybeRoute : vueDemi.isRef(maybeRoute) ? maybeRoute.value : maybeRoute; | ||
} | ||
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; | ||
} | ||
return name; | ||
} | ||
function resolveBaseUrl(baseUrl, context) { | ||
if (isFunction(baseUrl)) { | ||
return baseUrl(context); | ||
} | ||
return baseUrl; | ||
} | ||
function matchBrowserLocale(locales, browserLocales) { | ||
const matchedLocales = []; | ||
for (const [index, browserCode] of browserLocales.entries()) { | ||
const matchedLocale = locales.find((l) => l.iso.toLowerCase() === browserCode.toLowerCase()); | ||
if (matchedLocale) { | ||
matchedLocales.push({ code: matchedLocale.code, score: 1 - index / browserLocales.length }); | ||
break; | ||
} | ||
} | ||
for (const [index, browserCode] of browserLocales.entries()) { | ||
const languageCode = browserCode.split("-")[0].toLowerCase(); | ||
const matchedLocale = locales.find((l) => l.iso.split("-")[0].toLowerCase() === languageCode); | ||
if (matchedLocale) { | ||
matchedLocales.push({ code: matchedLocale.code, score: 0.999 - index / browserLocales.length }); | ||
break; | ||
} | ||
} | ||
return matchedLocales; | ||
} | ||
const DefaultBrowserLocaleMatcher = matchBrowserLocale; | ||
function compareBrowserLocale(a, b) { | ||
if (a.score === b.score) { | ||
return b.code.length - a.code.length; | ||
} | ||
return b.score - a.score; | ||
} | ||
const DefaultBrowerLocaleComparer = compareBrowserLocale; | ||
function findBrowserLocale(locales, browserLocales, { matcher = DefaultBrowserLocaleMatcher, comparer = DefaultBrowerLocaleComparer } = {}) { | ||
const normalizedLocales = []; | ||
for (const l of locales) { | ||
const { code } = l; | ||
const iso = l.iso || code; | ||
normalizedLocales.push({ code, iso }); | ||
} | ||
const matchedLocales = matcher(normalizedLocales, browserLocales); | ||
if (matchedLocales.length > 1) { | ||
matchedLocales.sort(comparer); | ||
} | ||
return matchedLocales.length ? matchedLocales[0].code : ""; | ||
} | ||
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, | ||
optionsResolver = void 0, | ||
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]; | ||
} | ||
let routeOptions = null; | ||
if (optionsResolver != null) { | ||
routeOptions = optionsResolver(route, allowedLocaleCodes); | ||
if (routeOptions == null) { | ||
return [route]; | ||
} | ||
} | ||
const componentOptions = { | ||
locales: _localeCodes, | ||
paths: {} | ||
}; | ||
if (routeOptions != null) { | ||
assign(componentOptions, routeOptions); | ||
} | ||
assign(componentOptions, { locales: allowedLocaleCodes }); | ||
if (componentOptions.locales.length > 0 && routeOptions && routeOptions.locales != null && routeOptions.locales.length > 0) { | ||
const filteredLocales = []; | ||
for (const locale of componentOptions.locales) { | ||
if (routeOptions.locales.includes(locale)) { | ||
filteredLocales.push(locale); | ||
} | ||
} | ||
componentOptions.locales = filteredLocales; | ||
} | ||
return componentOptions.locales.reduce((_routes, locale) => { | ||
const { name } = route; | ||
let { path } = route; | ||
const localizedRoute = { ...route }; | ||
if (name) { | ||
localizedRoute.name = `${name}${routesNameSeparator}${locale}`; | ||
} | ||
if (route.children) { | ||
localizedRoute.children = route.children.reduce( | ||
(children, child) => [...children, ...makeLocalizedRoutes(child, [locale], true, isExtraPageTree)], | ||
[] | ||
); | ||
} | ||
if (componentOptions.paths && componentOptions.paths[locale]) { | ||
path = componentOptions.paths[locale]; | ||
} | ||
const isDefaultLocale = locale === defaultLocale; | ||
if (isDefaultLocale && strategy === "prefix_and_default") { | ||
if (!isChild) { | ||
const defaultRoute = { ...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({ ...route }); | ||
} | ||
localizedRoute.path = path; | ||
_routes.push(localizedRoute); | ||
return _routes; | ||
}, []); | ||
} | ||
return routes.reduce( | ||
(localized, route) => [...localized, ...makeLocalizedRoutes(route, _localeCodes || [])], | ||
[] | ||
); | ||
} | ||
function proxyVueInstance(target) { | ||
return function() { | ||
return Reflect.apply( | ||
target, | ||
{ | ||
getRouteBaseName: this.getRouteBaseName, | ||
localePath: this.localePath, | ||
localeRoute: this.localeRoute, | ||
localeLocation: this.localeLocation, | ||
resolveRoute: this.resolveRoute, | ||
switchLocalePath: this.switchLocalePath, | ||
localeHead: this.localeHead, | ||
i18n: this.$i18n, | ||
route: this.$route, | ||
router: this.$router | ||
}, | ||
arguments | ||
); | ||
}; | ||
} | ||
function extendI18n(i18n, { locales = [], localeCodes = [], baseUrl = DEFAULT_BASE_URL, hooks = {} } = {}) { | ||
const scope = vueDemi.effectScope(); | ||
const orgInstall = i18n.install; | ||
i18n.install = (vue, ...options) => { | ||
Reflect.apply(orgInstall, i18n, [vue, ...options]); | ||
const composer = getComposer(i18n); | ||
scope.run(() => extendComposer(composer, { locales, localeCodes, baseUrl, hooks })); | ||
if (isVueI18n(i18n.global)) { | ||
extendVueI18n(i18n.global, hooks.onExtendVueI18n); | ||
} | ||
const app = vue; | ||
const exported = i18n.mode === "composition" ? vueDemi.isVue3 ? app.config.globalProperties.$i18n : i18n : vueDemi.isVue2 ? i18n : null; | ||
if (exported) { | ||
extendExportedGlobal(exported, composer, hooks.onExtendExportedGlobal); | ||
} | ||
const pluginOptions = isPluginOptions(options[0]) ? options[0] : { inject: true }; | ||
if (pluginOptions.inject) { | ||
vue.mixin({ | ||
methods: { | ||
resolveRoute: proxyVueInstance(resolveRoute), | ||
localePath: proxyVueInstance(localePath), | ||
localeRoute: proxyVueInstance(localeRoute), | ||
localeLocation: proxyVueInstance(localeLocation), | ||
switchLocalePath: proxyVueInstance(switchLocalePath), | ||
getRouteBaseName: proxyVueInstance(getRouteBaseName), | ||
localeHead: proxyVueInstance(localeHead) | ||
} | ||
}); | ||
} | ||
if (app.unmount) { | ||
const unmountApp = app.unmount; | ||
app.unmount = () => { | ||
scope.stop(); | ||
unmountApp(); | ||
}; | ||
} | ||
}; | ||
return scope; | ||
} | ||
function extendComposer(composer, options) { | ||
const { locales, localeCodes, baseUrl } = options; | ||
const _locales = vueDemi.ref(locales); | ||
const _localeCodes = vueDemi.ref(localeCodes); | ||
composer.locales = vueDemi.computed(() => _locales.value); | ||
composer.localeCodes = vueDemi.computed(() => _localeCodes.value); | ||
composer.__baseUrl = resolveBaseUrl(baseUrl, {}); | ||
if (options.hooks && options.hooks.onExtendComposer) { | ||
options.hooks.onExtendComposer(composer); | ||
} | ||
} | ||
function extendExportedGlobal(exported, global, hook) { | ||
const properties = [ | ||
{ | ||
locales: { | ||
get() { | ||
return global.locales.value; | ||
} | ||
}, | ||
localeCodes: { | ||
get() { | ||
return global.localeCodes.value; | ||
} | ||
}, | ||
__baseUrl: { | ||
get() { | ||
return global.__baseUrl; | ||
} | ||
} | ||
} | ||
]; | ||
hook && properties.push(hook(global)); | ||
for (const property of properties) { | ||
for (const [key, descriptor] of Object.entries(property)) { | ||
Object.defineProperty(exported, key, descriptor); | ||
} | ||
} | ||
} | ||
function extendVueI18n(vueI18n, hook) { | ||
const composer = getComposer(vueI18n); | ||
const properties = [ | ||
{ | ||
locales: { | ||
get() { | ||
return composer.locales.value; | ||
} | ||
}, | ||
localeCodes: { | ||
get() { | ||
return composer.localeCodes.value; | ||
} | ||
}, | ||
__baseUrl: { | ||
get() { | ||
return composer.__baseUrl; | ||
} | ||
} | ||
} | ||
]; | ||
hook && properties.push(hook(composer)); | ||
for (const property of properties) { | ||
for (const [key, descriptor] of Object.entries(property)) { | ||
Object.defineProperty(vueI18n, key, descriptor); | ||
} | ||
} | ||
} | ||
function isPluginOptions(options) { | ||
return isObject(options) && "inject" in options && isBoolean(options.inject); | ||
} | ||
const GlobalOptionsRegistory = makeSymbol("vue-i18n-routing-gor"); | ||
function registerGlobalOptions(router, options) { | ||
const _options = router[GlobalOptionsRegistory]; | ||
if (_options) { | ||
warn("already registered global options"); | ||
} else { | ||
router[GlobalOptionsRegistory] = options; | ||
} | ||
} | ||
function getGlobalOptions(router) { | ||
var _a; | ||
return (_a = router[GlobalOptionsRegistory]) != null ? _a : {}; | ||
} | ||
function createRouter(i18n, options = {}) { | ||
const { | ||
version, | ||
defaultLocale, | ||
locales, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
defaultDirection, | ||
baseUrl, | ||
routes, | ||
routeOptionsResolver: optionsResolver | ||
} = asDefaultVueI18nRouterOptions(options); | ||
const normalizedLocaleCodes = getNormalizedLocales(locales); | ||
const localeCodes = normalizedLocaleCodes.map((l) => l.code); | ||
const getLocaleFromRoute = createLocaleFromRouteGetter(localeCodes, routesNameSeparator, defaultLocaleRouteNameSuffix); | ||
extendI18n(i18n, { locales: normalizedLocaleCodes, baseUrl, localeCodes }); | ||
const localizedRoutes = localizeRoutes(routes, { | ||
locales, | ||
defaultLocale, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
optionsResolver | ||
}); | ||
options.routes = localizedRoutes; | ||
const router = createVueRouter(options, version); | ||
registerGlobalOptions(router, { | ||
defaultLocale, | ||
localeCodes, | ||
strategy, | ||
trailingSlash, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix, | ||
defaultDirection | ||
}); | ||
router.beforeEach((to, from, next) => { | ||
const currentLocale = getLocale(i18n); | ||
const finalLocale = getLocaleFromRoute(to) || currentLocale || defaultLocale || ""; | ||
if (currentLocale !== finalLocale) { | ||
setLocale(i18n, finalLocale); | ||
} | ||
next(); | ||
}); | ||
return router; | ||
} | ||
function createVueRouter(options, version) { | ||
if (vueDemi.isVue3 && version === 4) { | ||
return VueRouter3.createRouter(options); | ||
} else if (vueDemi.isVue2 && version === 3) { | ||
return new VueRouter3__default.default(options); | ||
} else { | ||
throw new Error("TODO:"); | ||
} | ||
} | ||
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 (isObject(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]; | ||
} | ||
} | ||
} else if (isString(route)) { | ||
const matches = route.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, _i, _j; | ||
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.defaultDirection = (_h = options.defaultDirection) != null ? _h : DEFAULT_DETECTION_DIRECTION; | ||
options.baseUrl = (_i = options.baseUrl) != null ? _i : DEFAULT_BASE_URL; | ||
options.routes = (_j = options.routes) != null ? _j : []; | ||
return options; | ||
} | ||
function getI18nRoutingOptions(router, proxy, { | ||
defaultLocale = DEFAULT_LOCALE, | ||
defaultDirection = DEFAULT_DETECTION_DIRECTION, | ||
defaultLocaleRouteNameSuffix = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX, | ||
routesNameSeparator = DEFAULT_ROUTES_NAME_SEPARATOR, | ||
strategy = DEFAULT_STRATEGY, | ||
trailingSlash = DEFAULT_TRAILING_SLASH, | ||
localeCodes = [] | ||
} = {}) { | ||
const options = getGlobalOptions(router); | ||
return { | ||
defaultLocale: proxy.defaultLocale || options.defaultLocale || defaultLocale, | ||
defaultDirection: proxy.defaultDirection || options.defaultDirection || defaultDirection, | ||
defaultLocaleRouteNameSuffix: proxy.defaultLocaleRouteNameSuffix || options.defaultLocaleRouteNameSuffix || defaultLocaleRouteNameSuffix, | ||
routesNameSeparator: proxy.routesNameSeparator || options.routesNameSeparator || routesNameSeparator, | ||
strategy: proxy.strategy || options.strategy || strategy, | ||
trailingSlash: proxy.trailingSlash || options.trailingSlash || trailingSlash, | ||
localeCodes: proxy.localeCodes || options.localeCodes || localeCodes | ||
}; | ||
} | ||
const RESOLVED_PREFIXED = /* @__PURE__ */ new Set(["prefix_and_default", "prefix_except_default"]); | ||
function getRouteBaseName(givenRoute) { | ||
const router = this.router; | ||
const { routesNameSeparator } = getI18nRoutingOptions(router, this); | ||
const route = givenRoute != null ? vueDemi.isRef(givenRoute) ? vueDemi.unref(givenRoute) : givenRoute : this.route; | ||
if (route == null || !route.name) { | ||
return; | ||
} | ||
const name = getRouteName(route.name); | ||
return name.split(routesNameSeparator)[0]; | ||
} | ||
function localePath(route, locale) { | ||
const localizedRoute = resolveRoute.call(this, route, locale); | ||
return localizedRoute == null ? "" : vueDemi.isVue3 ? localizedRoute.redirectedFrom || localizedRoute.fullPath : localizedRoute.route.redirectedFrom || localizedRoute.route.fullPath; | ||
} | ||
function localeRoute(route, locale) { | ||
const resolved = resolveRoute.call(this, route, locale); | ||
return resolved == null ? void 0 : vueDemi.isVue3 ? resolved : resolved.route; | ||
} | ||
function localeLocation(route, locale) { | ||
const resolved = resolveRoute.call(this, route, locale); | ||
return resolved == null ? void 0 : vueDemi.isVue3 ? resolved : resolved.location; | ||
} | ||
function resolveRoute(route, locale) { | ||
const router = this.router; | ||
const i18n = this.i18n; | ||
const _locale = locale || getLocale(i18n); | ||
const { routesNameSeparator, defaultLocale, defaultLocaleRouteNameSuffix, strategy, trailingSlash } = getI18nRoutingOptions(router, this); | ||
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) { | ||
let _resolvedRoute = null; | ||
try { | ||
_resolvedRoute = router.resolve(localizedRoute); | ||
} catch { | ||
} | ||
const resolvedRoute = vueDemi.isVue3 ? _resolvedRoute : _resolvedRoute.route; | ||
const resolvedRouteName = getRouteBaseName.call(this, resolvedRoute); | ||
if (isString(resolvedRouteName)) { | ||
localizedRoute = { | ||
name: getLocaleRouteName(resolvedRouteName, _locale, { | ||
defaultLocale, | ||
strategy, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix | ||
}), | ||
params: resolvedRoute.params, | ||
query: resolvedRoute.query, | ||
hash: resolvedRoute.hash | ||
}; | ||
if (vueDemi.isVue3) { | ||
localizedRoute.state = resolvedRoute.state; | ||
} | ||
} else { | ||
const isDefaultLocale = _locale === defaultLocale; | ||
const isPrefixed = !(isDefaultLocale && RESOLVED_PREFIXED.has(strategy)) && !(strategy === "no_prefix"); | ||
if (isPrefixed) { | ||
localizedRoute.path = `/${_locale}${localizedRoute.path}`; | ||
} | ||
localizedRoute.path = trailingSlash ? withTrailingSlash(localizedRoute.path, true) : withoutTrailingSlash(localizedRoute.path, true); | ||
} | ||
} else { | ||
if (!localizedRoute.name && !localizedRoute.path) { | ||
localizedRoute.name = getRouteBaseName.call(this, this.route); | ||
} | ||
localizedRoute.name = getLocaleRouteName(localizedRoute.name, _locale, { | ||
defaultLocale, | ||
strategy, | ||
routesNameSeparator, | ||
defaultLocaleRouteNameSuffix | ||
}); | ||
if (vueDemi.isVue2) { | ||
const { params } = localizedRoute; | ||
if (params && params["0"] === void 0 && params.pathMatch) { | ||
params["0"] = params.pathMatch; | ||
} | ||
} | ||
} | ||
try { | ||
const resolvedRoute = router.resolve(localizedRoute); | ||
if (vueDemi.isVue3 ? resolvedRoute.name : resolvedRoute.route.name) { | ||
return resolvedRoute; | ||
} | ||
return router.resolve(route); | ||
} catch (e) { | ||
if (vueDemi.isVue3 && e.type === 1) { | ||
return null; | ||
} else if (vueDemi.isVue2) { | ||
return null; | ||
} | ||
} | ||
} | ||
function switchLocalePath(locale) { | ||
const route = this.route; | ||
const name = getRouteBaseName.call(this, route); | ||
if (!name) { | ||
return ""; | ||
} | ||
const { params, ...routeCopy } = !vueDemi.isVue3 && vueDemi.isRef(route) ? route.value : route; | ||
const langSwitchParams = {}; | ||
const _baseRoute = { | ||
name, | ||
params: { | ||
...params, | ||
...langSwitchParams | ||
} | ||
}; | ||
if (vueDemi.isVue2) { | ||
_baseRoute.params[0] = params.pathMatch; | ||
} | ||
const baseRoute = assign({}, routeCopy, _baseRoute); | ||
const path = localePath.call(this, baseRoute, locale); | ||
return path; | ||
} | ||
function localeHead({ addDirAttribute = false, addSeoAttributes = false } = {}) { | ||
const router = this.router; | ||
const i18n = this.i18n; | ||
const { defaultDirection } = getI18nRoutingOptions(router, this); | ||
const metaObject = { | ||
htmlAttrs: {}, | ||
link: [], | ||
meta: [] | ||
}; | ||
if (i18n.locales == null || i18n.__baseUrl == null) { | ||
return metaObject; | ||
} | ||
const locale = getLocale(i18n); | ||
const locales = getLocales(i18n); | ||
const currentLocale = getNormalizedLocales(locales).find((l) => l.code === locale) || { | ||
code: locale | ||
}; | ||
const currentLocaleIso = currentLocale.iso; | ||
const currentLocaleDir = currentLocale.dir || defaultDirection; | ||
if (addDirAttribute) { | ||
metaObject.htmlAttrs.dir = currentLocaleDir; | ||
} | ||
if (addSeoAttributes && locale && i18n.locales) { | ||
if (currentLocaleIso) { | ||
metaObject.htmlAttrs.lang = currentLocaleIso; | ||
} | ||
addHreflangLinks.call(this, locales, i18n.__baseUrl, metaObject.link); | ||
addCanonicalLinks.call(this, i18n.__baseUrl, metaObject.link, addSeoAttributes); | ||
addCurrentOgLocale(currentLocale, currentLocaleIso, metaObject.meta); | ||
addAlternateOgLocales(locales, currentLocaleIso, metaObject.meta); | ||
} | ||
return metaObject; | ||
} | ||
function addHreflangLinks(locales, baseUrl, link) { | ||
const router = this.router; | ||
const { defaultLocale, strategy } = getI18nRoutingOptions(router, this); | ||
if (strategy === STRATEGIES.NO_PREFIX) { | ||
return; | ||
} | ||
const localeMap = /* @__PURE__ */ new Map(); | ||
for (const locale of locales) { | ||
const localeIso = locale.iso; | ||
if (!localeIso) { | ||
warn("Locale ISO code is required to generate alternate link"); | ||
continue; | ||
} | ||
const [language, region] = localeIso.split("-"); | ||
if (language && region && (locale.isCatchallLocale || !localeMap.has(language))) { | ||
localeMap.set(language, locale); | ||
} | ||
localeMap.set(localeIso, locale); | ||
} | ||
for (const [iso, mapLocale] of localeMap.entries()) { | ||
const localePath2 = switchLocalePath.call(this, mapLocale.code); | ||
if (localePath2) { | ||
link.push({ | ||
hid: `i18n-alt-${iso}`, | ||
rel: "alternate", | ||
href: toAbsoluteUrl(localePath2, baseUrl), | ||
hreflang: iso | ||
}); | ||
} | ||
} | ||
if (defaultLocale) { | ||
const localePath2 = switchLocalePath.call(this, defaultLocale); | ||
if (localePath2) { | ||
link.push({ | ||
hid: "i18n-xd", | ||
rel: "alternate", | ||
href: toAbsoluteUrl(localePath2, baseUrl), | ||
hreflang: "x-default" | ||
}); | ||
} | ||
} | ||
} | ||
function addCanonicalLinks(baseUrl, link, seoAttributesOptions) { | ||
const route = this.route; | ||
const currentRoute = localeRoute.call(this, { | ||
...route, | ||
name: getRouteBaseName.call(this, route) | ||
}); | ||
if (currentRoute) { | ||
let href = toAbsoluteUrl(currentRoute.path, baseUrl); | ||
const canonicalQueries = isObject(seoAttributesOptions) && seoAttributesOptions.canonicalQueries || []; | ||
if (canonicalQueries.length) { | ||
const currentRouteQueryParams = currentRoute.query; | ||
const params = new URLSearchParams(); | ||
for (const queryParamName of canonicalQueries) { | ||
if (queryParamName in currentRouteQueryParams) { | ||
const queryParamValue = currentRouteQueryParams[queryParamName]; | ||
if (isArray(queryParamValue)) { | ||
queryParamValue.forEach((v) => params.append(queryParamName, v || "")); | ||
} else { | ||
params.append(queryParamName, queryParamValue || ""); | ||
} | ||
} | ||
} | ||
const queryString = params.toString(); | ||
if (queryString) { | ||
href = `${href}?${queryString}`; | ||
} | ||
} | ||
link.push({ | ||
hid: "i18n-can", | ||
rel: "canonical", | ||
href | ||
}); | ||
} | ||
} | ||
function addCurrentOgLocale(currentLocale, currentLocaleIso, meta) { | ||
const hasCurrentLocaleAndIso = currentLocale && currentLocaleIso; | ||
if (!hasCurrentLocaleAndIso) { | ||
return; | ||
} | ||
meta.push({ | ||
hid: "i18n-og", | ||
property: "og:locale", | ||
content: hypenToUnderscore(currentLocaleIso) | ||
}); | ||
} | ||
function addAlternateOgLocales(locales, currentLocaleIso, meta) { | ||
const localesWithoutCurrent = locales.filter((locale) => { | ||
const localeIso = locale.iso; | ||
return localeIso && localeIso !== currentLocaleIso; | ||
}); | ||
if (localesWithoutCurrent.length) { | ||
const alternateLocales = localesWithoutCurrent.map((locale) => ({ | ||
hid: `i18n-og-alt-${locale.iso}`, | ||
property: "og:locale:alternate", | ||
content: hypenToUnderscore(locale.iso) | ||
})); | ||
meta.push(...alternateLocales); | ||
} | ||
} | ||
function hypenToUnderscore(str) { | ||
return (str || "").replace(/-/g, "_"); | ||
} | ||
function toAbsoluteUrl(urlOrPath, baseUrl) { | ||
if (urlOrPath.match(/^https?:\/\//)) { | ||
return urlOrPath; | ||
} | ||
return baseUrl + urlOrPath; | ||
} | ||
function proxyForComposable(options, target) { | ||
const { | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
strategy, | ||
defaultLocaleRouteNameSuffix, | ||
trailingSlash, | ||
routesNameSeparator | ||
} = options; | ||
return function(...args) { | ||
return Reflect.apply( | ||
target, | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
strategy, | ||
defaultLocaleRouteNameSuffix, | ||
trailingSlash, | ||
routesNameSeparator | ||
}, | ||
args | ||
); | ||
}; | ||
} | ||
function useRouteBaseName(givenRoute = VueRouter3.useRoute(), { router = VueRouter3.useRouter(), routesNameSeparator = void 0 } = {}) { | ||
const proxy = { | ||
router, | ||
route: givenRoute, | ||
routesNameSeparator | ||
}; | ||
return getRouteBaseName.call(proxy, givenRoute); | ||
} | ||
function useLocalePath({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ router, route, i18n, defaultLocale, defaultLocaleRouteNameSuffix, routesNameSeparator, strategy, trailingSlash }, | ||
localePath | ||
); | ||
} | ||
function useLocaleRoute({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
defaultLocaleRouteNameSuffix, | ||
routesNameSeparator, | ||
strategy, | ||
trailingSlash | ||
}, | ||
localeRoute | ||
); | ||
} | ||
function useLocaleLocation({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
defaultLocaleRouteNameSuffix, | ||
routesNameSeparator, | ||
strategy, | ||
trailingSlash | ||
}, | ||
localeLocation | ||
); | ||
} | ||
function useSwitchLocalePath({ | ||
router = VueRouter3.useRouter(), | ||
route = VueRouter3.useRoute(), | ||
i18n = vueI18nBridge.useI18n(), | ||
defaultLocale = void 0, | ||
defaultLocaleRouteNameSuffix = void 0, | ||
routesNameSeparator = void 0, | ||
strategy = void 0, | ||
trailingSlash = void 0 | ||
} = {}) { | ||
return proxyForComposable( | ||
{ | ||
router, | ||
route, | ||
i18n, | ||
defaultLocale, | ||
defaultLocaleRouteNameSuffix, | ||
routesNameSeparator, | ||
strategy, | ||
trailingSlash | ||
}, | ||
switchLocalePath | ||
); | ||
} | ||
function useLocaleHead({ | ||
addDirAttribute = false, | ||
addSeoAttributes = false, | ||
strategy = void 0, | ||
defaultLocale = void 0, | ||
route = VueRouter3.useRoute(), | ||
router = VueRouter3.useRouter(), | ||
i18n = vueI18nBridge.useI18n() | ||
} = {}) { | ||
const _router = router; | ||
const metaObject = vueDemi.ref({ | ||
htmlAttrs: {}, | ||
link: [], | ||
meta: [] | ||
}); | ||
function cleanMeta() { | ||
metaObject.value = { | ||
htmlAttrs: {}, | ||
link: [], | ||
meta: [] | ||
}; | ||
} | ||
function updateMeta(_route) { | ||
metaObject.value = Reflect.apply( | ||
localeHead, | ||
{ | ||
router, | ||
route: _route, | ||
i18n, | ||
defaultLocale, | ||
strategy | ||
}, | ||
[{ addDirAttribute, addSeoAttributes }] | ||
); | ||
} | ||
if (inBrowser) { | ||
if (vueDemi.isVue3) { | ||
const stop = vueDemi.watchEffect(() => { | ||
cleanMeta(); | ||
updateMeta(toRawRoute(_router.currentRoute)); | ||
}); | ||
vueDemi.onUnmounted(() => stop()); | ||
} else { | ||
const handler = _router.afterEach( | ||
(to, from) => { | ||
cleanMeta(); | ||
updateMeta(to); | ||
} | ||
); | ||
vueDemi.onUnmounted(() => handler()); | ||
updateMeta(route); | ||
} | ||
} else { | ||
updateMeta(toRawRoute(_router.currentRoute)); | ||
} | ||
return metaObject; | ||
} | ||
const VERSION = "0.1.5"; | ||
exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL; | ||
exports.DEFAULT_DETECTION_DIRECTION = DEFAULT_DETECTION_DIRECTION; | ||
exports.DEFAULT_LOCALE = DEFAULT_LOCALE; | ||
exports.DEFAULT_LOCALE_ROUTE_NAME_SUFFIX = DEFAULT_LOCALE_ROUTE_NAME_SUFFIX; | ||
exports.DEFAULT_ROUTES_NAME_SEPARATOR = DEFAULT_ROUTES_NAME_SEPARATOR; | ||
exports.DEFAULT_STRATEGY = DEFAULT_STRATEGY; | ||
exports.DEFAULT_TRAILING_SLASH = DEFAULT_TRAILING_SLASH; | ||
exports.STRATEGIES = STRATEGIES; | ||
exports.VERSION = VERSION; | ||
exports.createLocaleFromRouteGetter = createLocaleFromRouteGetter; | ||
exports.createRouter = createRouter; | ||
exports.extendI18n = extendI18n; | ||
exports.findBrowserLocale = findBrowserLocale; | ||
exports.getGlobalOptions = getGlobalOptions; | ||
exports.getLocale = getLocale; | ||
exports.getLocaleCodes = getLocaleCodes; | ||
exports.getLocales = getLocales; | ||
exports.getLocalesRegex = getLocalesRegex; | ||
exports.getRouteBaseName = getRouteBaseName; | ||
exports.isComposer = isComposer; | ||
exports.isExportedGlobalComposer = isExportedGlobalComposer; | ||
exports.isI18nInstance = isI18nInstance; | ||
exports.isLegacyVueI18n = isLegacyVueI18n; | ||
exports.isVueI18n = isVueI18n; | ||
exports.localeHead = localeHead; | ||
exports.localeLocation = localeLocation; | ||
exports.localePath = localePath; | ||
exports.localeRoute = localeRoute; | ||
exports.localizeRoutes = localizeRoutes; | ||
exports.proxyVueInstance = proxyVueInstance; | ||
exports.registerGlobalOptions = registerGlobalOptions; | ||
exports.resolveBaseUrl = resolveBaseUrl; | ||
exports.resolveRoute = resolveRoute; | ||
exports.setLocale = setLocale; | ||
exports.switchLocalePath = switchLocalePath; | ||
exports.useLocaleHead = useLocaleHead; | ||
exports.useLocaleLocation = useLocaleLocation; | ||
exports.useLocalePath = useLocalePath; | ||
exports.useLocaleRoute = useLocaleRoute; | ||
exports.useRouteBaseName = useRouteBaseName; | ||
exports.useSwitchLocalePath = useSwitchLocalePath; |
{ | ||
"name": "vue-i18n-routing", | ||
"description": "The i18n routing with using vue-i18n", | ||
"version": "0.1.4", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"typecheck": "tsc -p . --noEmit", | ||
"switch:2": "vue-demi-switch 2 vue2 && vue-router-switch 3 vue-router3 && vue-i18n-switch 8 vue-i18n-legacy", | ||
"switch:3": "vue-demi-switch 3 && vue-router-switch 4 && vue-i18n-switch 9", | ||
"test": "vitest run", | ||
"test:watch": "vitest", | ||
"test:cover": "vitest --coverage", | ||
"build:docs": "api-docs-gen ./temp/vue-i18n-routing.api.json -c ./docsgen.config.js -o ./ -g noprefix" | ||
}, | ||
"version": "0.1.5-57be896", | ||
"dependencies": { | ||
@@ -28,3 +17,3 @@ "@intlify/shared": "next", | ||
"typescript": "^4.8.3", | ||
"vite": "^3.0.0", | ||
"vite": "^3.1.2", | ||
"vite-plugin-dts": "^0.9.6", | ||
@@ -119,3 +108,14 @@ "vitest": "^0.23.4", | ||
}, | ||
"sideEffects": false | ||
} | ||
"sideEffects": false, | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"typecheck": "tsc -p . --noEmit", | ||
"switch:2": "vue-demi-switch 2 vue2 && vue-router-switch 3 vue-router3 && vue-i18n-switch 8 vue-i18n-legacy", | ||
"switch:3": "vue-demi-switch 3 && vue-router-switch 4 && vue-i18n-switch 9", | ||
"test": "vitest run", | ||
"test:watch": "vitest", | ||
"test:cover": "vitest --coverage", | ||
"build:docs": "api-docs-gen ./temp/vue-i18n-routing.api.json -c ./docsgen.config.js -o ./ -g noprefix" | ||
} | ||
} |
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
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
138211
3797
1
0